Why I moved from Windsor to Autofac

A long while ago as I started with dependency injection and inversion of control, I started with the container the most people use at this time. Since a read a lot of Ayende I naturally started with Castle Windsor.

Now after a long time of using it, it dose not feel very well anymore. The registration interface is fluent, but looks ugly and it is not very clear what a lot of methods on the configuration interface do. At the end Windsor feels old and ugly (like it is with Monorail compared to MVC).

So to look at the other containers I have to define my requirements:

  • It should be simple
  • It should be extendable
  • It should have a documentation

Now what containers are existing today. Here is a list of the most known ones with some initial thoughts on it.

  • Spring.Net
    • Large and cumbersome port from Java.
    • Looks like a lot of things a never need
  • StructureMap
    • Modern but I don’t like its over fluent interface (I don’t consider fluent interface which trying to build sentences very helpful)
  • Ninjet
    • Small but it saw a lot of attributes on the sample code (don’t like to do all with attributes since CAB)
    • The same about the fluent interfaces like StructureMap
  • Autofac
    • Small and very extendable codebase
    • Interface looks simple and clean

So it seems Autofac is the right to try. And after using it now for some time, I really really like it.

Autofac has a clean and simple core which can be easily extended. And itself makes a lot of use of that. It have nearly all the functions that Windsor and the other containers have, but as extension to the core. Examples are XML configuration, ASP MVC, Proxy and a lot more. Autofac can create components with parameters from its type or position not only from its name like Windsor dose.

So how dose a simple registration looks like in Autofac?

var builder = new ContainerBuilder();

builder.RegisterType<MyComponent>()
    .As<IMyComponent>()
    .SingleInstance();

var container = builder.Build();

container.Resolve<IMyComponent>();

I also like the way you resolve multiple instances:

var assembly = Assembly.GetExecutingAssembly();

builder.RegisterAssemblyTypes(assembly)
    .Where(t => t.Name.EndsWith("Rule"))
    .As<IOrderRule>();

var container = builder.Build();

var rules = container.Resolve<IEnumerable<IOrderRule>>();

Conclusion

At the most it’s a matter of taste yes, but I can only suggest that you try it out yourself.

9 thoughts on “Why I moved from Windsor to Autofac”

  1. Feeling like a move myself. Mostly because I think Castle’s ugliness is due to their inability to listen to their own community. It’s very annoying to request a feature and be directed to an FAQ discussing why they won’t support it. Typically stating that they are protecting you from yourself, rather than implementing a feature that exists in many other containers. I’ve used StructureMap recently and LOVE it. It’s amazing how much simpler and less verbose it is to use, even for some of the more advanced use cases.

  2. having created the first version of windsor i have to say that backward compatibility is the one at fault. It’s hard to keep moving when you have a user base to keep happy. Green field projects can always leverage the new and shinny features, and from what I see that’s what the team is doing by releasing more often and breaking compatibility between major versions.

  3. I think you must have been looking at some old StructureMap documentation by the way, it’s a lot less verbose than the first iterations, most registration takes place via convention anyway – very elegant and not at all sentence based 🙂

  4. @Rob Ashton That’s what I ran into with StructureMap. Tons of outdated documentation.

    Convention based is cool, but it’s not a great option when StructureMap is dropped into an existing system.

  5. I agree, I love Autofac. It’s has a simple, yet powerful API. I like especially that you just can pass a lamdba-expression as a ‘factory’. This is extremely powerful, since you have full control how stuff is created. Also the scope-control of components is super easy.

    I previously worked with a lot of Java-IoC containers like Spring, Guice and Pico-Container. I really wished that those were as elegant and easy to use as Autofac.

  6. There is also LinFu.IoC which is another good lightweight container with it’s own pros and cons.

Comments are closed.