So, I have a:
Appendix A: Class B required (other assembly)
Class B: Class C required (again, another assembly)
Class C: a container is used to resolve various objects, but the lifetime of the container (and the objects allowed by it) must be controlled by the root of the composition.
I think I understand how this will work in most cases, but in class C I need to resolve based on the property of the passed object.
I think I am asking if the container has a dependency, and as such, what is the best way to get it where it is needed (not sure if I really would like to pass it through a bunch of constructors - will there be an embedding of the properties in the path?)
I believe this source is as clean and simple as I can get:
namespace InjectionTest { using System; using Microsoft.Practices.Unity; public class ApplicationA { static void Main(string[] args) { using (IUnityContainer container = new UnityContainer()) {
Edit: so I did this with the introduction of the properties:
I changed ClassC:
public class ClassC : IClassC { [Dependency] public IUnityContainer Container { get; set; } public ResultObject BuildMyFoo(InitialObject bar) { IFooBuilder builder = null;
and updated my main method in ApplicationA:
public void Main() { using (IUnityContainer container = new UnityContainer()) { // Normally I'd use this, but for clarity in the example, I'm doing it in code. //container.LoadConfiguration(); container.RegisterType<IClassB, ClassB>(); container.RegisterType<IClassC, ClassC>(); container.RegisterType<IFooBuilder, FrobBuilder>("frob"); container.RegisterType<IFooBuilder, WidgetBuilder>("widget"); using (IUnityContainer child = container.CreateChildContainer()) { container.RegisterInstance<IUnityContainer>(child); IClassB machine = container.Resolve<IClassB>(); InitialObject bar = new InitialObject() { Name = "widget" }; machine.doSomethingWithBar(bar); bar = new InitialObject() { Name = "frob" }; machine.doSomethingWithBar(bar); } } }