0

I have an interface which takes a generic type

interface IIFace<T>

Then i have several implementations

public class IFaceImp1 : IIFace<MyObj> {}
public class IFaceImp2 : IIFace<MyObj> {}

I defined a viewmodel which gets injected into it an object of this interface for a specific type...

public MainViewModel(IIFace<MyObj> scrapper) { }

And then i configure Unity to populate it

<containers>
    <container>
        <register type="IIFace[MyObj]" mapTo="IFaceImp1">
            <constructor>
                <param name="loc" value="i am just a mock">
                </param>
            </constructor>
        </register>
    </container>
</containers>

This works fine The problem is, now i wanted to create named registrations, so that later i can resolve them by name... So I named it, and created another one first, bu t that didn't work. I reduced the problem to this :

<containers>
    <container>
        <register type="IIFace[MyObj]" mapTo="IFaceImp1" name="FirstImplementation">
            <constructor>
                <param name="loc" value="i am just a mock">
                </param>
            </constructor>
        </register>
    </container>
</containers>

Which also doesn't work... wh en trying to Resolve<>("FirstImplementation"). However, if I remove the name attribute and use the Resolve<>() overload, it works fine.. Any ideas about what could be happening, that having a name this doesn't work??

1
  • I think that the problem is that when there's no name, when trying to instantiate MainViewModel, Unity calls the default registered type in the container. However, when it has a name, there's no way to set that name in the Register call. I think that (maybe i'm wrong) i need to tell unity somehow that there's a named dependency inside, but i don't know how to do this Dec 14, 2010 at 14:04

1 Answer 1

0

Ok, i found out what was the problem I needed to use the tag, building my object model in the xml the end result is something similar to this :

<containers>
    <container>

        <register type="ViewModelBase" mapTo="MainViewModel" name="MockMainViewModel">
          <constructor>
            <param name="imp">
              <dependency name="FirstImplementation" />
            </param>
          </constructor>
        </register>

        <register type="IIFace[MyObj]" mapTo="IFaceImp1" name="FirstImplementation">
            <constructor>
                <param name="loc" value="i am just a mock">
                </param>
            </constructor>
        </register>
    </container>
</containers>

This way, this configuration is telling Unity to, when called with Resolve("MockMainViewModel"), instantiate it, and resolve (through dependency too) the parameter that needs in its constructor, with the specified name ("FirstImplementation") I was basically unaware of the tag, but anyway this may be useful to someone that comes up with the same problem and doesn't know why the resolution fails to work automatically, and what's the right approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.