简体   繁体   中英

When to use ASP.NET 5 OptionsModel?

During my investigations through ASP.NET guts I found some interesting part of it - OptionsModel .

Sample code defined in the Startup class:

// Add MVC services to the services container
services.AddMvc(configuration)
    .Configure<MvcOptions>(options =>
    {
        //Configure some MVC options like customizing the 
        // view engines, etc...
        options.ViewEngines.Insert(0, typeof(TestViewEngine));
    });

And there are few questions:

  1. What is the good manner to use OptionsModel in your own solutions ?
  2. Where is it more useful than SettingsModel ?

I see difference between them but want to find best use cases.

From the asp.net docs :

Using Options you can easily convert any class (or POCO - Plain Old CLR Object) into a settings class. It's recommended that you create well-factored settings objects that correspond to certain features within your application, thus following the Interface Segregation Principle (ISP) (classes depend only on the configuration settings they use) as well as Separation of Concerns (settings for disparate parts of your app are managed separately, and thus are less likely to negatively impact one another).

So you can create any POCO class as your options class:

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

You then enable the options service for your application (Which is already done when you add the MVC services):

//In your ConfigureServices method
services.AddOptions();

This allows you to inject an IOptions<YourOptionsClass> on any class that needs to access your options:

public HomeController(IOptions<MyOptions> optionsAccessor)
{
    Options = optionsAccessor.Options;
}

The final piece of the puzzle is how to configure (provide the values) your options.

  • The most obvious way is using the new Configuration framework, for example you could use a config.json file (You don´t need to create a new configuration object, you could reuse the same one added by default with the MVC application template). Once you have your configuration object you can call Configure<YourOptionsClass>(Configuration) :

    Note: When you bind options to configuration each property in your options type is bound to a configuration key of the form property:subproperty:etc, which are case insensitive.

     //In your Startup method MyConfig = new ConfigurationBuilder() .SetBasePath(appEnv.ApplicationBasePath) .AddJsonFile("appsettings.json") .Build(); //In your ConfigureServices method services.Configure<MyOptions>(Configuration); 
  • You can also use a delegate and initialize your options by running any piece of code needed. For example you could provide some hardcoded values (but you could also do something more complex like reading from a database):

     services.Configure<MyOptions>(myOptions => { myOptions.Option1 = "Foo"; myOptions.Option2 = 42; }); 

You can add multiple Configure<MyOptions> calls, and they will be applied in order. For example, you could provide some hardcoded values and allow them to be overriden using the configuration:

services.Configure<MyOptions>(myOptions =>
{
    ...
})
.Configure<MyOptions>(Configuration);

In the end, between the Configuration and the Options frameworks, you have a flexible, extensible, composable and loosely coupled way of providing the settings that your code might need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM