Small DI-Containers and Autofac

There are tons of dependency injection (DI) containers around in the Java and the .NET world. The are the well know, big ones like Spring (Java, .NET), Castle Windsor etc. Most of them are quite powerful. However, they aren’t small, are heavily based on XML-Configurations and they have their complexity.

So, on small projects I prefer a small container which does not require any XML-Configuration. The small containers usually are configured with plain old code. Having no XML reduces the possible errors like typos, because its checked by the compiler.

So on the Java-world I quite like Guice. On the .Net side I’ve found Autofac. Autofac is a dependency injection container at its core and around 150 KiByte in size. So don’t expect a huge feature list. Nevertheless this small library is nicely designed, so its a pleasure to work with. It supports the usual things like auto wiring components, creation of automatic factories, collections of components, open generic types as components etc.

One cool feature of Autofac is its support for the disposal of components (IDisposable). You create an inner-container instance, use components, then you dispose this sub-container. All used components will be disposed with it. You don’t need to propagate the IDisposable.Dispose() through your whole dependency-graph.

Also its easy to build extensions on top of Autofac. For example Autofac doesn’t support interceptors. So I’ve integrated a simple interceptor-facility in a few hour. Also there is already a integration library for DynamicProxy2.

For small a project, Autofac could be a good choice.

Leave a Reply

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