Category: ASP.NET Certifications

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

Named options – Options, Settings, and Configuration

Now, let’s explore named options by configuring two more instances of the MyOptions class. The concept is to associate a configuration of the options with a name. Once that is done, we can request the configuration we need. Unfortunately, the ways we explore named options and most online examples break the Inversion of Control principle. […]

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

IOptionsFactory – Options, Settings, and Configuration

This interface is a factory, as we saw in Chapter 7, Strategy, Abstract Factory, and Singleton, and in Chapter 8, Dependency Injection, we use factories to create instances; this interface is no different. Unless necessary, I suggest sticking with IOptionsMonitor<TOptions> or IOptionsSnapshot<TOptions> instead. How the factory works is simple: the container creates a new factory […]

Learning the building blocks – Options, Settings, and Configuration

There are four main interfaces to use settings: IOptionsMonitor<TOptions>, IOptionsFactory<TOptions>, IOptionsSnapshot<TOptions>, and IOptions<TOptions>. We must inject that dependency into a class to use the available settings. TOptions is the type that represents the settings that we want to access.The framework returns an empty instance of your options class if you don’t configure it. We learn […]

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

Summary – Dependency Injection

This chapter delved into Dependency Injection, understanding its crucial role in crafting adaptable systems. We learned how DI applies the Inversion of Control principle, shifting dependency creation from the objects to the composition root. We explored the IoC container’s role in object management, service resolution and injection, and dependency lifetime management. We tackled the Control […]

Revisiting the Factory pattern – Dependency Injection

A factory creates other objects; it is like a literal real-world factory. We explored in the previous chapter how to leverage the Abstract Factory pattern to create families of objects. A factory can be as simple as an interface with one or more Create[Object] methods or, even more, a simple delegate. We explore a DI-oriented […]



         


          Terms of Use | Accessibility Privacy