liveBook · Manning

Configuration binder

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

A configuration binder is a component that maps a collection of configuration values to the properties of a strongly typed object.

A configuration binder maps configuration values to a strongly typed object, often called an options class. It replaces repeated string lookups and manual parsing with typed properties, IntelliSense, and runtime conversion. section 10.3

Binding settings for an endpoint

Separate configuration under MapSettings and AppDisplaySettings, then inject IOptions<AppDisplaySettings>. The handler reads options.Value and returns settings.Title and settings.ShowCopyright without dictionary keys or bool.Parse calls. section 10.3

Design options classes so the binder can construct and populate them:

  • Use a nonabstract class with a public parameterless constructor. section 10.3.3
  • Expose public properties with getters and setters; complex properties need a setter or a non-null value. section 10.3.3
  • Use collections carefully: common list and dictionary interfaces can be created automatically, but initialized IEnumerable properties cannot be replaced. section 10.3.3
Listing 10.6 Separating settings into different objects in appsettings.json
{
  "MapSettings": {                    #1
    "DefaultZoomLevel": 6,            #1
    "DefaultLocation": {              #1
      "latitude": 50.500,             #1
      "longitude": -4.000             #1
    }
  },
  "AppDisplaySettings": {             #2
    "Title":  "Acme Store Locator",   #2
    "ShowCopyright": true             #2
  }
}

Use nested configuration sections to keep settings for different application areas in separate strongly typed objects. section 10.3

IOptions<T> is the canonical integration, but the binder can also bind a configuration section manually, register the resulting object as a singleton, and inject that object directly. This avoids the IOptions<T>.Value step but gives up automatic reload behavior and advanced options features unless added separately. section 10.3.4

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

How deep it goes
Defines binding, demonstrates injection, and details constructors, properties, collections, dictionaries, and reload tradeoffs.
Examples
Uses MapSettings and AppDisplaySettings, appsettings.json sections, IOptions injection, and direct singleton registration.
What it assumes
Assumes configuration sections and dependency injection, while teaching typed settings from dictionary access onward.
What you can do after
Build it from scratch by defining options classes, binding configuration sections, and injecting the resulting settings.
How it connects
Connects configuration binder with options pattern, dependency injection, IConfiguration, JSON deserialization, MVC model binding, and reflection.

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.