unity design time configuration

Oct 17, 2011 at 1:15 PM

Hi guys,

I'm a n00bie on the unity universe and I want to do a constructor injection like so 

uContainer.RegisterType<HC.Business.Interface.IBusinessManager<HC.Models.Contact>, Business.V2.ContactBusinessManager>(new InjectionConstructor(SessionProvider.GetInstance));           

contact = uContainer.Resolve<HC.Business.Interface.IBusinessManager<HC.Models.Contact>>();

 

but I want to export the register party in the web.config file.

this is what I've come to after some research but It doesn't work, I always get an error indicating that IBusinessManager is unrecognized

 <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">

<alias alias="Contact" type="HC.Models.Contact, HC.Models"/>   

<alias alias="ContactBusinessManager" type="Business.V2.ContactBusinessManager, Business.V2"/>   

<alias alias="SessionProvider" type="HC.Managers.NHibernate.SessionProvider.SessionProvider, HC.Managers.NHibernate.SessionProvider"/>   

<container name="BusinessManagersContainer">     

<register type="HC.Business.Interface.IBusinessManager[HC.Models.Contact]" mapTo="ContactBusinessManager>">               

<constructor>         

<param name="sessionProvider" value="SessionProvider.GetInstance" type="SessionProvider"  />       

</constructor>   

  </register> 

  </container>

  </unity>

 

Please Help Me!

Oct 24, 2011 at 6:36 AM

I've taken some liberties with your class model but hopefully it translates for you.  Also, I changed Session provider to be a container managed singleton (since that's what your use of GetInstance implied).

Here is the code:

public interface ISessionProvider
{
    int Version { get; }
}

public class SessionProvider : ISessionProvider
{
    public SessionProvider()
    {
    }

    public int Version
    {
        get;
    }
}

public interface IBusinessManager<T>
{
    string ID
    {
        get;
        set;
    }
}

public class Contact
{
}

public class ContactBusinessManager : IBusinessManager<Contact>
{
    public ContactBusinessManager(ISessionProvider sessionProvider)
    {
    }

    public string ID
    {
        get;
        set;
    }
}

class Program
{
    static void Main(string[] args)
    {
        UnityFromConfig();
    }

    static void UnityFromConfig()
    {
        IUnityContainer container = new UnityContainer();
        UnityConfigurationSection section = (UnityConfigurationSection) ConfigurationManager.GetSection("unity");
        section.Configure(container);

        var contact = container.Resolve<IBusinessManager<Contact>>();
    }
}

 And the configuration file:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
  </configSections>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <alias alias="ISessionProvider" type="UnityIBusinessManager.ISessionProvider, UnityIBusinessManager"/>
    <alias alias="SessionProvider"  type="UnityIBusinessManager.SessionProvider, UnityIBusinessManager"/>
    <alias alias="IBusinessManager" type="UnityIBusinessManager.IBusinessManager`1, UnityIBusinessManager" />
    <alias alias="Contact" type="UnityIBusinessManager.Contact, UnityIBusinessManager"/>
    <alias alias="ContactBusinessManager" type="UnityIBusinessManager.ContactBusinessManager, UnityIBusinessManager"/>
    <container>
      <register type="ISessionProvider" mapTo="SessionProvider">
        <lifetime type="singleton"/>
      </register>
      <register type="IBusinessManager[Contact]" mapTo="ContactBusinessManager">
        <constructor>
          <param name="sessionProvider" type="ISessionProvider"/>
        </constructor>
      </register>
    </container>
  </unity>
</configuration>

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

Oct 24, 2011 at 8:42 AM

Thank you randylevy for you response.

Your solution will, surely, resolve my problem. In fact this was almost the solution I've adobted so I can carry on with my project.

But I still wonder if it's possible to convert the registration part which I've done hard coded in a xml file