Biwen.Settings 1.3.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Biwen.Settings --version 1.3.2                
NuGet\Install-Package Biwen.Settings -Version 1.3.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Biwen.Settings" Version="1.3.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Biwen.Settings --version 1.3.2                
#r "nuget: Biwen.Settings, 1.3.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Biwen.Settings as a Cake Addin
#addin nuget:?package=Biwen.Settings&version=1.3.2

// Install Biwen.Settings as a Cake Tool
#tool nuget:?package=Biwen.Settings&version=1.3.2                

Biwen.Settings

image

开源动机

  • 由于当前有好几个单体项目,且每个小项目都有比较多的配置项,所以把这个模块抽离出来打包到Nuget共用.
  • 每个项目以及模块可以在任意想要动态配置的地方定义Setting, 系统会直接注入到自己的业务代码中且存储到持久层 所见即所得,有部分用户有这样的需求.

NuGet 包

  • dotnet add package Biwen.Settings --version 1.3.1

开发环境

运行环境

使用方式

[* 示例项目]

Easy to Use

Step 1

1.1 使用EntityFrameworkCore的方式

  • implements IBiwenSettingsDbContext

    public class MyDbContext : DbContext, IBiwenSettingsDbContext
    {
        public MyDbContext(DbContextOptions<MyDbContext> options)
            : base(options)
        {
        }

        public DbSet<Setting> Settings { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            //根据情况自定义表名
            builder.Entity<Setting>(b =>
            {
                b.ToTable("Settings");
            });
            //可以根据自己的情况约束存储列
            //builder.Entity<Setting>().HasKey(x => x.SettingName);
            //builder.Entity<Setting>().Property(x => x.SettingName).HasMaxLength(500);
        }
    }

1.1.1 Migration
  • Add a new Entity Framework Core migration to save your changes
dotnet ef migrations add biwenSettings
dotnet ef database update

1.2 使用自定义SettingManager

  • 直接跳入step 2.2

step 2

  • 2.1 如果使用Biwen.Settings提供的EF仓储,必须注入DBContext

      builder.Services.AddDbContext<MyDbContext>(options =>
      {
          //根据您的情况使用任意EFCore支持的数据库
          //当前使用内存数据库作为演示.生产环境务必修改!
          options.UseInMemoryDatabase("BiwenSettings");
      });
    
    
  • 2.2 如果使用自定义仓储,请实现ISettingManager并修改AddBiwenSettings()

  • 
        options.UseSettingManager<T,V>()
    
    

step 3

  • AddBiwenSettings & UseBiwenSettings

    builder.Services.AddBiwenSettings(options =>
    {
//ProjectId : 项目标识 用于区分不同的项目,比如:日志系统,文件系统;或者环境,比如:开发环境,测试环境,生产环境
#if DEBUG
    options.ProjectId = $"Biwen.Settings.TestWebUI-{"Development"}";
#endif
#if !DEBUG
    options.ProjectId = $"Biwen.Settings.TestWebUI-{"Production"}";
#endif
        //自定义布局
        options.Layout = "~/Views/Shared/_Layout.cshtml";
        options.Title = "Biwen.Settings";
        //路由地址 ,http://..../system/settings
        options.Route = "system/settings";
        //授权规则
        options.HasPermission = (ctx) => true;
        options.EditorOption.EditorOnclick = "return confirm('Are You Sure!?');";
        options.EditorOption.EdtiorConfirmButtonText = "Submit";
        options.EditorOption.EditorEditButtonText = "Edit";
        options.EditorOption.ShouldPagenation = true;

        //开启AutoFluentValidation
        options.AutoFluentValidationOption.Enable = true;

        //支持缓存提供者,默认不使用缓存
        options.UseCacheOfNull();
        //您也可以使用Biwen.Settings提供内存缓存:MemoryCacheProvider
        //options.UseCacheOfMemory();
        //使用自定义缓存提供者
        //options.UseCache<T>();

        //默认提供EntityFrameworkCore持久化配置项 dbContextType必须配置
        //options.UseSettingManagerOfEFCore(options =>
        //{
        //    options.DbContextType = typeof(MyDbContext);
        //});
        //使用JsonStore持久化配置项
        options.UserSettingManagerOfJsonStore(options =>
        {
            options.FormatJson = true;
            options.JsonPath = "systemsetting.json";
        });
        
        //自行实现的ISettingManager注册
        //options.UseSettingManager<T,V>()
    });

   //...............

   app.UseBiwenSettings();

Enjoy!

支持提交更新时验证器自动验证

image

    //模拟的配置项,注意描述信息,以及默认值.初始化将以默认值为准
    [Description("微信配置")]
    public class WeChatSetting : SettingBase
    {
        [Description("AppId")]
        public string AppId { get; set; } = "wx1234567890";

        [Description("AppSecret")]
        public string AppSecret { get; set; } = "1234567890";

        [Description("Token")]
        public string Token { get; set; } = "1234567890";

        [Description("EncodingAESKey")]
        public string EncodingAESKey { get; set; } = "1234567890";
        //排序
        public override int Order => 500;
        //定义验证器
        public class WeChatSettingValidtor : AbstractValidator<WeChatSetting>
        {
            public WeChatSettingValidtor()
            {
                //验证规则
                RuleFor(x => x.AppId).NotEmpty().Length(12, 32);
                RuleFor(x => x.AppSecret).NotNull().NotEmpty().Length(12, 128);
            }
        }
    }

    /// <summary>
    /// 内置验证器的配置项,推荐使用这种方式
    /// </summary>
    [Description("内置验证器的配置项测试")]
    public class TestAutoValidSetting : ValidationSettingBase<TestAutoValidSetting>
    {

        public string Name { get; set; } = "Hello"!;

        public TestAutoValidSetting()
        {
            //构造函数中添加验证规则
            RuleFor(x => x.Name).NotEmpty().Length(8, 32);
        }
        override public int Order => 600;
    }

    //anywhere you can inject
    //View:
    //@inject WeChatSetting WeChatSetting;
    //@inject TestAutoValidSetting TestAutoValidSetting;

    //Service:
    //public class MyClass
    //{
    //    private readonly WeChatSetting _weChatSetting;
    //    private readonly TestAutoValidSetting _testAutoValidSetting;
    //    public MyClass(WeChatSetting weChatSetting,TestAutoValidSetting testAutoValidSetting)
    //    {
    //        _weChatSetting = weChatSetting;
    //        _testAutoValidSetting = testAutoValidSetting;
    //    }
    //}

More
  • INotify订阅配置变更

        public class WeChatSettingNotify : INotify<WeChatSetting>
        {
            private readonly ILogger<WeChatSettingNotify> _logger;

            public WeChatSettingNotify(ILogger<WeChatSettingNotify> logger)
            {
                _logger = logger;
            }

            public async Task Notify(WeChatSetting setting)
            {
                _logger.LogInformation("微信配置发生变更!");
                await Task.CompletedTask;
            }
        }

License

  • MIT

联系我

  • QQ:552175420
  • Email: vipwan#sina.com

项目地址

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Biwen.Settings:

Package Downloads
Biwen.Settings.OC

Biwen.Settings.OC ,NET8+ 应用配置项管理模块

Biwen.Settings.Caching.Garnet

Biwen.Settings ,NET8+ 应用配置项管理模块

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.6.2 73 7/23/2024
2.2.5 105 6/10/2024
2.2.4 96 5/22/2024
2.1.1 104 4/8/2024
2.1.0 125 4/1/2024
2.0.3 130 3/5/2024
2.0.2 184 1/5/2024
2.0.1 139 12/28/2023
2.0.0 189 11/16/2023
2.0.0-preview1 84 9/25/2023
1.4.0 115 9/18/2023
1.3.3 152 9/14/2023
1.3.2 144 9/7/2023
1.2.2 134 9/1/2023

提供对OrchardCore的支持