2
Vote

How to inject child container to injected classes.

description

What is the right way to inject the per request child container to an injected member?

I have a class that discovers, registers, and resolves classes at runtime with Unity. I register the dependencies for these classes with the container passed to UnityDependencyResolver.

I also register the class that does the discovering with the original container and use an InjectionConstructor to inject that container into the class.

The issue comes with types registered with a HierarchicalLifetimeManager, for example my repository. Because my class has the parent container and the controller is resolved with a child container, I am getting multiple instances of my repository.

How can I solve this issue?

comments

DevTrends wrote Jun 5, 2012 at 4:09 PM

If the class that does the discovering takes in an IUnityContainer as a constructor argument, it will be provided with the child container. No InjectionConstructor is necessary unless you are using that for some other reason.

cadrell0 wrote Jun 5, 2012 at 4:53 PM

Thanks, I didn't realize that.