How do I passthrough the name arguement to child dependancies

May 16, 2013 at 11:21 AM

I've been trying to work this out but come to a point where I need a bit of guidance before designing this wrong.

I have a number of named registered Types of say IMyClass each with a different name argument.

UnityInstance.Container.RegisterType<IMyClass, FooClass>("FOO");
UnityInstance.Container.RegisterType<IMyClass, BarClass>("BAR");

for which the constructor has another dependancy

public FooClass(IAnotherClass anotherClass)

If I have a single registration of IAnotherClass then injection is great, if however I have another 2 registrations of IAnotherClass

UnityInstance.Container.RegisterType<IAnotherClass, FooAnotherClass>("FOO");
UnityInstance.Container.RegisterType<IAnotherClass, BarAnotherClass>("BAR");

how can I pass the name instance through

i.e. when resolving FooClass I want to inject FooAnotherClass.

I was hoping to use child containers but I cant see how to name them to keep the registrations together.

Basically I am trying to build a generic process for creating 'things' in more than one external system and while the base code is all the same obviously there are differences which I want to encapsulate on their own.

Help much appreciated.

May 16, 2013 at 3:57 PM
Maybe this article can help you.

It's an extension I wrote a while ago.