tags:

views:

267

answers:

1

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

+1  A: 

to answer my own question, I found this scrap on Codeplex/Unity home:

<type type="IFoo" mapTo="ServerFoo" name="server" />
<type type="IFoo" mapTo="ClientFoo" name="client">
    <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,Microsoft.Practices.Unity.Configuration">
      <constructor>
         <param name="server" parameterType="IFoo"> 
            <dependency name="server" />
         </param>
      </constructor>
     </typeConfig></type>

Notice that the type element has an additional field: 'name' that I was unaware of. This provides the 'registration name' value I was looking for.

Also the param element also has a subelement named dependency with a name attribute that performs the same function.

So... Thanks to codeplex!

Kimball

Use the xsd to the config to avoid "missing" out on stuff in the future ;-): http://unitycontributions.codeplex.com/WorkItem/View.aspx?WorkItemId=2309
Johan Leino
Please, mister, any sample (with all complete source and testing) using IEnlistmentNotification" ?? thanks
alhambraeidos