How to organize your dependencies in .NET

In the world of .NET development, it’s a common practice to establish registrations for dependencies, ranging from basic services to third-party components obtained through NuGet packages.

1/ Inline Dependency Registration

Consider a straightforward example of dependency registration. Below is an illustration of how dependencies would be registered in a Program.cs file.

How to organize your dependencies in .NET

2/ Extension Method Dependency Registration

The previous approach is suitable but becomes less efficient as the number of dependencies increases.

A more effective strategy involves relocating these dependencies into an extension method and then invoking that extension method within the Program file.

How to organize your dependencies in .NET

Add in Program How to organize your dependencies in .NET

3/ Modularized Dependency Registration

I prefer to create distinct extension methods for each concern.

Then, I consolidate the invocation of these methods into a single method, and only this method is called in the Program file.

This ensures a more organized and modular approach to dependency registration. How to organize your dependencies in .NET

Add in Program file How to organize your dependencies in .NET

This way dependencies becomes :

  • More clean

  • Easy to manage

This article was originally published at https://mwaseemzakir.substack.com/ on .

Whenever you're ready, there are 3 ways I can help you:

  1. Subscribe to my youtube channel : For in-depth tutorials, coding tips, and industry insights.
  2. Promote yourself to 9,000+ subscribers : By sponsoring this newsletter
  3. Patreon community : Get access to all of my blogs and articles at one place