Home > Back-end >  Benefit of using Dependency Injection inside ASP.NET Core MVC
Benefit of using Dependency Injection inside ASP.NET Core MVC

Time:11-01

I am reading this article about using DI inside ASP.NET Core @ https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-6.0 .. but i can not understand its benefit of providing abstraction level.

for example without DI, we will have those classes:-

public class MyDependency
{
    public void WriteMessage(string message)
    {
        Console.WriteLine($"MyDependency.WriteMessage called. Message: {message}");
    }
}

public class IndexModel : PageModel
{
    private readonly MyDependency _dependency = new MyDependency();

    public void OnGet()
    {
        _dependency.WriteMessage("IndexModel.OnGet");
    }
}

and with DI we will have those classes:-

public interface IMyDependency
{
    void WriteMessage(string message);
}
public class MyDependency : IMyDependency
{
    public void WriteMessage(string message)
    {
        Console.WriteLine($"MyDependency.WriteMessage Message: {message}");
    }
}
public class Index2Model : PageModel
{
    private readonly IMyDependency _myDependency;

    public Index2Model(IMyDependency myDependency)
    {
        _myDependency = myDependency;            
    }

    public void OnGet()
    {
        _myDependency.WriteMessage("Index2Model.OnGet");
    }
}

but at the end with DI or without DI if i want to modify the WriteMessage method, to accept 2 strings instead of one as follow:-

public void WriteMessage(string message,string message2)
        {
            Console.WriteLine($"MyDependency.WriteMessage called. Message: {message}{message2}");
        }

i will have to modify the related classes; without DI case:-

public class IndexModel : PageModel
{
    private readonly MyDependency _dependency = new MyDependency();

    public void OnGet()
    {
        _dependency.WriteMessage("IndexModel.OnGet","two");
    }
}

with DI case:-

public class Index2Model : PageModel
{
    private readonly IMyDependency _myDependency;

    public Index2Model(IMyDependency myDependency)
    {
        _myDependency = myDependency;            
    }

    public void OnGet()
    {
        _myDependency.WriteMessage("Index2Model.OnGet","two");
    }
}

so not sure how using DI will create an abstraction between the WriteMessage implementation and the classes which consume it.. or i am understanding DI and its benefits wrongly?

Thanks

CodePudding user response:

Roughly, It reduces dependency and makes code more reusable.

Imagine you have an implementation of IMyDependency and you would like to change it to another one, you just need to change the DI configuration instead of change all MyDependency references in your code.

You can also make use of patterns in a simpler way (e.g. singleton/transient instances).

You make code more testeable, mocking interfaces and making use of constructor injection.

Check more advantages in https://jenkov.com/tutorials/dependency-injection/dependency-injection-benefits.html

CodePudding user response:

First of all, it is almost impossible to look at DI as a standalone concept to understand its benefit, you will need to at least understand the difference between a normal class and a class that implements the interface.

Secondly, you are looking at the benefit of DI in the wrong aspects as per @quaabaam's comment.

Without the prerequisites, I hope the below can give you an idea of one of the benefits of using DI.

Based on your example, imagine your dependency class MyDependency is used in more than one place/class. Later on, when you need to replace it with MyUpdatedDependency1(which is very likely nowadays), you will need to update everywhere that used MyDependency.

A solution to the above would create an interface class IDependency, and create implementation class like MyDependency and MyUpdatedDependency1. And allow DI to manage the initialization for you.

As a result, With DI, all you need to do is update your initial set-up AddScope<IDependency,MyDependency> to AddScope<IDependency,MyUpdatedDependency1>

Then DI will handle the rest for you, which includes initialize the target implementation class everywhere.

  • Related