C
C#3w ago
Ale

✅ 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
kurumi
kurumi3w ago
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:
public enum Weather
{
Sunny,
Rainy,
};

public class WeatherService : IWeatherService
{
public Weather GetCurrentWeather() => Weather.Sunny;
}
public enum Weather
{
Sunny,
Rainy,
};

public class WeatherService : IWeatherService
{
public Weather GetCurrentWeather() => Weather.Sunny;
}
so you can normally use it for your solar panel service:
public class SolarPanelService : ISolarPanelService
{
- private readonly IWeatherService _weatherService = new WeatherService();

public bool CanActivate() => _weatherService.GetCurrentWeather() == Weather.Sunny;
}
public class SolarPanelService : ISolarPanelService
{
- private readonly IWeatherService _weatherService = new WeatherService();

public bool CanActivate() => _weatherService.GetCurrentWeather() == Weather.Sunny;
}
so, now take a look at this line
- private readonly IWeatherService _weatherService = new WeatherService();
- private readonly IWeatherService _weatherService = new WeatherService();
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
- private readonly ILogger _logs = new Logger();
- private readonly ICache _cache = new Cache();
- private readonly IEmail _email = new Email();

- private readonly IWeatherService _weatherService = new WeatherService(_logs, _cache, _email, ...);
- private readonly ILogger _logs = new Logger();
- private readonly ICache _cache = new Cache();
- private readonly IEmail _email = new Email();

- private readonly IWeatherService _weatherService = new WeatherService(_logs, _cache, _email, ...);
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:
public class SolarPanelService : ISolarPanelService
{
+ private readonly IWeatherService _weatherService;

+ public SolarPanelService(IWeatherService weather)
+ {
+ _weatherService = weather;
+ }

public bool CanActivate() => _weatherService.GetCurrentWeather() == Weather.Sunny;
}
public class SolarPanelService : ISolarPanelService
{
+ private readonly IWeatherService _weatherService;

+ public SolarPanelService(IWeatherService weather)
+ {
+ _weatherService = weather;
+ }

public bool CanActivate() => _weatherService.GetCurrentWeather() == Weather.Sunny;
}
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
MODiX
MODiX3w ago
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.
Auger
Auger3w ago
had no idea that ```diff was an option 😮
kurumi
kurumi3w ago
haha, I wonder if discord team add full colorized MD support, that will make life easier :when:
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View