tnblog
首页
视频
资源
登录

.net core 选项框架:服务组件集成配置的最佳实践

5836人阅读 2020/7/1 11:21 总访问:3467269 评论:0 收藏:0 手机
分类: .net后台框架

.netcore

.net core 选项框架:服务组件集成配置的最佳实践

如何通过选项框架来处理服务和配置的关系?


特性


  • 支持单例模式读取配置
  • 支持快照
  • 支持配置变更通知
  • 支持运动时动态修改选项值

设计原则


  • 接口分离原则(ISP),我们的类不应该依赖它不使用的配置
  • 关注点分离(SoC),不同组件、服务、类之间的配置不应相互依赖或耦合

建议


  • 为我们的服务设计 XXXOptions
  • 使用 IOptions、IOptionsSnapshort、IOptionsMonitor作为服务构造函数的参数

项目结构

这里我们创建的是一个WebAPI

项目结构

通过选项注入案例

项目内容


OrderService.cs
  1. public interface IOrderService
  2. {
  3. int ShowMaxOrderCount();
  4. }
  5. public class OrderService : IOrderService
  6. {
  7. OrderServiceOptions _orderServiceOptions;
  8. public OrderService(OrderServiceOptions orderServiceOptions)
  9. {
  10. _orderServiceOptions = orderServiceOptions;
  11. }
  12. public int ShowMaxOrderCount()
  13. {
  14. return _orderServiceOptions.MaxOrderCount;
  15. }
  16. }
  17. public class OrderServiceOptions
  18. {
  19. public int MaxOrderCount { get; set; } = 100;
  20. }
Startup.cs
  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.AddSingleton<OrderServiceOptions>();
  4. services.AddSingleton<IOrderService, OrderService>();
  5. services.AddControllers();
  6. }
WeatherForecastController.cs
  1. [HttpGet]
  2. public int Get([FromServices]IOrderService orderService)
  3. {
  4. Console.WriteLine($"orderService.ShowMaxOrderCount:{orderService.ShowMaxOrderCount()}");
  5. return orderService.ShowMaxOrderCount();
  6. }

运行结果

运行结果

服务组件集成配置

修改项目内容

OrderServiceExtensions.cs
  1. namespace Microsoft.Extensions.DependencyInjection
  2. {
  3. public static class OrderServiceExtensions
  4. {
  5. public static IServiceCollection AddOrderService(this IServiceCollection services,Action<OrderServiceOptions> action)
  6. {
  7. return services;
  8. }
  9. }
  10. }
OrderService.cs
  1. public class OrderService : IOrderService
  2. {
  3. IOptions<OrderServiceOptions> _orderServiceOptions;
  4. public OrderService(IOptions<OrderServiceOptions> orderServiceOptions)
  5. {
  6. _orderServiceOptions = orderServiceOptions;
  7. }
  8. public int ShowMaxOrderCount()
  9. {
  10. return _orderServiceOptions.Value.MaxOrderCount;
  11. }
  12. }
Startup.cs
  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.Configure<OrderServiceOptions>(Configuration.GetSection("OrderService"));
  4. services.AddSingleton<IOrderService, OrderService>();
  5. services.AddControllers();
  6. }
appsetting.json
  1. {
  2. "Logging": {
  3. "LogLevel": {
  4. "Default": "Information",
  5. "Microsoft": "Warning",
  6. "Microsoft.Hosting.Lifetime": "Information"
  7. }
  8. },
  9. "OrderService": {
  10. "MaxOrderCount": 200
  11. },
  12. "AllowedHosts": "*"
  13. }

运行结果

运行结果

我们可以看见这里的值通过配置而发生了变化

其他

内置的配置优先级高到低是:
命令行配置
环境变量配置
文件配置


欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739

评价

bk

2020/10/9 14:23:34

我选择 Ctrl + R [嘻嘻]

这一世以无限游戏为使命!
排名
2
文章
634
粉丝
44
评论
93
docker中Sware集群与service
尘叶心繁 : 想学呀!我教你呀
一个bug让程序员走上法庭 索赔金额达400亿日元
叼着奶瓶逛酒吧 : 所以说做程序员也要懂点法律知识
.net core 塑形资源
剑轩 : 收藏收藏
映射AutoMapper
剑轩 : 好是好,这个对效率影响大不大哇,效率高不高
ASP.NET Core 服务注册生命周期
剑轩 : http://www.tnblog.net/aojiancc2/article/details/167
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术