Named registration question...

Jun 25, 2011 at 11:26 AM

Hi all, Unity newbie here.

I'm playing around with Unity and so far so good.

Have a situation now though where I want to register 2 different classes to the same interface.

To do this it seems I need to register named types else it gets duplicate key error, which makes sence.

However when I add a "name" to my registration i get the following error.

This works...

      <register type="ICurrencyAdapter" mapTo="SqlCurrencyAdapter">
        <lifetime type="ContainerControlledLifetimeManager" />
        <interceptor type="InterfaceInterceptor" />
        <policyInjection />
This Errors with "name"...
      <register type="ICurrencyAdapter" mapTo="SqlCurrencyAdapter" name="Test">
        <lifetime type="ContainerControlledLifetimeManager" />
        <interceptor type="InterfaceInterceptor" />
        <policyInjection />

{"The current type, DataAccess.ICurrencyAdapter, is an interface and cannot be constructed. Are you missing a type mapping?"}

I've spent a while looking at this (and it's now past my bedtime) but can't figure it out?

What am i missing?



Jun 26, 2011 at 4:41 AM

Did you pass the name when you called container.Resolve? In other words, do:

var adapter = container.Resolve<ICurrencyAdapter>("Test");

You have to use the name that matches what you registered, otherwise it tries to do the default.

Jun 26, 2011 at 11:08 AM

Thanks for that, you put me on the right track.

I'm using constructor injection and configuring everything in config file.

What i needed to do is add constructor & parameter info to my registration with the parameter using the named registration.

      <register type="ICurrencyBusinessManager" mapTo="CurrencyBusinessManager">
          <param name="logWriter" />
          <param name="currencyAdapter">
            <dependency name="test" type="ICurrencyAdapter" />