Thursday, December 24, 2009

Unity Application Block – Loading configuration information


The Unity Application Block does not automatically read the configuration information or create and prepare containers. You must programmatically instantiate a Unity container in your application. You can programmatically configure it with registrations, type mappings, and any extensions or you can configure it by reading configuration information from a file.
The following example code demonstrates how to configure unity container and load the instances from the configuration information.
public interface IRepository
{
    void Insert(T instance);
    void Update(T instance);
    T GetById(Id id);
    IEnumerable GetAll();
    bool Delete(Id id);
}
The IRepository interface is defined in the assembly UnitySamples.Infrastructure. The implementation is defined in the assembly UnitySamples.DataAccessLayer in the class EmployeeRepository
public class EmployeeRepository : IRepository<Employee, Guid>
{
    public EmployeeRepository()
    {
        ___EmployeeCollection = EmployeeFactory.Create();
    }

    public void Insert(Employee instance)
    {
        ___EmployeeCollection.ToList().Add(instance);
    }

    public void Update(Employee instance)
    {
        var __Employee = GetById(instance.Id);
        if (__Employee == null) return;
        __Employee.FirstName = instance.FirstName;
        __Employee.LastName = instance.LastName;
    }

    public Employee GetById(Guid id)
    {
        return ___EmployeeCollection.First<Employee>(x => x.Id == id);
    }

    public IEnumerable<Employee> GetAll()
    {
        return ___EmployeeCollection;
    }

    public bool Delete(Guid id)
    {
        var __Count = ___EmployeeCollection.ToList().RemoveAll(x => x.Id == id);
        return __Count > 0;
    }

    static IEnumerable<Employee> ___EmployeeCollection;
}
My application configuration file looks like
<configuration>
  <configSections>
    <section name="unity"
             type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
                   Microsoft.Practices.Unity.Configuration,
                   Version=1.2.0.0,
                   Culture=neutral,
                   PublicKeyToken=31bf3856ad364e35" />
  configSections>
  <unity>
    <typeAliases>
     
      <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
     
      <typeAlias alias="IRepository`2" type="UnitySamples.Infrastructure.Interfaces.IRepository`2, UnitySamples.Infrastructure" />
      <typeAlias alias="employeeRepository" type="UnitySamples.DataAccessLayer.EmployeeRepository, UnitySamples.DataAccessLayer" />
    typeAliases>
    <containers>
      <container name="unityContainer">
        <types>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
          <type type="IRepository`2"
                mapTo="employeeRepository"
                name="EmployeeRepository">
            <lifetime type="singleton" />
          type>
        types>       
      container>
    containers>
  unity>
configuration>
As you can see I have mentioned the IRepository interface preceeding with a ‘`’ and 2. The .NET Framework convention for expressing generic types is ObjectWithOneType`1, where the digit following the "`" matches the number of types contained in the generic type.
To load the configuration information, I have my code as
IUnityContainer __Container = new UnityContainer();
UnityConfigurationSection __Section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
Assert.IsNotNull(__Section, "Failed to load unity section from configuration");

__Section.Containers["unityContainer"].Configure(__Container);

var __EmployeeRepository = __Container.Resolve<IRepository<Employee, Guid>>("EmployeeRepository");
Assert.IsNotNull(__EmployeeRepository, "Failed to load the repository instance from the configuration file mapping");

var __EmployeeCollection = __EmployeeRepository.GetAll();
Assert.IsNotNull(__EmployeeCollection, "Repository failed to retrieve employee data");
Assert.IsTrue(__EmployeeCollection.Count() > 0, "Repository failed to retrieve employee data");
We’ll continue looking into the details of the Unity Application block in the upcoming posts. Till then happy programming!!!.

No comments: