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.

mrydengren
  • 4,270
  • 2
  • 32
  • 33
Adi Sembiring
  • 5,798
  • 12
  • 58
  • 70
  • 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 MVC – Allen Wang May 26 '11 at 15:26
  • 1
    Recommended by who? There is no officially recommended DI framework and everyone has their favorite. – hwiechers May 26 '11 at 18:21

5 Answers5

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.

mrydengren
  • 4,270
  • 2
  • 32
  • 33
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/

BonyT
  • 10,750
  • 5
  • 31
  • 52
3

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

Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
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.

Marijn
  • 10,367
  • 5
  • 59
  • 80
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.

bbaia
  • 1,193
  • 7
  • 8