且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

asp.net web api控制器接口有什么好处吗?

更新时间:2023-02-15 20:48:33

我认为使用类似于控制器的接口没有任何价值。 WCF很大程度上依赖于接口,因为它是SOAP服务的常见问题,它是用于在WSDL中生成契约的东西。但是,HTTP Web API没有这种合同,拥有它通常是一种不好的做法。此外,当您需要使用依赖项注入和单元测试时,接口很常见,因为它们允许您轻松伪造依赖项。但是,我认为你不会将控制器作为依赖注入另一个类,所以它也没有意义。

I think there is no value in using an interface like that for a controller. WCF relies a lot on interfaces because it's a common thing for SOAP services, and it is something used for generating the contracts in WSDL. However, an HTTP Web API does not have that kind of contract, and it is typically a bad practice to have it. Also, interfaces are common when you need to use dependency injection and unit testing because they allow you to easily fake dependencies. However, I don't think you would inject a controller as dependency into another class, so it does not make sense there too.