Resolving object graph based on named

Sep 24, 2012 at 10:04 AM

I have multiple implementation of ITrace in my code, Each service layer object uses its own ITrace implementation. When I resolve my service layer, I want all objects in graph should resolve based on name in resolving root.

presently I am achieving this by using InectionConstructor like

container.RegisterType<IType1, Type1>("name1");

container.RegisterType<IType1, Type1_2>("name2");

container.RegisterType<IType2, Type2>("name1", new InjectionConstructor(new ResolvedParameter<IType1>("name1"));

container.RegisterType<IType2, Type2_1>("name2", new InjectionConstructor(new ResolvedParameter<IType1>("name2"));

 

in second and third line I have to tell that when resolving "name1", all object graph should be resolved on same "name1". Otherwise Unity try to resolve default registration.

Is there a way I can tell unity to resolve all objects in the graph using "name1" registration

Thanks

Sep 24, 2012 at 10:12 AM

The default way in Unity would be to register your mappings with InjectionConstructors to specify the exact mapping to use. I think this is clumsy and clutters your registration code with a lot of unneccessary noise.

Have a look at this article. It describes an extension that allows you to group mappings and resolve them as one.