views:

61

answers:

1

Hi

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()
    {

    }
}

public class ClassThatUsesDI
{
    public ClassThatUsesDI(IService service) **or** (Service service) 
    {

    }
}

What are your thoughts?

+2  A: 

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.

Lasse V. Karlsen
Writing the interfaces first makes good sense, but when I look at my last project, replacing the type by configuration would be YAGNI for me.
Karsten