Category: Configuring the options

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 […]

Adding post-configuration – Options, Settings, and Configuration

We must take a similar path to add post-configuration values but implement the IPostConfigureOptions<TOptions> instead. To achieve this, we update the ConfigureAllConfigureMeOptions class to implement that interface: namespace OptionsConfiguration;public class ConfigureAllConfigureMeOptions :    IPostConfigureOptions<ConfigureMeOptions>,    IConfigureNamedOptions<ConfigureMeOptions>{    // Omitted previous code    public void PostConfigure(string?name, ConfigureMeOptions options)    {        options.Lines = options.Lines.Append(            $”ConfigureAll:PostConfigure name: {name}”);    }} In the preceding code, […]

Configuring the options – Options, Settings, and Configuration

We want two different options to test out many possibilities: To achieve this, we must add the following lines in the Program.cs file: const string NamedInstance = “MyNamedInstance”;builder.Services    .Configure<ConfigureMeOptions>(builder.Configuration        .GetSection(“configureMe”))    .Configure<ConfigureMeOptions>(NamedInstance, builder.Configuration        .GetSection(“configureMe”)); The preceding code registers a default instance (highlighted code) and a named instance. Both use the configureMe configuration sections and so start […]

Project – OptionsConfiguration – Options, Settings, and Configuration

Now that we have covered basic usage scenarios, let’s attack some more advanced possibilities, such as creating types to configure, initialize, and validate our options.We start by configuring options which happen in two phases: In a nutshell, the post-configuration phase happens later in the process. This is a good place to enforce that some values […]

Reloading options at runtime – Options, Settings, and Configuration

A fascinating aspect of the ASP.NET Core options is that the system reloads the value of the options when someone updates a configuration file like appsettings.json. To try it out, you can: This is an out-of-the-box feature. However, the system rebuilds the options instance, which does not update the references on the previous instance. The […]

Using the settings file – Options, Settings, and Configuration

Loading configurations from a file is often more convenient than hardcoding the values in C#. Moreover, the mechanism allows overriding the configurations using different sources, bringing even more advantages.To load MyOptions from the appsettings.json file, we must first get the configuration section, then configure the options, like the following: var defaultOptionsSection = builder.Configuration    .GetSection(“defaultOptions”);builder.Services    .Configure<MyOptions>(defaultOptionsSection); […]

Loading the configuration – Options, Settings, and Configuration

Before you begin: Join our book community on Discord Give your feedback straight to the author himself and chat to other early readers on our Discord server (find the “architecting-aspnet-core-apps-3e” channel under EARLY ACCESS SUBSCRIPTION). https://packt.link/EarlyAccess This chapter covers the .NET Options pattern, a building block of any application. .NET Core introduced new predefined mechanisms […]



         


          Terms of Use | Accessibility Privacy