I have a class with a DAL dependency:
public class FirstClass()
{
[Dependency]
public IDalType Dal { get; set; }
}
I can create an instance of the class with:
UnityContainer c = new UnityContainer();
c.RegisterType<IDalType, DalType>();
FirstClass fc = c.Resolve<FirstClass>();
This will set the dependency and I can use the instance. This is something like a starting main class (main form, main ViewModel).
Now, I have a SecondClass
again with a DAL dependency and that second class must be instantiated and called from the first class. How do I do that?
public class FirstClass()
{
[Dependency]
public IDalType Dal { get; set; }
public DoSomething()
{
??? SecondClass sc = App.UnityContainer.Resolve<SecondClass>();
}
}
If I use the UnityContainer inside the first class, that will be a coupling with the container, and I will have to map IDAL -> DAL somewhere in application root and have a static container.
I read that coupling with the container is bad, as well as using static map: http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx