Dependency Injection Frameworks


“The Unity Container (Unity) is a lightweight, extensible dependency injection container. It facilitates building loosely coupled applications”

This was very simple to setup for an ASP.NET MVC project (net461) using the nuget packages:

  •  Unity.Container, Version=5.7.0
  •  Unity.Abstractions, Version=3.3.0
  •  Unity.Mvc, Version=5.0.13

Implementation is as simple as this:

~ /App_Start/UnityConfig.cs
public static void RegisterComponents()
     var container = new UnityContainer();
     container.RegisterType<IClientRepository, ClientRepository>();
     DependencyResolver.SetResolver(new UnityDependencyResolver(container));


Constructor signature can then include `IClientRepository clientRepository`


Uses an IOC (Inversion of control) container. This was useful in a legacy Web Forms application (Autofac, Version= however it would have worked just as well in the MVC application I used Unity in.

~ /Modules/ApplicationModule.cs
protected override void Load(ContainerBuilder builder)
   builder.Register(db => new NpgsqlConnectionFactory(ConnectionName)).As<IDatabaseConnectionFactory>();
   //default ctr
   builder.Register(cm => new ClientRepository()).As<IClientRepository>();

   //ctr with connection param
   .WithParameter(ConnectionName, new NpgsqlConnectionFactory(ConnectionName));
~ Build
var container = builder.Build();

~ Resolve
var clientRepository = container.Resolve<IClientRepository>();


  • Structure Map
  • Windsor

Leave a Reply