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 every time you ask for one (transient lifetime), and the factory creates a new options instance every time you call its Create(name) method (transient lifetime).To get the default instance (non-named options), you can use the Options.DefaultName field or pass an empty string; this is usually handled for you by the framework.

If we only configure named options or no instance at all, the consumer will receive an empty TOptions instance (new TOptions()) after calling factory.Create(Options.DefaultName).

IOptionsSnapshot<TOptions>

This interface is useful when you need a snapshot of the settings for the duration of an HTTP request.

  • The container creates only one instance per request (scoped lifetime).
  • It supports named configuration.
  • It supports unnamed default settings through its CurrentValue property.

If we only configure named options or no instance at all, the consumer will receive an empty TOptions instance (new TOptions()).

IOptions<TOptions>

This interface is the first that was added to ASP.NET Core.

  • It does not support advanced scenarios such as what snapshots and monitors do.
  • Whenever you request an IOptions<TOptions> instance, you get the same instance (singleton lifetime).

IOptions<TOptions> does not support named options, so you can only access the default instance.

Now that we looked at the building blocks, we dig into some code to explore leveraging those interfaces.

Project – CommonScenarios

This first example covers multiple basic use cases, such as injecting options, using named options, and storing options values in settings.Let’s start with the shared building block.

Manual configuration

In the composition root, we can manually configure options, which is very useful for configuring ASP.NET Core MVC, the JSON serializer, other pieces of the framework, or our own handcrafted options.Here’s the first options class we use in the code, which contains only a Name property:

namespace CommonScenarios;
public class MyOptions
{
    public string?
Name { get; set; }
}

In the composition root, we can use the Configure extension method that extends the IServiceCollection interface to achieve this. Here’s how we can set the default options of the MyOptions class:

builder.Services.Configure<MyOptions>(myOptions =>
{
    myOptions.Name = “Default Option”;
});

With that code, if we inject that options instance into a class, the value of the Name property will be Default Options.We explore loading settings from a non-hardcoded configuration source next.

Leave a Reply

Your email address will not be published. Required fields are marked *



         


          Terms of Use | Accessibility Privacy