Why we use regions? The use of regions is an antipattern and should be avoid in most of the cases. Using regions is a symptom of large classes with different responsabilities (Single Responsability Principle of SOLID).
It is better to organize Dependency Injection configuration in features. For example we can have classes to configure MVC, Swagger, HealthChecks, ApplicationServices...
This can be done with extension methods:
Before
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddAuthorization();
services.AddAuthentication().AddOpenIdConnect();
services.AddScoped<IFooRepository, FooRepository>();
services.AddScoped<IBarRepository, FooServiceFoo>();
services.AddScoped<IFooService, ServiceFoo>();
services.AddScoped<IBarService, ServiceBar>();
}
After
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddAuthorization();
services.AddAuthentication().AddOpenIdConnect();
services.AddApplicationServices();
}
public static class ApplicationConfiguration
{
public static AddApplicationServices(this IServiceCollection services)
{
services.AddScoped<IFooRepository, FooRepository>();
services.AddScoped<IBarRepository, FooServiceFoo>();
services.AddScoped<IFooService, ServiceFoo>();
services.AddScoped<IBarService, ServiceBar>();
}
}