Autofac 2.x

A new version of favorite dependency-injection-container Autofac is making progress. It follows the same principals, brings some new features etc. However they also refactored the API, so it isn’t compatible with the older versions.
This means, that migrating to the new version is some work. However there are useful new features.

Lots of them I’ve implemented on top of the old Autofac-version:

  • Collection Support ("Resolve All"): I’ve implemented something like this. Basically it allows you to resolve multiple implementation of a service. Very useful for some simple plugin-mechanisms. (not every application needs MEF)
  • Auto-Generated Factories: Allows you to create multiple instances of an service. The container is responsible for implementing a factory. This feature I’ve also implemented.
  • Owned Instances: Autofac has extremely good support for managing resources. Now you can inject a Owned<T> instance, which creates a new disposable scope. When you call Owed<T>.Dispose(), the service T, with its resources and dependencies are disposed.
    I’ve implemented something similar: All services exposing the IDisposable-interface create a own scope. As soon as you dispose the service the scope with its resources etc is disposed.

Autofac 2.1 already bring support for the .NET 4.0 stuff like Lazy<T> etc.

I keep an eye on Nicholas Blumhardt’s blog and as soon as Autofac is out of beta, I’ll migrate.

Leave a Reply

Your email address will not be published. Required fields are marked *