I was going to refactor my ASP.NET MVC application and inject some IoC. Last time I was using IoC, Unity was all the rage, but I hated it. It was difficult to setup and had very nondescript errors.
Does anyone have any suggestion or preferences?
I was going to refactor my ASP.NET MVC application and inject some IoC. Last time I was using IoC, Unity was all the rage, but I hated it. It was difficult to setup and had very nondescript errors.
Does anyone have any suggestion or preferences?
Personally, I really like Ninject.
It's simple, yet powerful and allows you to achieve the "easy" parts of DI easily, whilst still giving plenty of scope for more advanced DI scenarios.
It's also fairly lightweight and easy to set-up within your project/application.
This is especially true for ASP.NET MVC applications if you're using NuGet, as there is a Nuget package for the Ninject assembly itself (Ninject), along with another Nuget package which tightly integrates Ninject with ASP.NET MVC 3 (Ninject.MVC3) (which uses such things as WebActivator to allow assemblies to execute start-up code etc.)
There's a few blog posts out there to help get started with all this, too.
You don't 'inject' IoC into anything. Inversion of Control, or more specifically Dependency Injection, is a technique where you inject services into consumers - preferably via Constructor Injection.
In ASP.NET, all you need in order to do that is a custom IControllerFactory. You can, however, implement that IControllerFactory with a DI Container to make things a little easier. Here's a list of various DI Containers for .NET.
I'm surprised nobody mentioned Spring.NET ? It's been available for a long time now, and it has the advantage of being based on the Java Spring Framework (based on the IOC "idea", and separation of packages/namespaces, there is no Java in Spring.NET) as for its maturity and ease of use. Also, if you happen to use both languages in your professional environment, it's great to have some common tools/API's between both.
It integrates pretty well with NHibernate, provides AOP programming tools and you can easily make it work with ASP.NET MVC 2/3.
As for Unity, I've never used it so I can't say.
I may just add that there also is the LinFu Framework, which is a bit more lightweight than Spring. There is a great tutorial on how to integrate LinFu with ASP.NET MVC courtesy from Thomas Weller. I also used LinFu for one of my personal projects and it was really easy to work with.