Configuration without secret sprawl
Separate deploy-time settings from application behavior.
The idea
A connection string belongs to the deployment environment, not a repository. Bind related settings into an options type and validate them at startup. Decide which values can reload safely; a database pool created once does not automatically follow a later settings change.
Follow the flow
Make it concrete
This excerpt isolates the concept. Connect it to the booking API as you work through the exercise.
builder.Services.AddOptions<BookingOptions>()
.BindConfiguration("Bookings")
.Validate(o => o.MaxSeats > 0, "MaxSeats must be positive")
.ValidateOnStart();
public sealed class BookingOptions
{
public int MaxSeats { get; init; } = 8;
}Decisions that matter
- Environment variables work well for deployment overrides; use a protected secret store for credentials.
- Validate settings once at startup and again when reloadable values change.
- Do not expose all effective settings through a public diagnostic endpoint.
Your turn
The production MaxSeats value is zero. When should this be detected, and what information is safe to log?
Reveal a worked answer
Fail startup validation before accepting traffic. Log the option name and failed rule, but exclude connection strings, tokens, and other secrets. Cover valid and invalid option sets in tests.