3

I am going to use it in a project with less-experienced developers so a complex framework such as Spring.NET is not an option. I was thinking about:

  1. Ninject
  2. Castle Windsor
  3. StructureMap

Which would present a moderate learning curve without losing flexibility?

and another question - where is the correct place to put the configuration? Since the kernel/configuration on a 3-tier ASP.NET application (not MVC!!! all examples are using MVC :) )

1
  • They all have a learning curve, but the principles are transferable. I say pick one, learn how it works, write a simple test application to get a feel for it. Then look at the others and see if they alleviate any pain points. Mar 5, 2010 at 14:58

11 Answers 11

4

The great thing about proper use of DI is that you can defer the decision about which DI Container to use until the last responsible moment. In application architecture terms, this corresponds to a so-called Composition Root, which is where you wire all depedendencies together. This is often the application's entry point.

Apart from the Composition Root, the entire application can be written without referencing any particular DI Container at all. You just need to follow well-known patterns and principles.

When it comes to pick a DI Container, I'm aware of these DI Containers for .NET:

Personally, I have been happy with Castle Windsor so far, but I have yet to gain extensive experience with all of these containers.

1
  • This is not entirely true. Various containers offer varying capabilities, and approach different problems from different angle. You can take advantage of powerful capabilities of container you choose (like startability, event wiring, typed factories etc) without referencing it, but by designing your components in a certain way. You can often adapt other container to work with your components, but it means you'd need to write some additional glue code to make up for its deficiencies/differences. Feb 26, 2010 at 9:03
2

Here's an ASP.NET (not MVC) Ninject sample:

http://davidhayden.com/blog/dave/archive/2008/06/20/NinjectDependencyInjectionASPNETWebPagesSample.aspx

0

I have used ninject and found it easy to bring new developers up to speed with it.

0

Consider to start with wiring by hand: see http://blog.objectmentor.com/articles/2010/01/17/dependency-injection-inversion . It gives less-experienced developers a better insight in the basics of IOC/DI.

0

I think you are being too hasty to reject spring.net. Spring offers an extremely flexible learning curve, so at the beginning it's kind of "you take what you want from it" approach.
You can start with the simplest-of-all IoC container, and later on move to aop, transactions, unit testing, or whatever you desire, so the complexity builds up gradually.

This was the #1 selling point at my last two jobs for using Spring. Additional points were:

  • It doesn't force you to use its api or to bend your architecture. Again, this leads you to adapt its features at your own pace.
  • Very extensive documentation.

When the project matures, so will your developers, so spring will come out handy at the end ... (at no cost in the beginning, in my opinion)

0

Autofac is my container of choice. It allows registration via lambda expressions for maximum flexibility (the link has some nice examples).

It also has a Silverlight-compatible version. I'm unsure if the other containers do.

As for placement, the container should be built during application startup. For an ASP.NET application, this would be in the Global.Application_Start method.

Autofac has ASP.NET integration which injects pages instances using the container you build during startup.

1
  • 1
    Castle Windsor has a Silverlight 3 version Feb 4, 2010 at 3:59
0

Have a look at http://funq.codeplex.com/ first of all it's extremely fast compared to windsor and other reflection based containers second of all it's very flexible but still has a very readable configuration (If you've gotten your head around Lamba expressions which should take to long any ways)

4
  • 2
    it provides close to no out of the box functionality. It's a scaffolding on which you need to write your entire wiring up manually. Feb 26, 2010 at 9:04
  • I agree with Krzysztof: For instance, Funq doesn't support automatic constructor injection (auto-wiring), which makes even the simplest registration cumbersome and brittle. You'll end up changing your DI configuration after the simplest little change.
    – Steven
    May 4, 2011 at 7:24
  • @Steven Yes nothing is done for you (and the project is now laid dormant) I however disagree with the brittle part. It's type safe which as far as I remember it was the only one to accomplish when I wrote my answer. If there's no need to be able to change the configuration runtime I believe the configuration should be type safe. I'd rather have a compile error than a runtime error (which would be the case with windsor if you're not using autowiring or have forgotten to define the needed concrete classes)
    – Rune FS
    May 4, 2011 at 12:35
  • Most containers allow you to register delegates, so it's not a feature exclusive to Funq. I understand you love compile-time checking: I do too. With delegates however, it's still as easy as with auto-wiring to create a configuration that compiles but fails at runtime (you just have to forget to register a single dependency). Because of that, I can't see the lack of auto-wiring as a strength. Much more useful would be a feature that allows you to verify the container, once you're done configuring. For instance, Simple Injector contains a Verify() method that does exactly this.
    – Steven
    May 4, 2011 at 13:13
0

We use the managed extensibility framework. It is part of the .NET 4.0 framework (currently in release candidate), where you can find it in the System.ComponentModel.Composition namespace. The codeplex site is currently still the best source of documentation.

The focus of MEF is more on "extensibility" rather than "dependency injection", but it uses dependency injection to achieve this. For example, the visual studio 2010 code editor uses MEF to enable extensibility.

We use it as a DI framework and have not yet run into any problems (though I did need the dynamic instantiation sample which is not part of the core yet).

0

If you're yet familiar with any DI framework, I'd go with the Simple Injector.

It has a very simple API, that will get you started very quickly. Although simple, it still enables many advanced configuration scenario's. The library designed with migration in mind, which means that switching to another framework would is rather straightforward. To prove this point, there is an migration guide on the project's wiki.

0

In addition to Ninject, which is a great product, there are also two other options that I'm familiar with:

  1. Microsoft's Unity. Some Alt.NET folks think it's a little on the big & complicated side. I've been using it for several months now as part of Prism (Prism is NOT required to use Unity. It's available stand-alone) and I've found it to be simple and straight-forward.
  2. StructureMap. I've found StructureMap to also have a very gentle learning curve with fast ramp up.

Hope this helps.

-2

I find StructureMap very usefull and intuitive to use. I played with Ninject a bit and found that less convenient, but YMMV. I would also recommend you use the common service locator as an intermediary between the actual implementation of your IOC and your application. When you want to switch your IOC/DI container later on, this is less painfull. There are adapters for StructureMap and Castle windsor. And I think from googling that there also is an adapter for Ninject 2 according to this blog.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.