Reputation: 1670
It is possible to resolve the dependency by having a default constructor initializing the dependent object as below.
public class Foo:IFoo
{
public void disp()
{
//some code
}
}
public class MyClass
{
IFoo ifoo;
public MyClass():this(new Foo())
{
}
public MyClass(IFoo i)
{
this.ifoo = i;
}
void method1()
{
ifoo.disp();
}
}
Similarly, all dependencies could be resolved by having 2 constructors as above, with which the unit testing framework could use the parameterized constructor straight away.
In this case, what is the advantage of implementing IunityContainer and service locator to create an instance.
Upvotes: 0
Views: 129
Reputation: 27861
Having two constructors is considered an anti-pattern. Take a look at this article.
Using the container as a service locator inside your classes is also considered an anti-pattern. Take a look at this article and this article.
You should have a single constructor that takes in all dependencies, and then you should create the object in the Composition Root.
Upvotes: 7