Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection and ASP.net MVC 5 / Web API 2

I’m in the process of creating a new web site using ASP.net MVC 5, and it’s also going to have an API using Web API 2. In the past I’ve used Ninject for handling Dependency Injection, but it always seemed a bit flaky with Web API.

I’ve had a look at the Ninject web site, and it doesn’t look like there’s a plugin for MVC 5 (unless the MVC 3 one still works?) and there’s not much out there about using it with MVC 5 (not that I can find anyway).

So I was wondering what the recommended method is for Dependency Injection with MVC 5 and Web API is? Is there anything built in now, or do I still need to use a third party solution? Are there any tutorials out there?

Thanks!

like image 671
Dylan Parry Avatar asked Nov 20 '13 11:11

Dylan Parry


2 Answers

Take a look at this tutorial, it involves a custom DependencyResolver.

http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver

Seems like there is a nuget package

https://www.nuget.org/packages/Ninject.WebApi.DependencyResolver/

but I haven't used it and cannot recommend it.

like image 116
Wiktor Zychla Avatar answered Sep 18 '22 01:09

Wiktor Zychla


Ninject.MVC3 from Nuget works fine for MVC 4 and MVC 5. I have been using it without any issues.

like image 26
sunil Avatar answered Sep 18 '22 01:09

sunil