4

[Revision 5/14/09: I need this to target Dotnet 2.0 because of my deployment scenario]

I am new to Dependency Injection & IoC.

I see that there are a plethora of containers and libraries to help the process along.

What are the pros and cons of using these libraries. What is your favorite (dotnet) container and why?

What about rolling my own container for Dependency Injection?

Thanks,

Peter Stephens

Peter Stephens
  • 1,040
  • 1
  • 9
  • 23

8 Answers8

3

If you need only Dependency Injection probably AutoFac will be the best option, since it's small, quite simple and logical. It should not be to hard to grok.

Once you get that, and you feel the need for more mature product (that can do other things than simply DI) I'd suggest taking a look at Castle Windsor, which is pretty powerful out of the box, very well designed, and customizable. If you don't like that, I've heard good things about StrcutureMap.

Krzysztof Kozmic
  • 27,267
  • 12
  • 73
  • 115
  • This looks interesting. It also targets Dotnet 2.0, though it requires a 3.5 build environment. I will definitely consider this. – Peter Stephens May 19 '09 at 12:53
  • If you want to target .NET 2.0 in Windsor, there is a tag in the repository that supports .NET 2.0 All newer versions target v3.5sp1 – Krzysztof Kozmic May 19 '09 at 14:13
  • The version targeting .NET 2.0 is almost identical to Windsor 2.0 in terms of features. – Krzysztof Kozmic May 19 '09 at 14:14
  • I haven't made up my mind entirely yet, but I'm leaning towards AutoFac. I might couple it with the Common Service Locater, but I'm not sure yet. I probably need to write some test cases to see how it will all fit together. – Peter Stephens May 20 '09 at 13:01
2

Spring.Net

treehouse
  • 2,521
  • 1
  • 21
  • 39
  • This framework looks interesting. It seems to do a lot more than just IoC and DI. I'm not sure I want to solve more than the IoC problem at this moment though. – Peter Stephens May 18 '09 at 17:50
  • This framwork brings in a bloat of components and external dependencies but this could be a benefit because of its completeness of things you need in larger apps and the components are well choosen. – Beachwalker Jan 27 '12 at 16:00
1

I like MEF, mainly because it's going to be very well supported in .NET 4.0 (since MS has announced that they're using it for the base of DI in Visual Studio 2010).

It's also quite simple to use, especially for DI. Handling DI and Add-In scenarios is the main goal of that framework, so it's very clean and simple in those cases.

It works for IoC as well, but that isn't its primary goal.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 2
    I have heard good things about MEF. I recently listened to a Hanselminutes podcast that gave a good overview: http://www.hanselminutes.com/default.aspx?showID=166 "Managed Extensibility Framework with Glenn Block" – Peter Stephens May 18 '09 at 17:42
  • I've been using it for a while now, and it works great. I particularly like the way it handles catalogs and injection - you get a lot of control if you want it, but it's also very easy to just "make it work". I like the approach they took with it, even over my own DI solutions I've written (http://plugintoolkit.net/), which I've now abandoned in favor of MEF. – Reed Copsey May 18 '09 at 17:45
  • I wouldn't use MEF for dependency injection, since that is not what it's made for. There are better solutions for that, many of which can work with MEF. – Krzysztof Kozmic May 19 '09 at 09:30
  • After thinking about this a little--I would love to use MEF in my app but not for DI. Rather I would use it for composability of my application layers. Unfortunately, my app is Dotnet 2.0 Winforms and is unlikely to change anytime soon, so MEF will not work with it and must be crossed off my list :-( – Peter Stephens May 19 '09 at 12:34
  • Of course, Composability is very similar to Dependency Injection. The line blurs a bit :-) – Peter Stephens May 19 '09 at 12:36
  • @Krzysztof Kozmic: I disagree - MEF is great for depedency injection - in fact, that's really ALL that it does. It handles all three forms (http://en.wikipedia.org/wiki/Dependency_injection), including interface injection, setter injection, and constructor injection. It is not a good general purpose IoC framework, but a great DI framework. – Reed Copsey May 19 '09 at 15:08
  • Yes, MEF is not an IoC Container: http://ayende.com/Blog/archive/2008/09/25/the-managed-extensibility-framework.aspx – Robert Claypool May 26 '09 at 15:10
1

I use an IOC tool called StructureMap : Structure Map I found the tool to be very useful and easy to implement.

CSharpAtl
  • 7,374
  • 8
  • 39
  • 53
1

After some more research: Ninject seems to be a useful alternative. And it supports Dotnet 2.0. And it doesn't require massive XML configuration.

Still trying to figure out Best Practices for having a different DI configuration for the Production and Unit Testing environments.

Peter Stephens
  • 1,040
  • 1
  • 9
  • 23
1

Found another thread on this topic here

Community
  • 1
  • 1
Peter Stephens
  • 1,040
  • 1
  • 9
  • 23
0

Spring.NET. If you only want to use the DI component of Spring, that's all you need to use (and you only need to reference those DLLs). Then later if you want to use the unit testing stuff, or the Quartz stuff, or the NHibernate stuff, you can add those DLLs too - it's built in a modular way. And all the different parts have a consistency about them because they're from the same framework. I think it's a really good framework that complements .NET very nicely.

Richard
  • 1,731
  • 2
  • 23
  • 54
0

You can use Common Service Locator in your code, where it needs to call the container explicitly. This way if you're unhappy with the chosen framework you can switch it by pluging in the new one.

bbmud
  • 2,678
  • 21
  • 15
  • This looks like a possibility. Abstracting the abstraction layer is a nice concept :-) It also appears to support Dotnet 2.0. – Peter Stephens May 19 '09 at 13:18
  • Common Service Locator is a nice concept but the real hard work is not the change of the interface... you have to convert your IoC-config files. Did a switch from Spring.net config to Ninject... this can be pain in the ass. ;-) – Beachwalker Jan 27 '12 at 16:03