Galosys.Foundation.RulesEngine.EntityFrameworkCore
26.10.29.1
dotnet add package Galosys.Foundation.RulesEngine.EntityFrameworkCore --version 26.10.29.1
NuGet\Install-Package Galosys.Foundation.RulesEngine.EntityFrameworkCore -Version 26.10.29.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.10.29.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.10.29.1" />
<PackageReference Include="Galosys.Foundation.RulesEngine.EntityFrameworkCore" />
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.10.29.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Galosys.Foundation.RulesEngine.EntityFrameworkCore, 26.10.29.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.10.29.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.10.29.1
#tool nuget:?package=Galosys.Foundation.RulesEngine.EntityFrameworkCore&version=26.10.29.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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 | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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.
-
net10.0
- Galosys.Foundation.RulesEngine (>= 26.10.29.1)
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.10.29.1 | 105 | 10/29/2026 |
| 26.10.20.1 | 93 | 10/20/2026 |
| 26.10.16.2 | 100 | 10/16/2026 |
| 26.10.16.1 | 81 | 10/16/2026 |
| 26.10.9.1 | 102 | 10/9/2026 |
| 26.10.8.1 | 96 | 10/8/2026 |
| 26.9.23.1 | 113 | 9/23/2026 |
| 26.9.16.1 | 126 | 9/16/2026 |
| 26.9.15.1 | 105 | 9/15/2026 |
| 26.9.14.1 | 104 | 9/15/2026 |
| 26.8.29.1 | 120 | 8/31/2026 |
| 26.8.26.1 | 112 | 8/26/2026 |
| 26.8.23.1 | 126 | 8/23/2026 |
| 26.8.21.1 | 122 | 8/21/2026 |
| 26.8.20.1 | 128 | 8/20/2026 |
| 26.8.18.1 | 125 | 8/18/2026 |
| 26.8.17.1 | 123 | 8/17/2026 |
| 26.8.13.2 | 122 | 8/13/2026 |
| 26.8.13.1 | 122 | 8/13/2026 |
| 26.8.12.2 | 112 | 8/12/2026 |
Loading failed