views:

189

answers:

3

I've been using Castle Windsor in my previous project and I liked it a lot. For my current project I'm looking to use a different IoC container. Castle Windsor hasn't had any new releases since 2007 and is still not at version 1.0 so it is hard to justify using it in a commercial environment.

One of the things I like about Castle Windsor is that you can have the container call an Initialize method on your services after all dependencies have been set simply by making the service implement IInitializable. I used this a lot. It makes it easy to do property injection instead of constructor injection and that cleans up code and tests quite a bit.

I've been looking at StructureMap, AutoFac, Unity and Spring.Net as alternatives but of these only Spring.Net supports something similar, it automatically calls an Init() method. Unfortunately Spring.Net does not really support the way I want to work with an IoC container (it injects based on string keys instead of interface declarations and therefore its autowiring support is limited too)

Did I miss a similar feature in the IoC containers I looked at? Is my way of working with IoC containers wrong somehow? Or are there other IoC containers that do support something like IInitializable or Init()?

Thanks in advance.

+3  A: 

Autofac can do it - they call it Startable

Rashack
Thanks, I'll try it out, missed it in the documentation somehow. +1
Mendelt
+1  A: 

With StructureMap, you could do something like this:

ForRequestedType<IFoo>()
  .TheDefaultIsConcreteType<Foo>()
  .OnCreation(x => x.Init());

It's not as easy as implementing an 'Initialisation' interface on your class, but it also means you don't need to tie your class implementation to your choice of DI container by inheriting from a DI container specific interface (although I'm not sure how much of an issue that is in reality).

I believe that constructor injection is far more commonly used right now, and property injection is widely seen as a fallback for cases where it is not feasible to get the DI Container to perform object construction for you (e.g. ASP.NET webforms). I could be wrong there though, that's just my view on the subject!

Do you really think that property injection "cleans up code and tests quite a bit"? That's interesting because I sort of think the opposite - I think constructor injection is 'cleaner', and I'm guessing that could be simply because that's how I normally do it so that's what I'm used to. :)

Steve Willcock
Thanks. Great answer. +1 The main advantage I get from property injection is that I don't need to rewrite existing code and tests whenever I add a dependency to a class. Also having all dependencies in one place instead of having them mixed up in the constructor makes my code more readable. But I can imagine this is a matter of taste :-)
Mendelt
+1  A: 

Castle may not have had any release in some time, but it's still actively developed. You can get latest (pretty stable) builds here.

There also is going to be an official v2.0 release quite soon. Why not use what you already know, if you know that it's good?

Krzysztof Koźmic
I still use it myself but many commercial settings is pretty hard though. Using OSS libraries can be challenging with some clients, using OSS libraries that never had an official 1.0 release and is still only available as release candidate 3 is even harder that's why I'm looking for an alternative. Thanks for telling me about the upcoming 2.0 release, that might change things.
Mendelt