[Dev Tip] How To Use Unity Container In ASP.NET


Building The Container
The first thing to do is to build the Unity container.
We would like to have a persistent container that hold it’s state
during the running of the application. The right place to put the
is as part of the Global.asax file as a property of the current
running application.
First build an interface for the container property:

public interface IContainerAccessor
{
IUnityContainer Container { get; }
}

After the building of the interface implement it in the Global
class in the Global.asax file:

public class Global : HttpApplication, IContainerAccessor
{
private static IUnityContainer _container;
public static IUnityContainer Container
{
get
{
return _container;
}
set
{
_container = value;
}
}

IUnityContainer IContainerAccessor.Container
{
get
{
return Container;
}
}

protected void Application_Start(object sender, EventArgs e)
{
BuildContainer();
}

protected void Application_End(object sender, EventArgs e)
{
CleanUp();
}

private static void BuildContainer()
{
IUnityContainer container = new UnityContainer();
// Register the relevant types for the
// container here through classes or configuration
// register the container in the container property
Container = container;
}

private static void CleanUp()
{
if (Container != null)
{
Container.Dispose();
}
}
}

In the implementation you hold a static container variable which
will be available in the web application through the interface we build.
In the BuildContainer method we do all the work of registering types
and instances to the Unity container. It can be done by code or
by configuration this decision is yours.

How To Inject Dependencies
After you set up your container it’s time to use it. It is done
by building a base page for all the pages in your application.
The injection of dependencies should be done very early in the
page life cycle and therefore implemented in the OnPreInit event.
The base page class can look like:

public abstract class BasePage<T> : Page where T : class
{
protected override void  OnPreInit(EventArgs e)
{
InjectDependencies();
base.OnPreInit(e);
}

protected virtual void InjectDependencies()
{
var context = HttpContext.Current;
if (context == null)
{
return;
}

var accessor = context.ApplicationInstance as IContainerAccessor;
if (accessor == null)
{
return;
}

var container = accessor.Container;
if (container == null)
{
throw new InvalidOperationException(“No Unity container found”);
}
container.BuildUp(this as T);
}
}

All the checks in the InjectDependencies method are
a guarantee that we have a Unity container. The only interesting
part here is the BuildUp method in the end of the method.
The BuildUp method will ensure that the injection will happen as
required.

Example Of Concrete Page
After building the base page the only thing to do next is to
implement the other pages and inheriting from the base page.
The next example shows how to use the previous BasePage class:

public partial class _Default : BasePage<_Default>
{
[Dependency]
private ILogger Logger { set; get; }

protected void Page_Load(object sender, EventArgs e)
{
Logger.Log(“Something”);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s