ASP.Net Core Dependency Injection

Dependency Injection is a design pattern to achieve loose coupling between objects and their dependencies.

A design pattern is an approach you adopt to develop software applications. They are intended to enable reusing the pattern while developing applications. Loose coupling is a feature which indicates that software components are not tightly intervened and each component has no knowledge of the other components.

Dependency Injection is an important and extensively used feature in ASP.Net Core application development.

In every software application, for each class we create will have its dependent classes, classes will not instantiate its dependency rather they are injected using constructors. This makes your application loosely coupled, flexible and is ideal for testing.

What is meant by Injection?

Injection is a technique used in software development to achieve loose coupling. It is done by passing class dependencies through its constructor. This technique eliminates the need to create instances of a class explicitly. Instead, they (classes) are made available when required.

This technic is known as dependency injection.

A software application developer should be thoroughly knowledgeable about how to implement dependency injection.

Implementing dependency injection in an enterprise application development having many classes is a tedious process. We have to use third party framework to implement this technique.

The good news is, Microsoft has included a built-in Dependency Injection Framework with ASP.NET Core.

What you should know to implement DI in commercial software applications:

> Create the Service

> Create a model class

> Create an interface and a repository to handle Customers

> Register a Service

> Use the Service - Injection in ASP .Net Core MVC controller

> Understand the service lifetime

> In the second chapter, we develop a component and use the new implementation (component) in the same controller and perform mock tests

> In the third chapter, we register the Database context with dependency injection in ASP .Net Core application

Demonstrates how Dependency Injection works in an ASP.NET Core application with an Example by using the native ASP.NET Core DI container

The book titled Develop ERP Application using ASP .Net Core explains all of the above in the first and second chapters so that you will know how to use DI in the rest of the application you will develop yourselves.

 

Here is an article by Matthew P Jones on Dependency Injection and lifetime with an example.

Microsoft documentation for Dependency Injection in ASP.NET Core with an example is here.

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection

1 comment on “ ASP.Net Core Dependency Injection ”

Leave a Reply

Your email address will not be published. Required fields are marked *