{"id":31912149,"url":"https://github.com/vipwan/biwen.settings","last_synced_at":"2025-10-29T09:02:49.648Z","repository":{"id":191372547,"uuid":"684505986","full_name":"vipwan/Biwen.Settings","owner":"vipwan","description":"Biwen.Settings 组件配置模块","archived":false,"fork":false,"pushed_at":"2025-08-05T13:31:30.000Z","size":7498,"stargazers_count":11,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-10T17:06:06.614Z","etag":null,"topics":["configuration","netcore","options","setting"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vipwan.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-08-29T09:14:40.000Z","updated_at":"2025-08-05T13:31:33.000Z","dependencies_parsed_at":"2024-05-21T05:28:15.804Z","dependency_job_id":"50556358-375e-4636-9157-bf48b20c658b","html_url":"https://github.com/vipwan/Biwen.Settings","commit_stats":null,"previous_names":["vipwan/biwen.settings"],"tags_count":31,"template":false,"template_full_name":null,"purl":"pkg:github/vipwan/Biwen.Settings","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vipwan%2FBiwen.Settings","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vipwan%2FBiwen.Settings/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vipwan%2FBiwen.Settings/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vipwan%2FBiwen.Settings/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vipwan","download_url":"https://codeload.github.com/vipwan/Biwen.Settings/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vipwan%2FBiwen.Settings/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279016352,"owners_count":26085829,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-13T02:00:06.723Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["configuration","netcore","options","setting"],"created_at":"2025-10-13T17:53:38.137Z","updated_at":"2025-10-13T17:53:43.671Z","avatar_url":"https://github.com/vipwan.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"﻿# Biwen.Settings\n\n![Nuget](https://img.shields.io/nuget/v/Biwen.Settings)\n![Nuget](https://img.shields.io/nuget/dt/Biwen.Settings)\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/vipwan/Biwen.Settings/blob/master/LICENSE.txt) \n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/vipwan/Biwen.Settings/pulls) \n\n## Biwen.Settings介绍\n\n解决程序配置项重复引用的问题,比如我们有一个GitHubSetting配置项,\n我们可以在任意地方注入GitHubSetting,系统会自动将配置项注入到我们的业务代码中,并且可以在系统中动态修改配置项,\n修改后会自动持久化到存储中,并通知集群的订阅子节点Setting配置项变更事件,清空缓存并重新加载持久层的最新项 [案例项目:BiwenSettingsMutiNodeTest](https://github.com/vipwan/BiwenSettingsMutiNodeTest)\n\n当前仓储可以是一个抽象的概念,可以任意横向实现.比如使用多个数据库负载,或者多个Redis等.只要能保证数据一致性即可\n\n## 基础案例\n\n- 一个简单例子 比如我有一个Web站点,这个Web站点有很多的负载,以及一一些小程序后台服务 , \n- 这些独立的程序(或者裂变或者使用代理转发的微服务), 都使用同一个 WX账号收账,那么这些站点或者后台服务都可以是这个WX账号配置的消费节点,\n- 这些节点可以配置一个或者多个作为主节点,当任意一个主节点修改配置项时,主动通知其他子节点更新配置,并且清空配置的缓存服务并重新加载Store!\n\n## NuGet 包\n\n- dotnet add package Biwen.Settings\n\n## 开发环境\n\n* Windows 10\n* [Visual Studio 2022](https://visualstudio.microsoft.com) / [Visual Studio Code](https://code.visualstudio.com)\n* [.NET 8.0](https://dotnet.microsoft.com/download/dotnet/8.0)\n  \n## 运行环境\n- [.NET 8.0](https://dotnet.microsoft.com/download/dotnet/8.0)\n## 使用方式\n\n[* [示例项目](https://github.com/vipwan/Biwen.Settings/tree/master/Biwen.Settings.TestWebUI)]\n\n## Easy to Use\n\n### Step 1 \n\n### 1.1 使用EntityFrameworkCore的方式\n\n- implements IBiwenSettingsDbContext\n\n```csharp\npublic class MyDbContext : DbContext, IBiwenSettingsDbContext\n{\n    public MyDbContext(DbContextOptions\u003cMyDbContext\u003e options)\n        : base(options)\n    {\n    }\n\n    public DbSet\u003cSetting\u003e Settings { get; set; }\n\n    protected override void OnModelCreating(ModelBuilder builder)\n    {\n        base.OnModelCreating(builder);\n        //根据情况自定义表名\n        builder.Entity\u003cSetting\u003e(b =\u003e\n        {\n            b.ToTable(\"Settings\");\n        });\n        //可以根据自己的情况约束存储列\n        //builder.Entity\u003cSetting\u003e().Property(x =\u003e x.SettingName).HasMaxLength(500);\n    }\n}\n```\n#### 1.1.1 Migration\n\n- Add a new Entity Framework Core migration to save your changes\n\n```bash\ndotnet ef migrations add biwenSettings\ndotnet ef database update\n```\n\n### 1.2 使用自定义SettingStore\n\n- 直接跳入step 2.2\n\n### step 2\n\n\n- 2.1 如果使用Biwen.Settings提供的EF仓储,必须注入DBContext\n\n```csharp\nbuilder.Services.AddDbContext\u003cMyDbContext\u003e(options =\u003e\n{\n    //根据您的情况使用任意EFCore支持的数据库\n    //当前使用内存数据库作为演示.生产环境务必修改!\n    options.UseInMemoryDatabase(\"BiwenSettings\");\n});\n```\n- 2.2 如果使用自定义仓储,请实现ISettingStore并修改AddBiwenSettings()\n\n```csharp\noptions.UseSettingStore\u003cT,V\u003e()\n```\n\n### step 3\n\n- AddBiwenSettings \u0026 UseBiwenSettings\n\n```csharp\nbuilder.Services.AddBiwenSettings(options =\u003e\n  {\n//ProjectId : 项目标识 用于区分不同的项目,比如:日志系统,文件系统;或者环境,比如:开发环境,测试环境,生产环境\n#if DEBUG\n    options.ProjectId = $\"Biwen.Settings.TestWebUI-{\"Development\"}\";\n#endif\n#if !DEBUG\n    options.ProjectId = $\"Biwen.Settings.TestWebUI-{\"Production\"}\";\n#endif\n\noptions.Title = \"Biwen.Settings\";\n//路由地址 ,http://..../system/settings\noptions.Route = \"system/settings\";\n//授权规则\noptions.PermissionValidator = (ctx) =\u003e true;\noptions.EditorOptions.EditorOnclick = \"return confirm('Are You Sure!?');\";\noptions.EditorOptions.EdtiorConfirmButtonText = \"Submit\";\noptions.EditorOptions.EditorEditButtonText = \"Edit\";\noptions.EditorOptions.ShouldPagenation = true;\n//分页大小\noptions.EditorOptions.PageSize = 10;\n\n//开启AutoFluentValidation\noptions.AutoFluentValidationOption.Enable = true;\n\n//支持缓存提供者,默认不使用缓存\noptions.UseCacheOfNull();\n//您也可以使用Biwen.Settings提供内存缓存:MemoryCacheProvider\n//options.UseCacheOfMemory();\n//使用自定义缓存提供者\n//options.UseCache\u003cT\u003e();\n//配置当前服务为主节点\noptions.NotifyOptions.IsNotifyEnable = true;\noptions.NotifyOptions.Secret = \"Biwen.Settings.Notify\";\n//子节点配置\noptions.NotifyOptions.EndpointHosts = new[]\n{\n    \"http://localhost:5150\"\n};\n//默认提供EntityFrameworkCore持久化配置项\n//options.UseStoreOfEFCore\u003cMyDbContext\u003e();\n\n//使用JsonStore持久化配置项\noptions.UserStoreOfJsonFile(options =\u003e\n{\n    options.FormatJson = true;\n    options.JsonPath = \"systemsetting.json\";\n});\n\n\noptions.MapNotifyEndpoint = true;\noptions.ApiConventionBuilder = (builder) =\u003e\n{\n    builder.WithTags(\"BiwenSettingApi\").WithOpenApi();\n};\n        \n//自行实现的ISettingStore注册\n//options.UseSettingStore\u003cT,V\u003e()\n});\n\n//提供对`IOptions`和`IConfiguration`直接支持:\nbuilder.Configuration.AddBiwenSettingConfiguration(builder.Services, true);\n\napp.UseBiwenSettings();\n\n```\n\n### Enjoy!\n\n2024-05-17更新 提供对`IOptions`,`IOptionsSnapshot`和`IConfiguration`直接支持:\n帮助参见:[releases 2.2.0 +](https://github.com/vipwan/Biwen.Settings/releases/tag/2.2.0)\n\n2024-11-29更新 提供对`NET9.0`支持!:\n帮助参见:[releases 2.2.0 +](https://github.com/vipwan/Biwen.Settings/releases/tag/2.3.0)\n\n\n#### 支持提交更新时验证器自动验证\n![image](https://github.com/vipwan/Biwen.Settings/assets/13956765/e2663e78-61da-43f1-990d-aa970736f023)\n![image](https://github.com/vipwan/Biwen.Settings/assets/13956765/40399554-90be-4927-8b03-a516614d4bd6)\n\n```csharp\n//模拟的配置项,注意描述信息,以及默认值.初始化将以默认值为准\n[Description(\"微信配置\")]\npublic class WeChatSetting : SettingBase\n{\n    [Description(\"AppId\")]\n    public string AppId { get; set; } = \"wx1234567890\";\n\n    [Description(\"AppSecret\")]\n    public string AppSecret { get; set; } = \"1234567890\";\n\n    [Description(\"Token\")]\n    public string Token { get; set; } = \"1234567890\";\n\n    [Description(\"EncodingAESKey\")]\n    public string EncodingAESKey { get; set; } = \"1234567890\";\n    //排序\n    public override int Order =\u003e 500;\n    //定义验证器\n    public class WeChatSettingValidtor : AbstractValidator\u003cWeChatSetting\u003e\n    {\n        public WeChatSettingValidtor()\n        {\n            //验证规则\n            RuleFor(x =\u003e x.AppId).NotEmpty().Length(12, 32);\n            RuleFor(x =\u003e x.AppSecret).NotNull().NotEmpty().Length(12, 128);\n        }\n    }\n}\n\n/// \u003csummary\u003e\n/// 内置验证器的配置项,推荐使用这种方式 ,同时支持`FluentValidation`和`DataAnnotations`\n/// \u003c/summary\u003e\n[Description(\"内置验证器的配置项测试\")]\npublic class TestAutoValidSetting : ValidationSettingBase\u003cTestAutoValidSetting\u003e\n{\n    [StringLength(50, MinimumLength = 3)] // DataAnnotations支持\n    public string Name { get; set; } = \"Hello\"!;\n\n    public TestAutoValidSetting()\n    {\n        //构造函数中添加验证规则\n        RuleFor(x =\u003e x.Name).NotEmpty().Length(8, 32);\n    }\n    override public int Order =\u003e 600;\n}\n\n//anywhere you can inject\n//View:\n//@inject WeChatSetting WeChatSetting;\n//@inject TestAutoValidSetting TestAutoValidSetting;\n\n//Service:\n//public class MyClass\n//{\n//    private readonly WeChatSetting _weChatSetting;\n//    private readonly TestAutoValidSetting _testAutoValidSetting;\n//    public MyClass(WeChatSetting weChatSetting,TestAutoValidSetting testAutoValidSetting)\n//    {\n//        _weChatSetting = weChatSetting;\n//        _testAutoValidSetting = testAutoValidSetting;\n//    }\n//}\n\n```\n\n#### More\n\n- INotify订阅配置变更\n\n```csharp\npublic class WeChatSettingNotify : INotify\u003cWeChatSetting\u003e\n{\n    private readonly ILogger\u003cWeChatSettingNotify\u003e _logger;\n\n    public WeChatSettingNotify(ILogger\u003cWeChatSettingNotify\u003e logger)\n    {\n        _logger = logger;\n    }\n\n    public async Task Notify(WeChatSetting setting)\n    {\n        _logger.LogInformation(\"微信配置发生变更!\");\n        await Task.CompletedTask;\n    }\n}\n```\n\n- Minimal API\n\n```csharp\n\n//all settings\n//GET ~/{RoutePrefix}/all\n\n//get setting by full type name\n//GET ~/{RoutePrefix}/get/{id}\n\n//update setting by full type name\n//POST ~/{RoutePrefix}/set/{id}\n//body:\n//{\n//    \"PropertyName1\": \"...\",\n//    \"PropertyName2\": \"...\",\n//    \"PropertyName3\": \"...\"\n//}\n\n```\n\n\n## License \n- MIT\n\n## 联系我\n- QQ:552175420\n- Email: vipwan#sina.com\n\n## 项目地址\n\n- [GitHub][(https://github.com/vipwan)](https://github.com/vipwan/Biwen.Settings)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvipwan%2Fbiwen.settings","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvipwan%2Fbiwen.settings","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvipwan%2Fbiwen.settings/lists"}