Autofac 2.2 Released

It’s that time again! There are some great features from the original plan still in the pipeline, but the completed work is important to some users so binaries for Autofac 2.2 have been made available on the Autofac site.

What’s new?

Version 2.2 is highly backwards-compatible with 2.1. Along with many small improvements, the significant additions in the new version are the return of a mutable container model, support for ASP.NET MVC 2.0 and lightweight adapters.

ContainerBuilder.Update() and full container mutability

One of the most controversial changes to Autofac in the 2.1 release was the deliberate omission of an easy way to add more components to an already-built container. The reasons for and against this are worth a blog post on their own, but in the end it turns out that integration into third-party frameworks is especially tough without this feature.

To add components to an existing container, use ContainerBuilder.Update():

var container = // something already built

var updater = new ContainerBuilder();
updater.RegisterType<A>();
updater.Register(c => new B()).As<IB>();

// Add the registrations to the container
updater.Update(container);

Configuration this way is often much harder to follow than a simple ‘register then build’ style, so again, use this feature only where necessary.

ASP.NET MVC 2.0 support

The new release is built against the ASP.NET MVC 2 binaries; you’ll have to compile from source if you want to target ASP.NET MVC 1. There can’t be many projects out there that can upgrade to Autofac 2.2, but not to the new release of ASP.NET MVC, so we expect this will be convenient for most users.

Out of the box, Autofac now supports ASP.NET MVC Areas, and integration with other ASP.NET MVC features is getting richer.

The AutofacControllerFactory policies have changed in this release, so if you previously added controllers by hand (as named services) you will need to update your code to register controllers by their concrete type (without an As() or Named() configuration clause..)

Lightweight adapters

Lightweight adapters are an easy way of expressing “for every registered X, provide an adapter of type Y.”
adapters

The introductory article is the best place to find out how this feature works.

Other changes

  • COM interfaces using No-PIA (or ‘ embedded interop types‘) can now be exposed as services
  • Simplified WCF configuration for self-hosted services
  • Scoped registration improvements: SingleInstance() now behaves as expected and binds the instance’s lifetime to the nested scope
  • Generic registration constraint checking improvements – more complex type constraints can now be handled when determining the suitability of an open generic type to satisfy a closed generic service
  • Additional Resolve()/TryResolve() convenience overloads have been added to fill some gaps
  • The same type can now be registered multiple times in a single XML configuration block
  • Several usability enhancements have been made to the scanning feature

Credits

This release, as with every Autofac release, is the product of the lively (and growing) community. There have been some especially fine patches and contributions this time around – thank you everyone for your hard work!

  • Pingback: Simplifying ‘Configuration by Exception’ : Nicholas Blumhardt()

  • Pingback: Dew Drop – May 29, 2010 | Alvin Ashcraft's Morning Dew()

  • Tom

    I just wanted to send a word of encouragement, since after a somewhat long and ardous test drive of a bunch of IoC frameworks we finally tried Autofac and it is brilliant. I wonder actually why would people use any other. Autofac is exceptionally simple yet powerful. I will certainly recommend it.

  • Thanks Tom! I’m glad it worked out well for you.

  • Is there a tutorial for the new version of Autofac? The CodeProject one (Dependency Injection in C# 3.0 with Autofac) seems to have a bunch of examples that no longer work.

    I am looking for a lightweight and VERY fast DI framework, because I would like to be able to create large numbers of small objects, or resolve the same object 10000s of times in different places… Ninject 2 claims to be fast, but I found it could only create 36,000 trivial objects per second on my machine, about 3,000 times slower than an ordinary “globalVariable = new TrivialObject()” statement.

  • As a DI expert, would you mind commenting on the problem of “pervasive services”? I have been thinking about how to reduce boilerplate in case there are several low-level services used by dozens or hundreds of higher-level services. I put an idea in my blog, http://loyc-etc.blogspot.com/2010/08/pervasive-services-and-di.html

  • Brian

    Nick – thanks SO much for all you do with Autofac – it is working very well for us. I just downloaded Autofac-2.2.4.900-NET40 and noticed there doesn’t seme to be a companion AutofacContrib-2.2.4.900-NET40 – there is a AutofacContrib-2.2.4.900-NET35 which says ‘NET3.5+’ on the download page. Is that what I need?

  • Hi Brian,

    Yes – the 3.5+ version should be what you need.

    Cheers,
    Nick