✅ Dependency injection
where the dependencies are injected into the constructor. Can someone explain to me less formally so that I understand better. I understood how much about the dependent class and the supplying class
5 Replies
DI aka dependency injection is all about IoC (inversion of control).
Imaginary a situation when you are creating solar panels service. It needs to know what is the current weather. You have your weather service:
so you can normally use it for your solar panel service:
so, now take a look at this line
as you see your are creating your WeatherService inside of another service.
And now imagine that your boss told you to add logging into weather, then to add cache system, then to add email for some reason (idk, maybe boss wants to send weather email, XD).
So, it becomes more and more complex
so it is super complex now...
now let's do some DI. Instead of controlling all dependencies inside of your SolarPanelService you tell: "Yeah, I have some things which I depends on (and they are also depends on others)", so you need to get them for your service, and now we call constructor:
so now you invert of control dependencies to somewhere else.
In real situations we have some systems to control our dependencies, which injects everything you need.
they are called dependency containers (or something like this, I don't really remember, XD).
AddSingleton, AddScoped and AddTransient injects them, if we are talking specifically about ASP NET
$di
Dependency injection is really just a pretentious way to say 'taking an argument'
See: http://blog.ploeh.dk/2017/01/27/dependency-injection-is-passing-an-argument/
Dependency injection is passing an argument
Is dependency injection really just passing an argument? A brief review.
had no idea that ```diff was an option 😮
haha, I wonder if discord team add full colorized MD support, that will make life easier :when:
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View