A few weeks ago I wrote an article about using IoC with BizTalk and Castle Windsor.  A few of the guys who I trust know their stuff really well told me that they were using Autofac which is another of the many IoC frameworks out there and recommended that it was pretty cool.

I wanted to learn a little more about it and to see if it would work with my original sample.

In short the simple answer was yes it does.  I was able to use the same approach of using a rule to tell me which assemblies to inspect for installers (which in Autofac are very similar but implement a different interface) and then I could use my same wrappers around the container to make it nice and easy to play with.

I needed to make a couple of small changes around the internal building of the container to suit the way that Autofac works compared to Castle but there wasnt much to change at all and I quite like what it does and seems to be documented really well.  The most noticable difference was for transient objects the use of a LifeTimeScope which allows you to more explicitly clean up objects.  This is something I like because it is a more obvious way to trust that the developers in my team would get used to cleaning up those objects where as with Castle there is some little twists around how object disposal is managed in different scenarios.  This is discussed well in this article:

http://tommarien.github.io/blog/2012/04/21/castle-windsor-avoid-memory-leaks-by-learning-the-underlying-mechanics/

You can see below how a using statement can be used to manage this with Autofac which is pretty cool

pic

Code Sample

If you want to check out the autofac version of the demo its on the below link:

http://cscblogsamples.blob.core.windows.net/publicblogsamples/IOC%20with%20Autofac.zip

 

Buy Me A Coffee