What is the recommended Dependency Injection framework for ASP.NET MVC - asp.net-mvc

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.

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.

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/

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

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.

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.

Related

Dependency Injection in .net 4.7?

I'm a little confused as to what integrated options I have for DI. I see it's pretty straightforward for .net core (for my particular projects), but I don't need to build a cross platform app and don't see the advantage to using core. However, it doesn't look like .net framework applications are still setup with Global.asax and without Startup.cs so does that mean there is no integrated DI option for .net framework 4.7? Do I still need to get a 3rd party solution or is there a way to use the same DI workflow in a .net framework project as is used in a core project?
Dependency Injection is not integrated by default in classic asp.net, you need to add a nuget package to handle DI (only integrated by default in asp.net core).
EDIT: Even though I found out how to do it as explained below, I still ended up going with Autofac because I didn't realize the Microsoft's solution only supports constructor injection, but not property injection.
I found instructions on how to do it here. I know link answers are bad, but I don't have time to do any more than this. If someone else wants to make an answer with full instructions I will mark it.
https://scottdorman.blog/2016/03/17/integrating-asp-net-core-dependency-injection-in-mvc-4/
Also note that if you are not using Owin already, it is not required. You can set it up just the same in Application_Start method of Global.asax. Only change you would need to make is when it references the Startup class in a statement that reflectively gets all the Controller classes, you will need to change that to be the class the code is in (or any other class in your assembly).

what is the difference between Autofac , Autofac.MVC ,Autofac.Integration.Mvc dlls

In my MVC web-app I get the following error thrown:
The request lifetime scope cannot be created because the HttpContext
is not available
When I google it I found a solution would be to upgrade my autofac.Mvc dll but I only use autofac dll, Autofac.Integration.Mvc dll etc and I can't find any dll with autofac.MVC
Am I missing any dll?
What is the difference between Autofac , Autofac.MVC ,Autofac.Integration.Mvc dlls?
The documentation on MVC integration may be of help to you going forward, but let me also answer your question.
First, it's good to understand that there's a difference between NuGet packages and DLLs (aka assemblies). Many times the name of the assembly inside a NuGet package is the same as the package, but sometimes the assembly inside has a different name. (And sometimes a NuGet package has more than one assembly.)
So, to answer your question:
NuGet Package Assembly Inside Purpose
------------- --------------------------- -------------------------
Autofac Autofac.dll Core Autofac
Autofac.Mvc5 Autofac.Integration.Mvc.dll ASP.NET MVC 5 integration
So, when you see that you need to update your MVC integration, what it translates to is that you need the latest version of the Autofac.Mvc5 NuGet package.
Autofac.mvc is used when you integrate autofac to your mvc application same as the autofac. WebAPI which is used when you integrate autofac for WebAPI's application.
MVC integration provides dependency injection integration for controllers, model binders, action filters, and views. It also adds per-request lifetime support.
Because the RegisterModelBinders() extension method uses assembly scanning to add the model binders you need to specify what type(s) the model binders (IModelBinder implementations) are to be registered for.
This is done by using the Autofac.Integration.Mvc.ModelBinderTypeAttribute
Likewise there are many other mvc integration supported by this dll, which are listed down in this link
https://autofac.org/apidoc/html/F26C16A.htm
Autofac lets you inject your constructor parameters along with property and method injection
Hence we need all three dlls to make autofac up and functional in mvc application.
Do let me know if you need any further clarification on this.
Thanks!!

Getting Started with Dependency Injection in Asp.Net Mvc 5.2.3

I'm having trouble getting started setting up dependency injection in my project.
Is there Microsoft provided functionality to do dependency injection with Mvc 5.2.3? If so where is it because I can't find it.
All the articles I can find are about the DI functionality in Core. According to Wikipedia that is the next version of Mvc. I'd rather just use what is built in to my version.
If nothing is built in, then I'll go do research myself on third party solutions.
There's no built-in DI functionality in ASP.NET MVC 5.2.3. You will need to use a third party DI container or write your own. If you are looking for one written by Microsoft you may checkout Unity.

mef or unity is a impretive choice for prism?

Heading for alternative using for prism and di/ioc. mef is skilful framework for prism but not so fast. trying to use autofac for prism because our application infrastructure have to many presentation layer like a web, mobile, tablet(silverlight) and desktop(wpf). mef is not good idea for web(if there is no pluggin operation etc. requirements)
tried mef and autofac together. used aggregate catalog and exposed parts to autofac but it's a not complatetly solution. there is to many problems to be solved. using are together.(prism, prism.mefextensions, prism.autofacextensions and autofac's mefintegration)
using prism.autofacextensions for alternating mef but autofac is not ready to prism i think. tried some alternative projects but not ready. also autofac is not inject import attributed object automatically and not have a scanning tool for directory(export attribute scanner. it's must be coded.) and all prism extensibility must be declared. i think its not ready to use for prism.
is there a any alternative ready to use for prism framework? we must use ioc container for all presentation layers. whats best solution?
Based on my understanding, Unity container supports all the layers you mentioned of ASP.NET Web API, Windows Phone 8, Silverlight and WPF.
In addition, Bootstrapper implementation and any other feature related to PRISM becomes relatively simple to achieve as PRISM already includes the UnityBootstrapper class which implement most of the required functionality.
You can find helpful information for Unity container in the following CodePlex Unity site:
Microsoft Unity Container 3
I hope this helps.

IOC container working with asp.net mvc 2.0

Is there any IOC container that already implements a controller factory compatible with asp.net mvc 2.0 ....if so i'll move my projects to 2.0 to test...
Anyone know a good reference about it?
We're using NInject and it works like a charm, too. I compiled it with MVC2 RTM and it works even with that.
MVCContrib has four different flavors for you ( StructureMap, Windsor, Spring.Net, Unity )
http://github.com/mvccontrib/MvcContrib/tree/mvc2/src/
Here is a direct link to the Unity one:
http://github.com/mvccontrib/MvcContrib/blob/mvc2/src/MvcContrib.Unity/UnityControllerFactory.cs
Good question. I migrated a project to ASP.NEt MVC 2 using Castle Windsor as IoC container. Been struggling to get areas to work to no avail. Keep getting System.ArgumentNullException exceptions. Other than this, everything works as expected...

Resources