Chapter 2 · Practice 4 of 4
Group registrations into extension methods per feature
Why it matters
A Program.cs with two hundred AddScoped lines is hard to read and causes merge conflicts. Group each feature's registrations into an extension method on IServiceCollection, next to the feature's code.
csharp
public static class OrdersModule
{
public static IServiceCollection AddOrders(this IServiceCollection services)
{
services.AddScoped<IOrderService, OrderService>();
services.AddScoped<IOrderRepository, OrderRepository>();
return services;
}
}
Program.cs then reads like a table of contents: builder.Services.AddOrders().AddPayments();. Do not register the same interface twice by accident: the last registration wins for single resolution, which hides bugs.