liveBook · Manning

OptionsBuilder<T>

As explained in ASP.NET Core in Action, 3e

OptionsBuilder<T> is a .NET options API type that fluently composes configuration and validation for an options type and can resolve dependencies for configuration actions.

OptionsBuilder<T> provides a fluent way to create and configure strongly typed IOptions<T> objects. Start with AddOptions<T>(), then chain calls such as BindConfiguration and Configure to combine configuration binding with code-based settings. section 31.2 section 31.4

Bind configuration and add code-based settings

builder.Services .AddOptions<CurrencyOptions>() .BindConfiguration("Currencies") .Configure(opts => opts.Currencies = new[] { "GBP", "USD" }); When a dependent service requests IOptions<CurrencyOptions>, the framework creates the options object, applies the registered configuration steps, and exposes the result through opts.Value. section 31.2 section 31.2

OptionsBuilder<T> can resolve dependencies from dependency injection while running a configuration action. Register the dependency, then use a Configure<TDependency> overload to populate the options from that service. For more separation, implement IConfigureOptions<T> and inject the dependency into its constructor. The builder also supports options validation. section 31.2 section 31.4

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains builder creation, configuration chaining, dependency resolution, and an IConfigureOptions<T> alternative, but does not detail validation rules.
Examples
Demonstrates CurrencyOptions bound to Currencies, configured with GBP and USD, and populated through an injected ICurrencyProvider.
What it assumes
Assumes familiarity with IOptions<T>, IConfiguration, configuration binding, and dependency injection.
What you can do after
Builds an IOptions<T> registration that combines configuration data, code, and DI services.
How it connects
Connects IOptions<T> with IConfiguration, dependency injection, and IConfigureOptions<T>.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.