Reputation: 804
I am new to IoC and I am playing with Unity. Let' say you have a solution with 'n' projects and you want to use Unity to register and resolve the dependencies. Lets say your composition root is in project a. Let's say you have the following projects in the solution.
a b c d
Lets say a depends on something in b, b depends on something in c and c depends on something in d
I have seen how you can use constructor injection to resolve the a => b dependency but I am stuck on how b's dependency on c can be resolved without access to the container that was configured and created in project a.
What is the approach for resolving the nested dependencies? Is there a discussion/blog/example addressing the resolution of deep dependencies?
Upvotes: 9
Views: 3651
Reputation: 48675
Your composition root should create and deliver all your dependencies, including nested ones, so it needs references to all the relevant assemblies (unless you're supplying them using reflection).
For example, you normally create an instance of B
(supplying its dependency, C
) before creating an A
. If you did it 'by hand', it would look like this:
C c = new C();
B b = new B(c);
A a = new A(b);
As long as you register all the appropriate types, your dependency injection framework will resolve them for you.
For a great article on the subject, see Miško Hevery's "Dependency Injection Myth: Reference Passing."
Upvotes: 9