Dependency Injection Frameworks

Unity

“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 (Unity.Container, Version=5.7.0.0)

/App_Start/UnityConfig.cs

public static void RegisterComponents()
{
     var container = new UnityContainer();
     container.RegisterType<IClientRepository, ClientRepository>();
     DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}

Autofac

This was useful in a legacy Web Forms application (Autofac, Version=4.8.1.0) 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
   builder.RegisterType<ClientRepository>()
   .As<IClientRepository>()
   .WithParameter(ConnectionName, new NpgsqlConnectionFactory(ConnectionName));
}

Others

These 3 are listed inĀ Professional Test Driven Development with C#: Developing Real World Applications with TDD

  • Structure Map
  • Windsor

Leave a Reply