Year: 2024

Summary – Options, Settings, and Configuration

This chapter explored the Options pattern, a powerful tool allowing us to configure our ASP.NET Core applications. It enables us to change the application without altering the code. The capability even allows the application to reload the options at runtime when a configuration file is updated without downtime. We learned to load settings from multiple […]

Using the ValidateOptionsResultBuilder class – Options, Settings, and Configuration

The ValidateOptionsResultBuilder is a new type in .NET 8. It allows to dynamically accumulate validation errors and create a ValidateOptionsResult object representing its current state.Its basic usage is straightforward, as we are about to see. Project – ValidateOptionsResultBuilder In this project, we are validating the MyOptions object. The type has multiple validation rules, and we […]

Using the configuration-binding source generator – Options, Settings, and Configuration

.NET 8 introduces a configuration-binding source generator that provides an alternative to the default reflection-based implementation. In simple terms, the name of the options class properties and the settings keys are now hard-coded, accelerating the configuration retrieval. Beware, the settings keys are case-sensitive and map one-on-one with the C# class property name, unlike the non-generated […]

Project – Centralizing the configuration – Options, Settings, and Configuration

Creating classes and classes is very object-oriented and follows the single-responsibility principle, among others. However, dividing responsibilities into programming concerns is not always what leads to the easiest code to understand because it creates a lot of classes and files, often spread across multiple layers and more.An alternative is to regroup the initialization and validation […]

Workaround – Injecting options directly – Options, Settings, and Configuration

The only negative point about the .NET Options pattern is that we must tie our code to the framework’s interfaces. We must inject an interface like IOptionsMonitor<Options> instead of the Options class itself. By letting the consumers choose the interface, we let them control the lifetime of the options, which breaks the inversion of control, […]

Project – OptionsValidationFluentValidation – Options, Settings, and Configuration

In this project, we validate options classes using FluentValidation. FluentValidation is a popular open-source library that provides a validation framework different from data annotations. We explore FluentValidation more in Chapter 15, Getting Started with Vertical Slice Architecture, but that should not hinder you from following this example.Here, I want to show you how to leverage […]

Validation types – Options, Settings, and Configuration

To implement options validation types or options validators, we can create a class that implements one or more IValidateOptions<TOptions> interfaces. One type can validate multiple options, and multiple types can validate the same options, so the possible combinations should cover all use cases.Using a custom class is no harder than using data annotations. However, it […]



         


          Terms of Use | Accessibility Privacy