What corresponds to the Unity 'registration name' in the unity configuration section?

Posted by unanswered on Stack Overflow See other posts from Stack Overflow or by unanswered
Published on 2009-06-29T23:20:21Z Indexed on 2010/06/07 16:02 UTC
Read the original article Hit count: 239

Filed under:

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");

and

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=2.0.0.0, 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=1.0.0.0, Culture=neutral, PublicKeyToken=null" />

<typeAlias alias="FlexAleManager" type="Sample.Dailies.Grid.Workers.FlexAleManager, Sample.Dailies.Grid.Workers, Version=1.0.0.0, 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?

Thanks,

Kimball

© Stack Overflow or respective owner

Related posts about unity