How to choose a DI container?

Possible Duplicate:
How do the major C# DI/IoC frameworks compare?

There being so many DI containers, I feel kind of lost. I'm new to the DI pattern.

I'm reading the book Dependency Injection in .NET and I've found DI to be incredible useful in improving a code base, making it loose coupled and more testable.

I now want to introduce a DI container for my dummy project, but there are just so many to choose from.

How am I supposed to choose between Castle Windsor, Unity, StructureMap, Spring.NET, Autofac, Ninject, Funq, LinFu, etc, etc?

I guess a coherent take would be to "just pick one" and start using it (since I figure they're pretty easily interchangeable, specially in the early stages), but I would like to make a more informed decision.


This is like buying a car. You might like a Toyota, but it's just 2.5L engine. You might like Ferrari, but it's too red. You might like Mazda, but your boss doesn't allow you to drive it. You might like Hummer, but then your colleagues would laugh at you. Mix the manufacturers to your taste, there's always going to be something missing for somebody or at some different moment.

My take is - first and foremost, DI is usually better then not having DI. Pick anything and you'll be better off. I'd pick something that:

  • Has good support in community (so you can get answers)
  • Has a good backing company behind it (so you don't get to rewrite your code when it goes bust)
  • Feels good to you (so you don't swear in front of the kids, not cool)
  • Is not an overkill for the project
  • Is not just DI, but offers an ecosystem of things that will reduce the time you spend on tasks that you know you can do, just not right now - and then you can focus on things that matter
  • Is used by a lot of people (so you know that many parts are also tested in real life and bugs filled)
  • Isn't 5 years old (such as that documentation says it is supported on Windows 98 or something)
  • My 2 cents - http://www.springframework.net/. I mean, their documentation contents page is like 20 pages long...

    Or you just might want to look at some more answers to a similar question:

  • Which .NET Dependency Injection frameworks are worth looking into?

  • You can start with the builtin DependencyResolver in MVC3. Later you can easily upgrade to Enterprise Library Unity DI.

    Brad Wilson had mentioned a series of posts on How to use DI in MVC3.


    My stance is, look at a few - you got the excellent "Dependency Injection in .NET" book already (to that list I would add Ninject, which unfortunately is not covered in the book) - and pick one of them that you understand the best and like the syntax. To get started with advanced features are not really important, just that you do start .

    Once you have a IoC container, replacing it should be mostly trivial since all your changes will be in one spot - the aggregate root - and not spread out all over your code base. Using an IoC container will also force you to design for dependency injection if you have not already, that will be the much bigger impact on your projects.

    链接地址: http://www.djcxy.com/p/82134.html

    上一篇: ASP.NET MVC +流利的nNibernate,什么是IoC工具?

    下一篇: 如何选择DI容器?