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
I want to point out that you don't have to use interfaces to use dependency injection. You definitely can, and that's great if you want to enforce a contract where you may potentially have a different implementation in the future. But it's not required. You can inject a service or other dependency without an interface.
One of the key benefits to DI is that you don't have to wire up the dependencies manually. In the example you gave MyDependency
doesn't have any dependencies. But what if it had four arguments for other services, or repositories etc. Now in Index2Model
you have to create an instance of all of those dependencies, pass them into your new MyDependency
constructor. It becomes a lot of work if you have a more complex application. With DI it just gives you an instance all ready to go.
The other big one is scopes. One big issue that I've seen in many different software products is accidental state. Someone gets a bit sloppy with their coding and creates a variable in a class and if that same instance gets used by more than one request you can have accidental state in the application.
With DI you can default to a transient dependency where each time you go to use the dependency it's a brand new instance, but you can easily switch a particular dependency to be scoped which keeps the same instance for the lifecycle of an HTTP request, which would be difficult to replicate manually, or you could set it to singleton when that makes sense.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With