惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
云风的 BLOG
云风的 BLOG
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
GbyAI
GbyAI
C
Check Point Blog
M
MIT News - Artificial intelligence
T
The Blog of Author Tim Ferriss
Jina AI
Jina AI
博客园 - 【当耐特】
U
Unit 42
月光博客
月光博客
腾讯CDC
Y
Y Combinator Blog
小众软件
小众软件
博客园_首页
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
The GitHub Blog
The GitHub Blog
博客园 - 聂微东
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
T
Tailwind CSS Blog

博客园 - 【唐】三三

《python编程从入门到实践》15 生成数据 《python编程从入门到实践》10-11章 文件和异常、测试代码 《python编程从入门到实践》8-9章 函数、类 fastcode 代码理解 AI 框架 Oh-My-OpenCode 3.5.6 完整使用指南 AI工具免费模型排名对比 (2026-02-12) ABP演示 - 分布式 Event Bus(使用rabbitmq) ABP演示 - 工作单元 (UOW) ABP演示 - 异常处理 ABP演示 - 缓存 APP - 实体变更监视事件 [EntityCreatedEventData、EntityUpdatedEventData、EntityDeletedEventData] ABP演示 - 授权 ABP演示 - 本地Event Bus ABP演示 - 简单的实现DDD LINQ:SelectMany LINQ - Concat、Union、Intersect、Except IEqualityComparer Nginx - 内置变量 vite - vue 打包 vue - 以deifne开头的 API vue - 进阶
ABP演示 - 实现选项 OPtion
【唐】三三 · 2026-01-09 · via 博客园 - 【唐】三三

1. Options 类定义 (PublisherOptions.cs)

  • 包含出版社相关的配置参数

  • 使用数据注解进行验证

  • 提供默认值

image

using System.ComponentModel.DataAnnotations;

namespace Acme.BookStore.Options
{
    public class PublisherOptions
    {
        [Required]
        [Range(1, 100)]
        public int DefaultPageSize { get; set; } = 10;

        [Required]
        [Range(1, 1000)]
        public int MaxPageSize { get; set; } = 100;

        [Required]
        [StringLength(100)]
        public string DefaultSorting { get; set; } = "Name";

        public bool EnableCaching { get; set; } = true;

        [Range(1, 1440)]
        public int CacheDurationMinutes { get; set; } = 30;

        [Url]
        public string? DefaultWebsiteUrl { get; set; }

        [EmailAddress]
        public string? SupportEmail { get; set; }
    }
}

2. 配置注册 (BookStoreDomainModule.cs)

  • 在依赖注入容器中注册 Options

  • 从 appsettings.json 的 Publisher 节点读取配置

image

public override void ConfigureServices(ServiceConfigurationContext context)
{
    // 配置 PublisherOptions
    context.Services.Configure<PublisherOptions>(
        context.Services.GetConfiguration().GetSection("Publisher")
    );
}

3. 配置示例 (appsettings.json)

image

{
  "App": {
    "SelfUrl": "https://localhost:44333"
  },
  "ConnectionStrings": {
    "Default": "Server=SU;Database=BookStore;User Id=sa;Password=123456;Trusted_Connection=True;TrustServerCertificate=True"
  },
  "StringEncryption": {
    "DefaultPassPhrase": "k0uGenCTlEUMJraa"
  },
  "Publisher": {
    "DefaultPageSize": 20,
    "MaxPageSize": 100,
    "DefaultSorting": "Name",
    "EnableCaching": true,
    "CacheDurationMinutes": 30,
    "DefaultWebsiteUrl": "https://publisher.example.com",
    "SupportEmail": "support@publisher.example.com"
  }
}

4. 使用示例 (PublisherAppService.cs)

  • 通过 IOptions<PublisherOptions> 注入配置
  • 在业务逻辑中使用配置值:
    • 默认排序字段从配置读取
    • 分页大小受配置的最大值限制
    • 统一的配置管理

image

🎯 核心特性

  1. 强类型配置 - 编译时类型检查
  2. 验证支持 - 数据注解自动验证
  3. 默认值 - 提供合理的默认配置
  4. 热重载 - 运行时配置更新(如果启用)
  5. 依赖注入 - 轻松注入到任何服务

📋 如何使用

  1. 修改配置:直接在 appsettings.json 中修改 Publisher 节点
  2. 添加新配置:在 PublisherOptions 类中添加属性,然后更新配置文件
  3. 在其他服务中使用:注入 IOptions<PublisherOptions> 即可