skip to the main content area of this page
Patterns and Practices

Autofac IoC Tool


If you haven't settled on an IoC Tool or become completely obsessed with the dependency injection tool you use, you may want to look at Autofac, which utiltizes a number of C# 3.0 Features, like Lambda Expressions, Expression Trees, and Extension Methods, to give you an intuitive IoC Tool that is pretty extensible.

 

Open-Source IoC Tool

One of the nice things about Autofac is that it is open-source. You can find it on Google Code: http://code.google.com/p/autofac/

Grab Tortoise, or your favorite tool of choice, and check out the code in the Subversion Repository. Some really nice and well-documented code with a generous amount of C# 3.0 Features being used that will give you some learnings.

 

Awesome Wiki

The Wiki is seriously well-written with plenty of great examples on how to use it in your applications. There is also good advice on how to get the most out of Autofac. Per the wiki, I recommend getting started with the following pages:

 

Intuitive Interface

I am not big into XML these days and appreciate a nice fluent interface for registering components. I dig the intuitiveness of registering components with Autofac.

Use a simple type-mapping registration:

 

var builder = new ContainerBuilder();
builder.Register<Logger>().As<ILogger>();

var container = builder.Build();
ILogger logger = container.Resolve<ILogger>();

 

or take the idea of registration in a different direction and register all types that are assignable to a particular interface:

 

var builder = new ContainerBuilder();
builder.RegisterTypesAssignableTo<IController>();

 

go further and look at how to create Modules for managing registration dependencies.

 

ASP.NET and MVC Integration

As mentioned above, there are some integrations built for ASP.NET and the ASP.NET MVC Framework. Add a couple of HttpModule Declarations in your web.config and a few minor changes to your global.asax code-behind and you have IoC Integration in your web applications.

 

Conclusion

For more information on Autofac, check out Google Code.

 


Tags: Autofac, DependencyInjection, IoC


Topics



Popular Tags



Recent Links