15 | Options Framework: Best Practices Services Component Integration configuration
This section explains how to use the relationship framework to deal with service options and configuration
Options Framework features:
1, support the single mode to read configuration Example
2, support snapshot
3, supports the configuration change notification
4, modify the option values to support dynamic run-time
When designing the system need to follow two principles:
1, the principle of separation of the interface (the ISP), we should not rely on the configuration class it does not use the
2, separation of concerns (the SoC), arranged between the different components, services, or coupled type not interdependent
Suggest:
1, for our service design XXXOptions
2, IOptions
This will make all kinds of our ability to achieve faster service configuration
Source link:
https://github.com/witskeeper/geektime/tree/master/samples/OptionsDemo
In the definition of the service, generally define the service interface
namespace OptionsDemo.Services
{
public interface IOrderService
{
int ShowMaxOrderCount();
}
public class OrderService : IOrderService
{
OrderServiceOptions _options;
public OrderService(OrderServiceOptions options)
{
_options = options;
}
public int ShowMaxOrderCount()
{
return _options.MaxOrderCount;
}
}
// 代表从配置中读取的值
public class OrderServiceOptions
{
public int MaxOrderCount { get; set; } = 100;
}
}
Followed by a service registry
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<OrderServiceOptions>();
services.AddSingleton<IOrderService, OrderService>();
}
Followed by the definition of the controller
[HttpGet]
public int Get([FromServices]IOrderService orderService)
{
Console.WriteLine($"orderService.ShowMaxOrderCount:{orderService.ShowMaxOrderCount()}");
return 1;
}
Start the program, the output is as follows:
orderService.ShowMaxOrderCount:100
If you say that we need to put this value with the configuration bindings, how to do it?
Necessary to introduce the Options Framework
ASP.NET Core default has actually helped us to come in the framework of the introduction of
Namespace is: Microsoft.Extensions.Options
We need to modify the service to the Senate
public class OrderService : IOrderService
{
//OrderServiceOptions _options;
IOptionsMonitor<OrderServiceOptions> _options;
//public OrderService(OrderServiceOptions options)
public OrderService(IOptionsMonitor<OrderServiceOptions> options)
{
_options = options;
}
public int ShowMaxOrderCount()
{
//return _options.MaxOrderCount;
return _options.Value.MaxOrderCount;
}
}
Registered when using config method, read from the configuration file
public void ConfigureServices(IServiceCollection services)
{
//services.AddSingleton<OrderServiceOptions>();
services.Configure<OrderServiceOptions>(Configuration.GetSection("OrderService"));
services.AddSingleton<IOrderService, OrderService>();
}
Profiles
{
"OrderService": {
"MaxOrderCount": 200
}
}
Start the program, the output is as follows:
orderService.ShowMaxOrderCount:200
You can see, the output value of 200, indicating that the configuration has been completed and binding options
Service depends only on the OrderServiceOptions, and did not depend on the configuration framework, that is to say only what they value the service configuration is that it does not care about the value configured come from, lifted the dependencies between configuration and service
In addition to all of the service may be separately designed their Options, configure the options between such services also are not dependent on each other
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
欢迎转载、使用、重新发布,但务必保留文章署名 郑子铭 (包含链接: http://www.cnblogs.com/MingsonZheng/ ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。
如有任何疑问,请与我联系 ([email protected]) 。