resolve type with name

Sep 17, 2009 at 3:21 AM

I am build my system with Unity. I need to utilize the emthod ResolveType<T>(string name). I found out that the name was not propagated to all of its parameters in the constructor, but just the implementation class itself.


For example, I have a class called ReportModule. It has a constrcutor with two parameters, IScenario, IPresenter. When I resolve the ReportModule, I hope the uniti container is able to fetch the implementation class by name and also for the implementation classes for IScenario, IPresenter, they are still fetched out by name, not just by Type. I guess at least for now, Untiy doesn't support it. I am not sure if this requirement is reasonable. I am just bringing it up to listern more your ideas. Thanks a lot!

Oct 1, 2009 at 7:12 AM

Just to make sure I understand the scenario you are describing, IPresenter is a dependency of IScenario, right?  And when you do this:


you want the IPresenter typemapping with the "nonDefault" key name.  Is this right?

Your exact requirement is not supported but you can get to the same result by registering it like this:

container.Register<IScenario, Scenario>("nonDefault", new InjectionMembers [] { new InjectionProperty("Presenter", new ResolvedParameter(typeof(IPresenter), "nonDefault")) } );

It would also be trivial to create a helper class that will have this logic when registering a typemapping.


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

Oct 13, 2009 at 1:51 PM
Thanks a lot! I think you already resolve my problems.