📎 Webclip
What is the cleanest way to get configuration values in .NET?
Milan Jovanović explains that application configuration in .NET often comes from environment variables or JSON files and that directly accessing individual values via IConfiguration is cumbersome and error‑prone. He recommends using the Options pattern instead, where settings are represented by strongly typed classes that can be bound to configuration and consumed through IOptions, with support for validation.
Reading notes#
- Application configuration typically resides in environment variables or JSON configuration files.
- Accessing configuration values directly with the
IConfigurationinterface is described as error‑prone and awkward to use. - The Options pattern is proposed as a cleaner alternative for handling configuration data in .NET applications.
- Using the Options pattern starts with defining a class that models the configuration settings.
- The properties of the settings class are bound to the corresponding application settings from JSON configuration.
- The configured settings are then consumed in application code via the
IOptionsabstraction. - Data annotations can be applied on the settings class to perform simple validation of configuration values.
- More complex configuration validation rules can be implemented with tools such as FluentValidation.
- Jovanović promotes his .NET weekly newsletter as a resource with short, practical advice on .NET and software architecture.
- He mentions that configuration using this approach can be updated on the fly.
- The thread ends by asking readers whether they are already using the Options pattern.
