Max*_*der 1 interface asp.net-web-api
我是对接口进行编码的忠实粉丝.在WCF世界中,这一点非常强调每项服务.但是,我正在深入研究ASP.NET Web Api,作为MVC 4的扩展,我很想知道是否有一些典型的推理让你的控制器实现继承自接口.
我知道ASP.NET Web Api的默认设置看起来像这样
public class TestsController : ApiController
{
public IEnumerable<string> Get()
{
var testValues = new List<string>
{
"Something",
"Welcome to the top of the world"
};
return testValues;
}
}
Run Code Online (Sandbox Code Playgroud)
与此相反,具有相应的接口(或接口).
public class TestsController : ApiController, ITestsController
{
public IEnumerable<string> Get()
{
var testValues = new List<string>
{
"Something",
"Welcome to the top of the world"
};
return testValues;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为使用类似于控制器的接口没有任何价值.WCF很大程度上依赖于接口,因为它对于SOAP服务来说是常见的,它是用于在WSDL中生成契约的东西.但是,HTTP Web API没有这种合同,拥有它通常是一种不好的做法.此外,当您需要使用依赖项注入和单元测试时,接口很常见,因为它们允许您轻松伪造依赖项.但是,我认为你不会将控制器作为依赖注入另一个类,所以它也没有意义.
| 归档时间: |
|
| 查看次数: |
1998 次 |
| 最近记录: |