Notes for asp.net core options Options module

This blog is written for myself. I have seen the emergence of AddOptions more than once, whether it is in the .net core source code or other people's frameworks, it is full of AddOptions. So I probably researched it, but I didn't go in depth, because my skill is still not enough, and I will go back and study when I have the ability. I have to say it again here, because the importance of DI is self-evident, so I don’t have to talk too much about it, just say my own understanding: 

The DI implementation is actually very simple. First, design a class to implement the interface, instead of writing all the program logic in a class file, then we pass in an interface and a class that inherits from the interface as parameters, and then we use the corresponding function that Taking the generic parameter T as a formal parameter, pseudocode:

//call part

HandleDI<ITest, Test>

//implementation part

HandleDI<TInterface, T>

// Use reflection, EMIT, delegate to instantiate T to create an object of TInterface

Then we use reflection or EMIT or delegate the TInterface object. This is the implementation process of DI.

DI put it bluntly, the role is to decouple the instantiation of the class inherited from the interface

If you implement your own option configuration class based on IOptions<TOptions> in the program, it is best to call AddOptions to complete the instantiation of several important objects of Options.

AddOptions:
Complete the instantiation of several important Options objects:

public static IServiceCollection AddOptions(this IServiceCollection services)
{
    if (services == null)
    {
        throw new ArgumentNullException(nameof(services));
    }

    services.TryAdd(ServiceDescriptor.Singleton(typeof(IOptions<>), typeof(OptionsManager<>)));
    services.TryAdd(ServiceDescriptor.Scoped(typeof(IOptionsSnapshot<>), typeof(OptionsManager<>)));
    services.TryAdd(ServiceDescriptor.Singleton(typeof(IOptionsMonitor<>), typeof(OptionsMonitor<>)));
    services.TryAdd(ServiceDescriptor.Transient(typeof(IOptionsFactory<>), typeof(OptionsFactory<>)));
    services.TryAdd(ServiceDescriptor.Singleton(typeof(IOptionsMonitorCache<>), typeof(OptionsCache<>)));
    return services;
}
View Code

Configure: The instantiation process of the TOption class is completed, and finally AddSingleton is called to do DI.

/// <summary>
        /// Registers an action used to configure a particular type of options.
        /// Note: These are run before all <seealso cref="PostConfigure{TOptions}(IServiceCollection, Action{TOptions})"/>.
        /// </summary>
        /// <typeparam name="TOptions">The options type to be configured.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
        /// <param name="configureOptions">The action used to configure the options.</param>
        /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
        public static IServiceCollection Configure<TOptions>(this IServiceCollection services, Action<TOptions> configureOptions) where TOptions : class
            => services.Configure(Options.Options.DefaultName, configureOptions);

        /// <summary>
        /// Registers an action used to configure a particular type of options.
        /// Note: These are run before all <seealso cref="PostConfigure{TOptions}(IServiceCollection, Action{TOptions})"/>.
        /// </summary>
        /// <typeparam name="TOptions">The options type to be configured.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
        /// <param name="name">The name of the options instance.</param>
        /// <param name="configureOptions">The action used to configure the options.</param>
        /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
        public static IServiceCollection Configure<TOptions>(this IServiceCollection services, string name, Action<TOptions> configureOptions)
            where TOptions : class
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (configureOptions == null)
            {
                throw new ArgumentNullException(nameof(configureOptions));
            }

            services.AddOptions();
            services.AddSingleton<IConfigureOptions<TOptions>>(new ConfigureNamedOptions<TOptions>(name, configureOptions));
            return services;
        }
View Code

The implementation of these two is the credit of IOC. If you don't study its function, you just look at the code, which is actually the instantiated interface class of DI.

Reference article:  Options[3]:IOptionsMonitor for ASP.NET Core 2.1 source code learning

The new configuration system adopted by .NET Core [1]: read configuration data

Guess you like

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