Galosys.Foundation.RulesEngine.EntityFrameworkCore 26.5.20.1

dotnet add package Galosys.Foundation.RulesEngine.EntityFrameworkCore --version 26.5.20.1
                    
NuGet\Install-Package Galosys.Foundation.RulesEngine.EntityFrameworkCore -Version 26.5.20.1
                    
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="Galosys.Foundation.RulesEngine.EntityFrameworkCore" Version="26.5.20.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Galosys.Foundation.RulesEngine.EntityFrameworkCore" Version="26.5.20.1" />
                    
Directory.Packages.props
<PackageReference Include="Galosys.Foundation.RulesEngine.EntityFrameworkCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Galosys.Foundation.RulesEngine.EntityFrameworkCore --version 26.5.20.1
                    
#r "nuget: Galosys.Foundation.RulesEngine.EntityFrameworkCore, 26.5.20.1"
                    
#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.
#:package Galosys.Foundation.RulesEngine.EntityFrameworkCore@26.5.20.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Galosys.Foundation.RulesEngine.EntityFrameworkCore&version=26.5.20.1
                    
Install as a Cake Addin
#tool nuget:?package=Galosys.Foundation.RulesEngine.EntityFrameworkCore&version=26.5.20.1
                    
Install as a Cake Tool

Galosys.Foundation.RulesEngine.EntityFrameworkCore

规则引擎数据库存储模块,提供 DatabaseRuleProvider<TEntity> 从数据库加载规则定义,EF 上下文由业务应用决定。

依赖: Galosys.Foundation.RulesEngine(必选)

快速开始

1. 定义实体

业务层定义规则实体,实现 IRuleEntity 约定接口:

using Galosys.Foundation.Core;
using Microsoft.Extensions.Rules;

public class RuleDefinitionEntity : FullEntity<long>, IRuleEntity
{
    public string Code { get; set; } = "";
    public string Name { get; set; } = "";
    public string RuleSet { get; set; } = "";
    public string Condition { get; set; } = "";
    public string? Output { get; set; }
    public int Priority { get; set; }
    public bool Enabled { get; set; } = true;

    // 扩展字段(不映射到 IRuleEntity,需自定义 Provider 映射)
    public string? GroupCode { get; set; }
    public RuleStatus Status { get; set; } = RuleStatus.Active;
    public DateTimeOffset? EffectiveAt { get; set; }
    public DateTimeOffset? ExpireAt { get; set; }
}

2. 表结构

CREATE TABLE rule_definition (
    id              BIGINT PRIMARY KEY,
    code            VARCHAR(100) NOT NULL,
    name            VARCHAR(200) NOT NULL,
    rule_set        VARCHAR(100) NOT NULL,
    condition       TEXT NOT NULL,
    output          TEXT NULL,
    priority        INT DEFAULT 0,
    enabled         BOOLEAN DEFAULT true,
    group_code      VARCHAR(100) NULL,
    status          INT DEFAULT 3,
    effective_at    TIMESTAMP NULL,
    expire_at       TIMESTAMP NULL,
    -- FullEntity 审计字段 ...
    UNIQUE (tenant_id, code)
);

3. 注册服务

// Program.cs
services.AddRuleEngine(configuration);
services.AddDatabaseRuleProvider<RuleDefinitionEntity>();

DatabaseRuleProvider<TEntity> 通过 IRepository<TEntity, long>(Core 层接口)访问数据,自动注入到 IRuleProvider

4. 初始化规则数据

INSERT INTO rule_definition (id, code, name, rule_set, condition, output, priority, enabled) VALUES
(1, 'vip-discount', 'VIP折扣', 'discount',
 'input.VipLevel >= 2 && input.OrderAmount >= 500',
 'new { Type = "Percentage", Value = 10m }',
 100, true);

自定义映射

默认映射只包含 IRuleEntity 的 7 个字段。若有扩展字段(GroupCode/Status/EffectiveAt 等),继承 DatabaseRuleProvider<TEntity> 覆盖 MapToDefinition

public class MyRuleProvider : DatabaseRuleProvider<RuleDefinitionEntity>
{
    public MyRuleProvider(
        IRepository<RuleDefinitionEntity, long> repository,
        IMemoryCache cache,
        TimeSpan? cacheDuration = null)
        : base(repository, cache, cacheDuration) { }

    protected override RuleDefinition MapToDefinition(RuleDefinitionEntity entity)
    {
        var def = base.MapToDefinition(entity);
        def.GroupCode = entity.GroupCode;
        def.Status = entity.Status;
        def.EffectiveAt = entity.EffectiveAt;
        def.ExpireAt = entity.ExpireAt;
        return def;
    }
}

注册自定义 Provider:

services.TryAddSingleton<IRuleProvider>(sp =>
{
    var repo = sp.GetRequiredService<IRepository<RuleDefinitionEntity, long>>();
    var cache = sp.GetRequiredService<IMemoryCache>();
    return new MyRuleProvider(repo, cache, TimeSpan.FromMinutes(10));
});

缓存

  • 默认 30 分钟滑动过期
  • 可通过 AddDatabaseRuleProvider<TEntity>(TimeSpan.FromMinutes(10)) 自定义

与 JSON 配置组合

JSON 定义基础规则 + DB 定义业务规则:

services.AddRuleEngine(configuration);                    // ConfigurationRuleProvider (JSON)
services.AddDatabaseRuleProvider<RuleDefinitionEntity>(); // DatabaseRuleProvider (DB)

业务场景

场景 执行模式 文档
运费附加费叠加 AllEvaluate docs/scenarios/shipping.md
订单路由命中即停 FirstMatch docs/scenarios/routing.md
价格覆盖同组互斥 ConflictResolve + GroupExclusive docs/scenarios/pricing.md

IRuleEntity 约定接口

public interface IRuleEntity
{
    string Code { get; }
    string Name { get; }
    string RuleSet { get; }
    string Condition { get; }
    string? Output { get; }
    int Priority { get; }
    bool Enabled { get; }
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
26.5.20.1 96 5/20/2026
26.5.19.1 96 5/19/2026
26.5.18.1 89 5/18/2026
26.5.15.1 91 5/15/2026
26.5.12.3 92 5/12/2026
26.5.12.2 94 5/12/2026