Wednesday, February 16, 2011

Unity Application Block Configuration File Setup

Recently I have been working on a C# backend project were we are using the Unity Application Block (Unity) injection features to enable our backend to inject dependency mappings between the Data Abstraction Layer (DAL) and the Business Abstraction Layer (BAL).

"The Unity Application Block (Unity) is a lightweight, extensible dependency injection container with optional support for instance and type interception."

This architecture approach and the use of Unity allow us to completely decouple the BAL from the DAL layer increasing the extensibility and maintainability of the system. We also get the advantage of simplifying object creation when applying the Factory Pattern for dynamic class instantiation/resolution. It also give us greater abstraction and allow different team members to work on different layers reducing the dependencies and increase the flexibility by deferring component configuration to the container.

Configuring Unity is generally straight forward, but your configuration file can become quite big, complex and difficult to manage. What we do to solve this issue, is to separate the unity configuration into a different file, say "unity.config". I wanted to share the tip on how to implement this approach of having multiple configuration files, breaking down the configuration settings and simplifying each of the config files.

First, you need to include the Unity config section in your main configuration file:


And then specify which is the file containing that configuration file:


And voila. Once you have the unity.config file in place everything will work and you will be able to retrieve the Unity configuration settings from the secondary unity.config file.

Now remember that on the unity.config file you need to specify the Unity container. The unity.config file should look something like this:


But since in your application code you will be resolving the container dynamically, you need to be able to retrieve the container configuration. For this, you will need to include the container name in the main configuration file as an application setting:


Then in your code, you can simply retrieve the container name from the configured app settings:

private static readonly string DalContainerName = ConfigurationManager.AppSettings["DALContainer"];

Then to resolve your container when creating the classes on the factory class, you need to declare a unity container and then resolve the container:

private static readonly IUnityContainer Container;
Container = new UnityContainer().LoadConfiguration(DalContainerName);

At this point you have loaded the container specific configuration and dependency mappings, so you can go ahead and resolve the class you need:

        public static T CreateProvider() where T
        {
            return Container.Resolve();
        }

Where "T" will be the interface type (declared in the BAL layer) you need the container to resolve. That will give you the concrete class instance that implements the given interface. Remember that the interface will be declared at the BAL layer while the concrete class implementing the interface will be implemented in the DAL layer.

I hope it helps you out there to get the most of your configuration files, and I recommend keeping an eye on the Unity Application Block for extensible, loosely coupled application development.

Cheers!
-arbbot

No comments:

Post a Comment