Register Type with Register Name in config file?

Jun 30, 2009 at 1:15 AM

When registering and resolving types in a Unity Container using code you can use 'Registration Names' to disambiguate your references that derive from an interface or base class hierarchy.

The 'registration name' text would be provided as a parameter to the register and resolve methods:

    myContainer.RegisterType<IMyService, CustomerService>("Customers");


    MyServiceBase result = myContainer.Resolve<MyServiceBase>("Customers");


However when I register types in the configuration files I do not see where the 'registration name' can be assigned

I register an Interface:

    <typeAlias alias="IEnlistmentNotification" type="System.Transactions.IEnlistmentNotification, System.Transactions, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

Then two types that I happen to know implement that interface:

    <typeAlias alias="PlaylistManager" type="Sample.Dailies.Grid.Workers.PlaylistManager, Sample.Dailies.Grid.Workers, Version=, Culture=neutral, PublicKeyToken=null" />

    <typeAlias alias="FlexAleManager" type="Sample.Dailies.Grid.Workers.FlexAleManager, Sample.Dailies.Grid.Workers, Version=, Culture=neutral, PublicKeyToken=null" />

Then I provide mappings between the interface and the two types:

    <type type="IEnlistmentNotification" mapTo="FlexAleManager"><lifetime type="singleton"/></type>

    <type type="IEnlistmentNotification" mapTo="PlaylistManager"><lifetime type="singleton"/></type>

That seems to correspond to this code:

    myContainer.RegisterType<IEnlistmentNotification, FlexAleManager>();
    myContainer.RegisterType<IEnlistmentNotification, PlaylistManager>();

but clearly what I need is a disambiguating config entry that corresponds to this code:

    myContainer.RegisterType<IEnlistmentNotification, FlexAleManager>("Flex");
    myContainer.RegisterType<IEnlistmentNotification, PlaylistManager>("Play");

Then when I get into my code I could do this:

    IEnlistmentNotification flex = myContainer.Resolve<IEnlistmentNotification>("Flex");
    IEnlistmentNotification play = myContainer.Resolve<IEnlistmentNotification>("Play");

See what I mean?




Jun 30, 2009 at 2:29 AM

Specify the registration name using the name attribute

 <type type="IEnlistmentNotification" mapTo="FlexAleManager" name="Flex" />


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.


Jun 30, 2009 at 4:16 AM


I stumbled on the fact myself just a little later.

Could have just thought about it a little more I guess.

Thank you,