开发者

Typemock and Dependency Injection, still use interfaces?

I see some good design benefits in DI and not just good testability design. So even though I have Typemock and could unit test without IOC, I still prefer constructor DI. I think it's a great way to quickly discover the dependencies of a class.

Now I'm wondering should I keep using interfaces as the type parameter in the constructor. They are very easy to create using Resharper, but it's still a type that I don't really need.

A quick example of what I mean

public interface IService
{
    void Method();
}

public class Service : IService
{
    public void Method()
    {

    }
}
开发者_JAVA技巧
public class ClassThatUsesDI
{
    public ClassThatUsesDI(IService service) **or** (Service service) 
    {

    }
}

What are your thoughts?


With interfaces, you have the option to replace the actual type being used at runtime, based on configuration. You can do that with classes too, if you inherit from them, but it's easier still with just interfaces.

My advice is to stay with the interfaces.

And to be honest, I write my services the other way around. First I update the interfaces, because the "world" I'm in at that point is more limited, and makes me think more about what I can really do in the interface, and then I update the concrete types afterwards.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜