options pattern
Binding a configuration section to a plain class with typed properties, validating it, and injecting it into services instead of reading raw strings.
In more detail
Bound once and validated at startup, a bad setting stops the host instead of failing a request in production. Services receive it through IOptions<T>, IOptionsSnapshot<T> or IOptionsMonitor<T>.
Example
public sealed class CatalogOptions { public const string SectionName = "Catalog"; }Related terms
- IOptions<T> — The options interface that gives a singleton view of an options class, bound once and never recomputed for the life of the process.
- AddOptions — The registration call, services.AddOptions<T>(), that returns an options builder on which binding and validation calls are chained.
- ValidateOnStart — The options-builder call that makes the host validate options while starting and refuse to start if they are invalid.
- data annotations — Attributes from System.ComponentModel.DataAnnotations, such as [Required] and [Range(1, 200)], that declare simple validation rules on properties.
Taught in
- The Options Pattern: Strongly Typed Configuration — Modern .NET Core, C# & Enterprise Microservices
- Environment Layering and User Secrets — Modern .NET Core, C# & Enterprise Microservices
- Authentication: JWTs and Claims — Modern .NET Core, C# & Enterprise Microservices