Why is it generally considered a good practice to keep DI in separate class ?
๐๐ก๐ฒ ๐ข๐ฌ ๐ข๐ญ ๐ ๐๐ง๐๐ซ๐๐ฅ๐ฅ๐ฒ ๐๐จ๐ง๐ฌ๐ข๐๐๐ซ๐๐ ๐ ๐จ๐จ๐ ๐ฉ๐ซ๐๐๐ญ๐ข๐๐ ๐ญ๐จ ๐ค๐๐๐ฉ ๐๐๐ฉ๐๐ง๐๐๐ง๐๐ฒ ๐ข๐ง๐ฃ๐๐๐ญ๐ข๐จ๐ง๐ฌโฆ
๐๐ก๐ฒ ๐ข๐ฌ ๐ข๐ญ ๐ ๐๐ง๐๐ซ๐๐ฅ๐ฅ๐ฒ ๐๐จ๐ง๐ฌ๐ข๐๐๐ซ๐๐ ๐ ๐จ๐จ๐ ๐ฉ๐ซ๐๐๐ญ๐ข๐๐ ๐ญ๐จ ๐ค๐๐๐ฉ ๐๐๐ฉ๐๐ง๐๐๐ง๐๐ฒ ๐ข๐ง๐ฃ๐๐๐ญ๐ข๐จ๐ง๐ฌ ๐ข๐ง ๐ฌ๐๐ฉ๐๐ซ๐๐ญ๐ ๐๐ฅ๐๐ฌ๐ฌ๐๐ฌ
It is good practice to move all of your dependency injection work in a separate class instead of filling your Program.cs with all Injections. Here are some benefits of this approach
1. By keeping dependency injections in separate classes, you can better adhere to the principle of separation of concerns. So, your Program.cs is just focusing on configuration and its DI class headache to manage dependencies.
2. Keeping dependency injections in separate classes can make it easier to maintain the application over time. For any change in DIโs, you would not be changing the Program.cs rather you would just change the desired dependency injection class
3. For better understanding we can create different DI classes that would be dealing with similar dependencies.
How you prefer to keep your dependencies? ๐โคต๏ธ
If you like my work you can help me in growing through following ways
Subscribe my Weeklyย .NET Newsletter where I would be publishing one tip on C#/.NET on weekly basis.
You can Buy a Coffee for me.
#dependencyinjectio #csharp #dotnet #dotnetcore #dotnet6