Which .NET Dependency Injection frameworks are worth looking into?
edit (not by the author): There is a comprehensive list of IoC frameworks available at https://github.com/quozd/awesome-dotnet/blob/master/README.md#ioc:
- Castle Windsor - Castle Windsor is best of breed, mature Inversion of Control container available for .NET and Silverlight
- Unity - Lightweight extensible dependency injection container with support for constructor, property, and method call injection
- Autofac - An addictive .NET IoC container
- DryIoc - Simple, fast all fully featured IoC container.
- Ninject - The ninja of .NET dependency injectors
- Spring.Net - Spring.NET is an open source application framework that makes building enterprise .NET applications easier
- Lamar - A fast IoC container heavily optimized for usage within ASP.NET Core and other .NET server side applications.
- LightInject - A ultra lightweight IoC container
- Simple Injector - Simple Injector is an easy-to-use Dependency Injection (DI) library for .NET 4+ that supports Silverlight 4+, Windows Phone 8, Windows 8 including Universal apps and Mono.
- Microsoft.Extensions.DependencyInjection - The default IoC container for ASP.NET Core applications.
- Scrutor - Assembly scanning extensions for Microsoft.Extensions.DependencyInjection.
- VS MEF - Managed Extensibility Framework (MEF) implementation used by Visual Studio.
- TinyIoC - An easy to use, hassle free, Inversion of Control Container for small projects, libraries and beginners alike.
- Stashbox - A lightweight, fast and portable dependency injection framework for .NET based solutions.
Original answer follows.
I suppose I might be being a bit picky here but it's important to note that DI (Dependency Injection) is a programming pattern and is facilitated by, but does not require, an IoC (Inversion of Control) framework. IoC frameworks just make DI much easier and they provide a host of other benefits over and above DI.
That being said, I'm sure that's what you were asking. About IoC Frameworks; I used to use Spring.Net and CastleWindsor a lot, but the real pain in the behind was all that pesky XML config you had to write! They're pretty much all moving this way now, so I have been using StructureMap for the last year or so, and since it has moved to a fluent config using strongly typed generics and a registry, my pain barrier in using IoC has dropped to below zero! I get an absolute kick out of knowing now that my IoC config is checked at compile-time (for the most part) and I have had nothing but joy with StructureMap and its speed. I won't say that the others were slow at runtime, but they were more difficult for me to setup and frustration often won the day.
Update
I've been using Ninject on my latest project and it has been an absolute pleasure to use. Words fail me a bit here, but (as we say in the UK) this framework is 'the Dogs'. I would highly recommend it for any green fields projects where you want to be up and running quickly. I got all I needed from a fantastic set of Ninject screencasts by Justin Etheredge. I can't see that retro-fitting Ninject into existing code being a problem at all, but then the same could be said of StructureMap in my experience. It'll be a tough choice going forward between those two, but I'd rather have competition than stagnation and there's a decent amount of healthy competition out there.
Other IoC screencasts can also be found here on Dimecasts.
What should I consider when choosing a dependency injection framework for .NET
FYI, just this morning I came across an interesting comparison between all the .NET IoC containers here:
http://elegantcode.com/2009/01/07/ioc-libraries-compared/
A handful of questions:
- How much mainstream support do you need? Spring is probably the biggest one out there. Everyone has used it or has heard of it by now, so lots of info. It also probably has the largest number of features, but that means there's just more to learn. A smaller container like Autofac might be nice, but you might come across an issue you won't find help on.
- Are you handy with XML configuration? Every IoC container relies on configuration and setup of some sort. Spring and Unity are XML heavy.
- Is this a permanent choice? If you are at one of those places where you only get one shot at a choice, it won't matter. But, if you ever want to choose another solution down the road, you probably don't want an IoC that requires you to attribute your classes (sorta the inverse of the above question) because you'll hate yourself when you have to rip all that stuff out. In comparison, wrapping out some XML config might not be as painful.
- What's your shop like? I had trouble pitching a couple of open source options just because of the "gasp! it's not Microsoft!" reactions. If you're a straight MS shop, using Unity will be a much easier cultural win.
On a personal note:
I've used StructureMap for the same reasons mentioned in the blog I linked. I think XML config is a giant pain to maintain and, especially, debug (see WCF). I haven't tried Ninject yet, but based on their marketing, it must be super rad!
How do the major C# DI/IoC frameworks compare?
While a comprehensive answer to this question takes up hundreds of pages of my book, here's a quick comparison chart that I'm still working on:
Which Dependency Injection Tool Should I Use?
Sticking to one container is not really important, if your system has been designed with the IoC/DI in mind. With the proper approach you can easily change the IoC library down the road.
And, of course, the container has to provide enough flexibility to support common widely used scenarios (lifecycle management, proper container nesting, XML and code configuration, interception, fast resolution).
I'd recommend to pick between Castle (widely used and have a lot of integration libraries) and Autofac (lightweight, fast and has proper container nesting, but is not that widely used)
There is a comprehensive list of IoC containers by Hanselman
PS: You do not want to use Unity
Which Dependency Injection technique should I use in dotnet?
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.
Dependency injection frameworks for .Net
in our project also ASP.NET MVC 2 we using Ninject it is really nice and has a pretty cool name :)
How to choose a DI container?
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?
Related Topics
Deserializing Json to .Net Object Using Newtonsoft (Or Linq to Json Maybe)
How to Check If a Given String Is a Legal/Valid File Name Under Windows
Color Different Parts of a Richtextbox String
Show/Hide the Console Window of a C# Console Application
.Net Global Exception Handler in Console Application
Replacing .Net Webbrowser Control With a Better Browser, Like Chrome
How to Load Image to Wpf in Runtime
Make a Borderless Form Movable
Creating a Datetime in a Specific Time Zone in C#
Given a Filesystem Path, Is There a Shorter Way to Extract the Filename Without Its Extension
How to Get the Cpu Usage in C#