Getting Started with Dependency Injection in Asp.Net Mvc 5.2.3 - dependency-injection

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.

Related

Migrate from Using ninject in .Net Classic to .Net Core

I'm decided to migrate from .Net classic web api project to .Net core project. Many developers say to me for migration you don't have any concern about code changes, because .net core support package for Ninject, but I don't see any operational example. Please tell me exactly could I use my old api controller from .net classic project in .net core project using Ninject?
ASP.NET core comes with completly new web framework. Regarding your existing controllers from classic web api framework, you can do the following:
rewrite them to fit the new framework
or use Microsoft.AspNetCore.Mvc.WebApiCompatShim, so you can reuse them
Here is the article describing this topic:
https://learn.microsoft.com/en-us/aspnet/core/migration/webapi?view=aspnetcore-2.2
What you don't have to rewrite are dependencies that can be ported to .nestandard. This is also the case of Ninject, because it already supports .nestandard, so if you've already written any Ninject modules you can use them. But the integration with the ASP.NET core looks different, beacause the framework is different. As already commented this link shows the integration:
How to integrate Ninject into ASP.NET Core 2.0 Web applications?

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!!

Is it a good idea to add WebAPI into an existing ASP.NET MVC project?

We have an existing ASP.NET MVC 5 project. We want to use WebAPI too.
Is it a good idea to install WebAPI into the MVC project using Nuget? Or would it be better to create a new project in the same VS Solution specifically for WebAPI? If the latter, how would the routing be done?
I gather having MVC and WebAPI in the same project can cause issues with Dependency Injection. Is this true?
You can put the together in the same project, install the nuget package and then add the startup classes (you can see how to do that in the template).
I can't speak to dependency resolver issues though.
However I would ask myself first why would you want MVC + Web API, in many cases you can just use MVC to do that job, and thus you stay with one framework.
I'd really consider Web API if you want to use content negotiation, OData or if you have a lot of ready made Web API code you want to port.

does nhibernate.burrow work with mvc.net and dot net 4.0 framework

I am thinking of using nHibernate.Burrow in my mvc.net application. However there are several troubling things that I have read and I am hoping to get them sorted out before I embark on the project:
Are there any issues with running .Burrow with mvc.net?
Are there issues with running .Burrow with the 4.0 framework?
How tightly coupled is .Burrow with the nHibernate? I have read several things indicating that I have to use the same version of nHibernate as was used to create the .Burrow binaries.
Any other thoughts that people have?
Yes, you should use Burrow with appropriate NHibernate version. If you want to use it with another one you can try to put assemblyRedirect in web.config file.
And I wouldn't try to use Burrow with MVC. Yes, it has Session magement and some other usefull featrures, but they are tied to ASP.NET Web Forms.
As a base framework for mvc applications I would suggest Sharp Architecture. It has all required binaries and all versions are latest.

Resources