Silverlight Cookbook: Switching to another IoC Framework
The Rationale
As long as I have been using the Dependency Inversion Principle, Microsoft Unity has always been my preferred Inversion-of-Control framework. Consequently, the Silverlight Cookbook has been using Unity 2 in both its domain and query services as well as within the Silverlight client. I never even bothered looking at other frameworks with the exception of NInject, which I used in a Windows Mobile 5 project, and StructureMap, which I learned a lot about while reading Jeremy D. Miller’s posts.
Until I read this post where Philip Mateescu compared the performance of the most popular IoC framework and declared Autofac as the clear winner.
Let’s be honest though. I’ve always liked Unity and managed to solve all my IoC and AOP problems with it without too much hassle. However, while analyzing some performance issues in my latest project revealed that its AOP features were not the fastest available. Nevertheless, I would not simply change my strategy based on a single post. In fact, some claim that the comparison was faulty in the first place. But after browsing through the Autofac documentation I really became quite fond of what Autofac does. I’ve always abided to the Microsoft, unless philosophy, but my engineering heart couldn’t resist the temptation to introduce Autofac into the Silverlight Cookbook. So what are the advantages?
Separation between configuration and resolution.
To be more precise, you use a ContainerBuilder to setup the dependencies like this:

And use its Build() method to construct an IContainer that you cannot change anymore (well, you can, but you shouldn’t). Notice the fluent interface, one of the aspects of Autofac I’m particularly fond of.

This separation is actually the biggest reason why it took me so much time to migrate the Cookbook. You simply have to organize your setup code so that all registrations happen at the same place. But by doing so, I’ve found that my design became more clean with better separation of concerns. So, unlike what you might expect, I see this as an advantage rather than a disadvantage. But beware of this when you consider migrating from Unity to Autofac.
Implicit support for factory methods
Check out the updated version of the AddNewRecipeHandler:

So instead of introducing a dedicated factory interface, you can simply add a dependency to a Func<T> where T is your actual dependency. Autofac