Can someone explain to me the advantages of using an IOC container over simply hardcoding the default implementation into a default constructor?
In other words, what is wrong about this code?
public class MyClass
{
private IMyInterface _myInterface;
public MyClass()
{
_myInterface = new DefaultMyInterface();
}
public MyClass(IMyInterface myInterface)
{
_myInterface = myInterface;
}
}
As far as I can tell, this class supports constructor injection enough so unit testing and mocking is easily done. In addition to which, the default constructor removes the computational overhead of the IOC container (not to mention the whole process is a lot more transparent).
The only benefits i can see to using an IOC container is if you need to switch out the implementation of your interfaces frequently. Am I missing something?