banner



How To Inject Service .net Web Api

First, we accept to know what dependency injection is. DI is a design pattern and with the aid of this, we will develop a loosely coupled code.

This is the not bad idea, through which we volition reduce tight coupling between software components.

The principal Idea of DI is to develop a project with maintainable code.

We tin can develop DI with different ways,

In this article, we will develop DI with the help of constructor injection.

Now permit'south starting time coding,

Stride oneOpen Visual Studio => New Project => Web => Visual Studio 2012 => ASP.Net MVC 4 Spider web Awarding, give a dainty name to your project and click OK.

Step 2Select Empty Template => OK and your solution is gear up.

Step threeI'chiliad non going to make this solution too heavy, for this, I made two foldes in this solution, and gave the proper noun BusinessLayer and DataAccessLayer.

In the folder BusinessLayer, make a class- SchoolBusinessLayer.cs and 1 interface- ISchoolBusinessLayer.cs
SimilarinDataAccessLayer. Make a class- SchoolDataAccessLayer.cs and i interface-ISchoolDataAccesslayer.cs.

Step 4Basically, DataAccesslayer is used to communicate with the database but, in this article, I'g not going to employ any kind of database communication.

From Data admission Layer, I am just going to return an object school with the information. Our goal is to develop Dependency Injection.

In Model, I will make a class file School.cs and declare some property.

Step 5

Declare a method in ISchoolDataAccessLayer.cs.

Pace half dozen

ImplementISchoolDataAccessLayer.cs method in Due southchoolDataAccessLayer.cs.


Step 7

In ISchoolBusinessLayer.cs, declare a method GetSchoolInformation.

Step 8

Implement this method in SchoolBusinessLayer.cs. This method gets information from DataAccessLayer.

For this, in Constructor of SchoolBusinessLayer, inject ISchoolDataAccessLayer.

Hence, we are non direct dependent upon SchoolDataAccessLayer.cs (This technique is used to make an Application Loosely Couple).

Step 9Make a SchoolController and in SchoolController Constructor,  implement ISchoolBusinessLayer, with the aid of an Injection.

Throughout the project development, we never created any object. We communicated with one layer and another with the aid of an interface.

Permit'due south run the project.

I used Elementary Injector.

Now, we take to discuss about Unproblematic Injector. Unproblematic Injector is an easy, flexible and fast dependency injection.

It is used to develop an Application, which is loosely coupled.

For this, we need to utilize Simple Injector

Pace 10

I install Simple Injector in my Project using

Install-Packet Elementary Injector.Integration.WebApi -Version 3.1.5

Afterwards successful installation, two DLL'due south are added, as show below:

SimpleInjector.dll
SimpleInjector.Extensions.ExecutionContextScoping.dll

Now we have to do some lawmaking for unproblematic Injector.

Step 11

In Gloabal.asax.cs, make a container with the assist of a Simple Injector, Container Annals Interface and there is a respected class and at the terminate, verify all.

Stride 12

I made a form SimpleInjectorDependencyResolver and call it from Later on container.Verify();

Step 13

Now, run the projection and y'all will notice the output.

Source: https://www.c-sharpcorner.com/article/implement-dependency-injection-in-web-api/

Posted by: williamswifigh75.blogspot.com

0 Response to "How To Inject Service .net Web Api"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel