ASP.NET Core Interview Question #49

What is the difference between configuration and dependency injection?

Application Structure & Configuration Junior Beginner

Quick Interview Answer

Configuration and dependency injection solve different problems.

Detailed Explanation

Configuration and dependency injection solve different problems.

Configuration supplies values such as connection strings, URLs, limits, feature settings, and operational options. Dependency injection supplies objects and services that implement application behavior.

They often work together. Configuration can be bound to an options class, and that options object can then be injected into a service. Keeping the responsibilities separate improves testability and avoids service classes directly depending on configuration infrastructure when they only need a few typed settings.

Code Example

builder.Services.Configure<EmailSettings>(
    builder.Configuration.GetSection("EmailSettings"));

builder.Services.AddScoped<IEmailService, EmailService>();

public EmailService(IOptions<EmailSettings> options)
{
    _settings = options.Value;
}