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

 

Published Thursday, September 22, 2011 4:09 AM by shiju

Comments

# re: Autofac - My Choice of IoC Container

Friday, September 23, 2011 3:25 PM by Josh

How about a little more information on why you prefer it to Unity? Is it feature set, ease of use, reduced complexity, something else?

# Autofac - My Choice of IoC Container | .NET, ASP.NET, ASP.NET MVC | Syngu

Pingback from  Autofac - My Choice of IoC Container | .NET, ASP.NET, ASP.NET MVC | Syngu

# Autofac &#8211; My Choice of IoC Container &#8211; Shiju Varghese&#039;s Blog | ASA Container -For Afghanistan

Pingback from  Autofac &#8211; My Choice of IoC Container &#8211; Shiju Varghese&#039;s Blog | ASA Container -For Afghanistan

# re: Autofac - My Choice of IoC Container

Tuesday, September 27, 2011 1:13 AM by nmarun

Autofac also gives hooks for interceptors: weblogs.asp.net/.../using-autofac-dynamicproxy2-interceptors-with-mvc3-part-1.aspx

Arun

# re: Autofac - My Choice of IoC Container

Tuesday, September 27, 2011 2:43 AM by shiju

@Josh - Autofac is lightweight, feature set, ease of use and better performing IoC container. It also support Auto Registration features  

# re: Autofac - My Choice of IoC Container

Monday, January 02, 2012 5:46 PM by kanepe

what is in registered dll? Can anyone give some sample registered control code. How is it capture special action. ex: productDetail action and inject it?

# re: Autofac - My Choice of IoC Container

Wednesday, January 25, 2012 11:00 AM by CCPony

Autofac's RegisterFilterProviders method generates a NullReferenceException if using Glimpse in your solution.

Leave a Comment

(required) 
(required) 
(optional)
(required)