Dependency Injection in ASP.NET WebApi using Ninject

Share this

In this article, I will demonstrate how to implement Dependency Injection in ASP.NET Web API using Ninject. Dependency Injection which is a way to implement Inversion Of Control (IOC) design pattern.

Source Code : Complete source code for the article can be found on GitHub repo :-

https://github.com/sudipta-chaudhari/DependencyInjection_ASP.NET_WebApi_Ninject/

Firstly, let us understand the below key concepts :

  • Dependency
  • Injection
  • Inversion of Control (IOC)
  • Dependency Injection
  • Ninject

Dependency – A dependency is an object that can be used (a service).

Injection – Is the process of passing of a dependency to a dependent object (a client) that would use it.

Inversion of Control (IOC) – Is a design pattern where objects do not create dependencies or other objects on which they rely to do their work. Instead, they get the dependencies or objects that they need from an external service.

Below picture shows some approaches using which IOC can be implemented:

DependencyInjectionIn this article, I will be focusing on Dependency Injection.

Dependency Injection (DI) –  Is a design pattern which is a type of IOC that reduces hard-coded dependencies between your classes by injecting these dependencies at run-time, instead of during design-time.

Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern. The program flow is not sequential anymore, the control is inverted.

Ninject – Ninject is an dependency injector i.e. essentially an IOC container.

Let’s now see what led us to what dependencies look like in our code and how to get rid of dependencies.  In my previous post C# Unit Testing with Moq, we have the ProductRepository.cs class with various different methods. In the WebApi controller ProductController.cs, to call the repository methods, you need to create an repository object and then call the repository methods as shown in the following code.

In the above code, the dependency is as below :

Are Dependencies bad?

Yes, dependencies are bad. As the piece of code shown above defines a dependency, it tightly couples the code. It means that ProductRepository.cs and ProductController.cs are fully dependent on each other. Changing one of these classed may lead to several areas in other classes. Thus, these classes are tightly coupled or dependent on each other.

Loose Coupling : It means two classes or objects are independent and one class can use another class without being dependent on each other.

By now, you understand what is IOC and Dependency Injection. Let’s now proceed to implement Dependency Injection in our project manually.

Manual Dependency Injection : Create an interface IProduct.cs and add the method signature for various needed methods.

In the WebApi controller ProductController.cs, to call the repository methods, you need to create an repository object and then call the repository methods as shown in the following code.

The above code injects the repository object’s dependency using the Controller’s constructor. But still you have to manually instantiate the repository object in the constructor, which still doesn’t loosely couple our design.

Our aim is to loosely couple the code so that the called class ProductRepository.cs and calling class ProductController.cs are not dependent on each other.

Let’s see further how can be achieve a true loose coupling using Dependency Injection using Ninject.

Step 1 : Add Ninject references

Using Nuget, add the following references:-

  • Ninject
  • Ninject.Extensions.ChildKernel

Step 2 : Add Dependency Resolver class

In order to use dependency injection with Web API, we need to create a resolver class that implements IDependencyResolver interface. Under App_Start folder, add a new class NinjectResolver.cs with the following code.

Step 3 : Configure Dependency Resolver class in WebApiConfig

Now, we need to configure NijectResolver.cs class with Web API in the WebApiConfig.cs class as shown below.

In the above code, HttpConfiguration.DependencyResolver is set to NinjectResolver. Now, Web API will use NinjectResolver class to create the objects or dependencies it needs.

I hope you followed the article. If you have any comments, questions or suggestions, leave a message and I will try to respond at my earliest.


Share this

Leave a Reply