Register or Login to Your Account
What is dependency injection .net core? Can someone explain it by taking simple real-life based examples? I have checked it out on internet, but it's explained in very complex way everywhere.
Dependency Injection is a software design pattern where dependencies are not created by the client, but rather passed to the client.
Instead of creating dependencies by new keyword, we will define what we need. We delegate the responsibility of passing those to the injector. Class does not need to know how to create dependency and it’s not a part of its logic.
With a separation of creation and behavior of our service we can build loosely coupled services. In our classes, we concentrate on how it’s going to behave.
Let’s say we have a Web API for getting events. We have EventsController, that gets events from EventsProvider and it gets movie ratings from MovieRatingProvider. On the schema it will look like this: