Autofac - My Choice of IoC Container

I have been using Unity for dependency injection since the early days of ASP.NET MVC framework. Recently I have evaluated other IoC containers and really impressed with Autofac. The next version of my codeplex project EFMVC will be use Autofac as IoC container.  Autofac is a lightweight IoC container that providing better performance and can easily integrate with .NET applications. Autofac is developed by Nicholas Blumhardt and the community is actively working on the code base.

Using Autofac in ASP.NET MVC 3 Apps


Autofac provides better integration for ASP.NET MVC Framework.You can add the Autofac ASP.NET MVC3 Integration package using NuGet to working with MVC 3. The following command in the NuGet console will add Autofac and its ASP.NET MVC 3 integration components.

PM> Install-Package Autofac.Mvc3


Register components with Autofac

The following code block in the Application_Start()  of Global.asax.cs will configure Autofac  with ASP.NET MVC 3.

var builder = new ContainerBuilder();          
builder.RegisterControllers(Assembly.GetExecutingAssembly());    
builder.RegisterType<CommandProcessor>().As<ICommandProcessor>();
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest();
builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerHttpRequest();
builder.RegisterAssemblyTypes(typeof(CategoryRepository).Assembly)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces().InstancePerHttpRequest();          
var tasks = Assembly.Load("EFMVC.Tasks");
builder.RegisterAssemblyTypes(tasks)
.AsClosedTypesOf(typeof(ICommandHandler<>)).InstancePerHttpRequest(); 
IContainer container = builder.Build();                  
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

Registering Controllers

The below code register all the controllers in an assembly

builder.RegisterControllers(Assembly.GetExecutingAssembly());    


Register components

The following code will register components with container lifetime of Http Request

builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest();
builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerHttpRequest();


Auto Registration


Autofac will automatically register your components by scanning assembly The following registration syntax will register all types in an assembly where type name is end with "Repository".

builder.RegisterAssemblyTypes(typeof(CategoryRepository).Assembly)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces().InstancePerHttpRequest(); 


The following registration syntax will scan the given assembly and will register all types that implemented the open generic type ICommandHandler<>

var tasks = Assembly.Load("EFMVC.Tasks");
builder.RegisterAssemblyTypes(tasks)
.AsClosedTypesOf(typeof(ICommandHandler<>)).InstancePerHttpRequest(); 

Filter Attribute Property Injection


To make use of property injection in filter attributes, just call the RegisterFilterProvider method provided by Autofac.MVC3

builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterFilterProvider();

 Useful Links

Autofac Home

Nicholas Blumhardt's Blog

Rinat Abdullin's Blog

Alex Meyer's Blog

Improving Performance  

Help from Stackoverflow

 

5 Comments

Comments have been disabled for this content.