Research on asp.net core option configuration

The asp.net-core option module is a brand-new, extensible framework, which acts in the entire .net-core framework, as ubiquitous as dependency injection, and is a very important component.

In fact, the configuration module and the option module are closely linked. We can use the ConfigureBuilder class to use the configuration, but in the Startup class, we use dependency injection to implement the IConfiguration interface (the constructor in the Startup class uses dependency injection to instantiate IConfiguration interface), through this interface object to achieve various configurations. But configuration is not what we mainly talk about. This article mainly analyzes the option module.
To talk about options, you must understand the IOptions<TOptions> interface, which only has one attribute:

public interface IOptions<out TOptions> where TOptions : class, new()
{
TOptions Value { get; }
}

Here we describe the options from two examples. The first example is the option example from Microsoft's official website. First define a MyOptions class:

public class MyOptions
{
    public string Option1 { get; set; }

    public int Option2 { get; set; } = 5;

    public MyOptions()
    {
        Option1 = "value1 from ctor";
    }

}

Dependency injection instantiates the MyOptions class:

// Class DI injection not derived from IOptions (for details, please refer to the source code) 
services.Configure<MyOptions>(Configuration);

Next we are in the Index.cshtml.cs file:

private readonly MyOptions _options;
public string SimpleOptions{ get; private set;}

Then inject it in the constructor of IndexModel (mainly to implement dependency injection):

public IndexModel(IOptions<MyOptions> optionAccessor)
{
    _options = optionAccessor.Value;
}

In the OnGet() method:

public void OnGet()
{
    var option1 = _options.Option1;
    var option2 = _options.Option2;
    SimpleOptions = $"option1 = {option1}, option2 = {option2}";
}

Next is Index.cshtml:

@page
@model IndexModel
@using OptionsBeta.Models
@{
ViewData["Title"] = "Home page";
}

<h3>Basic options configuration, not derived from the implementation of the IOptions interface</h3> 
@Model.SimpleOptions

But in general, we inherit from the IOptions interface to make our own options. So what should be done? code show as below:

public class MyOptionsDelegate : IOptions<MyOptionsDelegate>
{
    public MyOptionsDelegate()
    {
        Option1 = "value1 from ctor";
    }

    public string Option1 { get; set; }
    public int Option2 { get; set; } = 5;

    public MyOptionsDelegate Value
    {
        get { return this; }
    }
}

We implement a class of IOptions<TOptions>, and then we extend a method based on the IServiceCollection interface:

public static IServiceCollection AddOptionsByDelegate(this IServiceCollection services, Action<MyOptionsDelegate> setupAction)
{
    services.Configure(setupAction);
    return services;
}

Then we inject the instance in the ConfigurationServices method:

// The way to extend the method to achieve 
services.AddOptionsByDelegate(myOptions =>
{
    myOptions.Option1 = "rrrrrrrrrrrrrrrrrrrrrrrrrr";
    myOptions.Option2 = 280000;
});

Next, the Index.cshtml.cs file:

private readonly MyOptionsDelegate _optionsDelagate;
public IndexModel(IOptions<MyOptions> optionAccessor, IOptions<MyOptionsDelegate> optionsDelegate)
{
    _options = optionAccessor.Value;
    _optionsDelagate = optionsDelegate.Value;
}
public string SimpleOptionsByDelagate { get; private set; }

In the OnGet method:

var optionsByDelegate3 = _optionsDelagate.Option1;
var optionsByDelegate4 = _optionsDelagate.Option2;
SimpleOptionsByDelagate = $"optionsByDelegate3 = {optionsByDelegate3}, optionsByDelegate4 = {optionsByDelegate4}";

In Index.cshtml:

<h3>Configure simple options based on delegates, derive from the implementation of the IOptions interface and use extension methods to inject dependencies</h3> 
@Model.SimpleOptionsByDelagate

Run as follows:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325013781&siteId=291194637