| Muhammad Waseem

About Newsletter
Books
30 .NET Tips
Sponsorship
How to organize your dependencies in .NET
Nov 16, 2023
2 min read

How to organize your dependencies in .NET

Sponsor this Newsletter

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.

builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IRoleService, RoleService>();
builder.Services.AddScoped<AddSecurityHeader>();
builder.Services.AddScoped<HttpClient>();
builder.Services.AddScoped<IValidator<CreateUserCommand>, CreateUserCommandValidator>();

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.

public static IServiceCollection AddApplication Dependencies( this IServiceCollection services)
{
services.AddScoped<IUserService, UserService>();
services.AddScoped<IRoleService, RoleService>();
services.AddScoped<AddSecurityHeader>();
services.AddScoped<HttpClient>();
services.AddScoped<IValidator<CreateUserCommad>, CreateUserCommadValidator>();
return services;
}

Add in Program

builder.Services.AddApplicationDependencies();

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.

public static IServiceCollection AddApiDependencies(this IServiceCollection services)
{
    services.AddServices();
    services.AddDelegateHandlers();
    services.AddValidators();
    return services;
}
public static IServiceCollection AddServices(this IServiceCollection services)
{
    services.AddScoped<IUserService, UserService>();
    services.AddScoped<IRoleService, RoleService>();
    return services;
}
public static IServiceCollection AddDelegateHandlers(this IServiceCollection services)
{
    services.AddScoped<AddSecurityHeader>();
    return services;
}
public static IServiceCollection AddValidators(this IServiceCollection services)
{
    services.AddScoped<IValidator<CreateUserCommand>, CreateUserCommandValidator>();
    return services;
}

Add in Program file

builder.Services.AddApiDependencies();

This way dependencies becomes :

  • More clean

  • Easy to manage

This article was originally published at https://mwaseemzakir.substack.com/ on Nov 16, 2023 .

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
Previous Next

Subscribe to Newsletter

Join 9,000 Software Engineers

Buy Me a Coffee

Enjoy my articles? Support me by buying a coffee!

Buy Me a Coffee

Muhammad Waseem

Resources
  • Books
  • Courses
Newsletter
  • Articles
  • Sponsorship
Books
  • 30 .NET Tips
  • 100 .NET Tips (Soon)
Author
  • About Us
  • Contact Us
Policy
  • Privacy Policy
  • Terms and Conditions
Interview
  • C# & .NET
  • Web API

Join my .NET newsletter and stay updated!

Your Subscribe Form Embed has expired.

If you’re the owner of this site, please create your new embed on Supascribe.

Create New Embed →
;

© 2025 Muhammad Waseem. All rights reserved.