ASP.NET Core Interview Question #48
How should application startup code be organized as a project grows?
Application Structure & Configuration Junior Beginner
Quick Interview Answer
As an ASP.NET Core application grows, Program.cs can become difficult to maintain if every service registration and middleware rule is written inline.
Detailed Explanation
As an ASP.NET Core application grows, Program.cs can become difficult to maintain if every service registration and middleware rule is written inline.
A common approach is to group related registrations into extension methods such as AddApplicationServices(), AddPersistence(), or AddAuthenticationServices(). Middleware and endpoint configuration can also be grouped when it improves readability.
The goal is not to hide behavior unnecessarily, but to keep Program.cs as a clear composition root that shows the application's major dependencies and request pipeline.
Code Example
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddApplicationServices(
this IServiceCollection services)
{
services.AddScoped<IOrderService, OrderService>();
services.AddScoped<IEmailService, EmailService>();
return services;
}
}
// Program.cs
builder.Services.AddApplicationServices();