Unity and SharePoint – Part 2

In the previous article about Unity and SharePoint I showed you about the basics of dependency injection and we touched a little bit about how to use unity. What we didn´t look at was how to configure the Unity framework. In this post we´ll go into that.

Unity and SharePoint – Part 1
Unity and SharePoint – Part 3

Unity supports both using a configuration file (xml)  and code configuration.
In order to use a piece of code like this,

IUnityContainer container;

public Book InstanceOfBook
{
   get
   {
      IBookRepository repository = container.Resolve<IBookRepository>();
      Book b = new Book(repository);
          
      return b;
   }
}

we would first need to tell the container how to “resolve” the IBookRepository. This can be done both through code and also via external configuration.
The code way could look something like this:

IUnityContainer container;
 

public Book InstanceOfBook
{
   get
   {
      container.RegisterInstance<IBookRepository>(new MyBookRepository(), new ContainerControlledLifetimeManager());
      IBookRepository repository = container.Resolve<IBookRepository>();
      Book b = new Book(repository);
          
      return b;
   }
}

The main disadvantage with this kind of approach is that the code needs to know about  MyBookRepository in order to “resolve” it.
Let´s look at how we can accomplish the same thing via external xml configuration to avoid such a limitation.

The Unity.config xml file looks something like this:

<typeAlias alias="singleton"
           type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager,
                 Microsoft.Practices.Unity" />
 

<typeAlias alias="IBookRepository"
           type="JL.Domain.Repositories.IBookRepository,
                 JL.Domain "/>

<typeAlias alias="MyBookRepository"
           type="JL.Infrastructure.Repositories.MyBookRepository,
                 JL.Infrastructure "/>

The first thing that we do is declare a couple of aliases. This is done just because we don´t want to write the full type names later on.
Ok, now lets look at the mapping between the interface and the concrete implementation:

<type type="IBookRepository" mapTo="MyBookRepository">
 
	<lifetime type="singleton" />
</type>
 

Here I´m using the aliases to map together IBookRepository with MyBookRepository. This is the same as the code did before but now I don´t need to have reference to MyBookRepository in order to use it.

There is so much more you can do here, like sending parameters to constructors (constructor injection) and more. But for now we´ll keep it simple.
Ok, so now we need to work on how to get access to that UnityContainer and how to make it use our external config file.
This is how to do that:

private IUnityContainer GetContainer()
{
    var map = new ExeConfigurationFileMap { ExeConfigFilename = path };
    var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
    var section = (UnityConfigurationSection)config.GetSection("unity");
 

    IUnityContainer container = new UnityContainer();
    section.Containers.Default.Configure(container);

    return container;
}

Note: The path variable is holding the path to where the config file is located inside the 12-hive 
Note: I´m not using the web.config file beacuse I think it´s a little messy.

To be continued…

, ,

  1. Leave a comment

Leave a comment