Recently I had merged Unity container and Factory pattern to create objects in a central place. Below is the code for the same. Thought it would be useful to you guys!!
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
public class DependencyFactory
{
public static T GetObject()
{
IUnityContainer container = new UnityContainer();
UnityConfigurationSection section =
(UnityConfigurationSection)ConfigurationManager.GetSection(ConfigurationManager.AppSettings["UnityContainer"]);
section.Configure(container);
T res = container.Resolve();
return res;
}
public static T GetObject(params ResolverOverride[] overrides)
{
IUnityContainer container = new UnityContainer();
UnityConfigurationSection section =
(UnityConfigurationSection)ConfigurationManager.GetSection(ConfigurationManager.AppSettings["UnityContainer"]);
section.Configure(container);
T res = container.Resolve(overrides);
return res;
}
}
<configuration>
<configSections>
<section name="TEST_UNITY"
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
Microsoft.Practices.Unity.Configuration" />
</configSections>
<TEST_UNITY>
<containers>
<container>
<types>
<type
type="Unity.Sample.Repositories.IEnrollmentRepository,Unity.Sample"
mapTo="Unity.Sample.Repositories.DummyEnrollmentRepository,
Unity.Sample" />
<type
type="Unity.Sample.Repositories.ICustomerDetailsRepository,Unity.Sample"
mapTo="Unity.Sample.Repositories.DummyCustomerDetailsRepository,
Unity.Sample" />
<type
type="Unity.Sample.Repositories.ICustomerRepository,Unity.Sample"
mapTo="Unity.Sample.Repositories.DummyCustomerRepository,
Unity.Sample" />
<type
type="Unity.Sample.Repositories.IBIBRepository,Unity.Sample"
mapTo="Unity.Sample.Repositories.DummyBIBRepository,
Unity.Sample" />
<type
type="Unity.Sample.Repositories.ILockBoxRepository,Unity.Sample"
mapTo="Unity.Sample.Repositories.DummyLockBoxRepository,
Unity.Sample" />
<type
type="Unity.Sample.Interfaces.IBIB,Unity.Sample"
mapTo="Unity.Sample.Impl.DummyBIB,
Unity.Sample" />
<type
type="Unity.Sample.Interfaces.ILockBox,Unity.Sample"
mapTo="Unity.Sample.Impl.DummyLockBox,
Unity.Sample" />
</types>
</container>
</containers>
</TEST_UNITY>
<appSettings>
<add key="UnityContainer" value="TEST_UNITY"/>
</appSettings>
</configuration>
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
public class DependencyFactory
{
public static T GetObject
{
IUnityContainer container = new UnityContainer();
UnityConfigurationSection section =
(UnityConfigurationSection)ConfigurationManager.GetSection(ConfigurationManager.AppSettings["UnityContainer"]);
section.Configure(container);
T res = container.Resolve
return res;
}
public static T GetObject
{
IUnityContainer container = new UnityContainer();
UnityConfigurationSection section =
(UnityConfigurationSection)ConfigurationManager.GetSection(ConfigurationManager.AppSettings["UnityContainer"]);
section.Configure(container);
T res = container.Resolve
return res;
}
}
<configuration>
<configSections>
<section name="TEST_UNITY"
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
Microsoft.Practices.Unity.Configuration" />
</configSections>
<TEST_UNITY>
<containers>
<container>
<types>
<type
type="Unity.Sample.Repositories.IEnrollmentRepository,Unity.Sample"
mapTo="Unity.Sample.Repositories.DummyEnrollmentRepository,
Unity.Sample" />
<type
type="Unity.Sample.Repositories.ICustomerDetailsRepository,Unity.Sample"
mapTo="Unity.Sample.Repositories.DummyCustomerDetailsRepository,
Unity.Sample" />
<type
type="Unity.Sample.Repositories.ICustomerRepository,Unity.Sample"
mapTo="Unity.Sample.Repositories.DummyCustomerRepository,
Unity.Sample" />
<type
type="Unity.Sample.Repositories.IBIBRepository,Unity.Sample"
mapTo="Unity.Sample.Repositories.DummyBIBRepository,
Unity.Sample" />
<type
type="Unity.Sample.Repositories.ILockBoxRepository,Unity.Sample"
mapTo="Unity.Sample.Repositories.DummyLockBoxRepository,
Unity.Sample" />
<type
type="Unity.Sample.Interfaces.IBIB,Unity.Sample"
mapTo="Unity.Sample.Impl.DummyBIB,
Unity.Sample" />
<type
type="Unity.Sample.Interfaces.ILockBox,Unity.Sample"
mapTo="Unity.Sample.Impl.DummyLockBox,
Unity.Sample" />
</types>
</container>
</containers>
</TEST_UNITY>
<appSettings>
<add key="UnityContainer" value="TEST_UNITY"/>
</appSettings>
</configuration>