What is the recommended Dependency Injection framework for ASP.NET MVC
Asked Answered
K

5

9

I've been looking for good dependency injection for ASP.NET MVC. recently I am Java EE programmer, I always used Spring for DI Framework. But after searching some tutorial from internet. Spring.NET requires explicit XML configuration for each controller that need to be injected.

<spring>
    <context>
        <resource uri="config://spring/objects"/>
    </context>
    <objects xmlns="http://www.springframework.net">

        <object id="HomeController" type="SpringTestApplication.Controllers.HomeController, SpringTestApplication" singleton="false">
            <property name="TestProperty" value="This has been injected via Spring.Net"/>
        </object>

    </objects>   
</spring>

And I think its very complected because there is no autowiring feature like Spring in Java World. Can you suggest me the recommended DI Framework for ASP.NET MVC ?

Thanks in advance.

Kirkuk answered 26/5, 2011 at 15:18 Comment(2)
I am using Unity for my current MVC 3 project. It works very well. Autofac also works fine with my previous project. Both DI framework are straight to work with ASP.NET MVCWeighin
Recommended by who? There is no officially recommended DI framework and everyone has their favorite.Greg
P
7

I wouldn't say there is a de facto recommended DI framework for the ASP.NET MVC framework. However, there's quite a few popular DI framework in the .NET world: Castle Windsor, Autofac, StructureMap, Ninject, Unity, Sprint.NET etc.

I would definitely recommend Ninject. The source can be found on github. There is a Ninject extension for the various versions of the ASP.NET MVC framework. The project also contains a sample application.

If you would like to have a look at an application using Ninject in the wild I would recommend the blog engine Subtext.

May I also suggest that you have a look at Mark Seemann's book: Dependency Injection in .NET. It covers some of the major frameworks for .NET. Unfortunately, it does not cover Ninject.

Pul answered 26/5, 2011 at 16:12 Comment(0)
D
4

Castle Windsor, Ninject, Autofac are all good candidates

Personally I use Castle now.

For good example of how to set things up see http://wiki.sharparchitecture.net/

Damask answered 26/5, 2011 at 15:21 Comment(0)
S
3

I've used both Unity and Castle Windsor with MVC. Both are good, both have either XML or fluent API configuration.

Sister answered 26/5, 2011 at 15:24 Comment(0)
M
2

I don't think there is a "preferred" framework. As the other answers show, you have multiple options, with spring.net being one of them.

With regard to spring.net:

  • there is a CodeConfig project that you might like
  • Visual Studio support for working with xml configuration has improved somewhat with the vs.net add-in, ReSharper helps too
  • asp.net mvc 2 is supported out-of-the-box with version 1.3.1 (current version)
  • asp.net mvc 3 will be supported out-of-the-box with version 1.3.2, which is expected soon

You might want to take the above in consideration if you want to leverage your Spring Java skills by using spring.net.

Macey answered 26/5, 2011 at 19:8 Comment(0)
N
1

You can use the CodeConfig approach like in the Java world :

http://springframework.net/#spring-code-config-1.0.1-released

A MVC sample is provided.

Nad answered 26/5, 2011 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.