Registering component with Unity

Topics: General discussion
Jul 31, 2012 at 2:17 AM

Hi,

I am facing issue with named registration of component with unity. If I register a component (which has a depedency property set) without named attribute set it works fine. However when I set the name attribute then its depedency property is not set and remains null.

For example -

following works fine -

 

<

 

container

>

 

<

 

register name="MemoryAdapter" type="IBusinessAdapter" mapTo="InMemoryAdapter"

>

</

 

register

>

 

<

 

register name="SqlAdapter" type="IBusinessAdapter" mapTo="SqlAdapter"

/>

 

<

 

register type="ICustomerController" mapTo="CustomerController"

>

<

 

property name="BusinessAdapter" dependencyName="MemoryAdapter" dependencyType="IBusinessAdapter"></property

>

</

 

register

>



</container

 

>

 But when I try to register the component CustomerController with a name its depedency property is not set and remains as null. Like the following code does not work properely.

 <container>
<register name="MemoryAdapter" type="IBusinessAdapter" mapTo="InMemoryAdapter">
      </register>
     
      <register name="SqlAdapter"  type="IBusinessAdapter" mapTo="SqlAdapter"/>
     
      <register type="ICustomerController" mapTo="CustomerController" name="xxx">
        <property name="BusinessAdapter" dependencyName="MemoryAdapter" dependencyType="IBusinessAdapter">
        </property>
      </register>

    
    </container>

Is something missed by me in the configuration ?

 

regards,

 

vikas kapoor

Jul 31, 2012 at 2:44 AM

Everything looks good to me.  Configuration:

    <container>
      <register name="MemoryAdapter" type="IBusinessAdapter" mapTo="InMemoryAdapter">
      </register>

      <register name="SqlAdapter"  type="IBusinessAdapter" mapTo="SqlAdapter"/>

      <register type="ICustomerController" mapTo="CustomerController" name="xxx">
        <property name="BusinessAdapter" dependencyName="MemoryAdapter" dependencyType="IBusinessAdapter">
        </property>
      </register>
    </container>

And the code:

    public interface IBusinessAdapter {}

    public class SqlAdapter : IBusinessAdapter {}

    public class InMemoryAdapter : IBusinessAdapter {}

    public interface ICustomerController
    {
        IBusinessAdapter BusinessAdapter { get; set; }
    }

    public class CustomerController : ICustomerController
    {
        public IBusinessAdapter BusinessAdapter { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer();
            container.LoadConfiguration();

            var xxx = container.Resolve<ICustomerController>("xxx");
        }
    }

ICustomerController is resolved with an InMemoryAdapter injected.  If the configuration is correct, then can you post some code which recreates the issue?

Also, just a note that Unity has a separate forum for Unity specific issues: http://unity.codeplex.com/discussions.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Jul 31, 2012 at 5:31 AM

Hi,

Thanks for the reply. I have identified the issue, it was with the container.Resolve. Now it is working.

Regards,

Vikas Kapoor