Muonroi.RuleEngine.NRules
1.0.0-alpha.16
dotnet add package Muonroi.RuleEngine.NRules --version 1.0.0-alpha.16
NuGet\Install-Package Muonroi.RuleEngine.NRules -Version 1.0.0-alpha.16
<PackageReference Include="Muonroi.RuleEngine.NRules" Version="1.0.0-alpha.16" />
<PackageVersion Include="Muonroi.RuleEngine.NRules" Version="1.0.0-alpha.16" />
<PackageReference Include="Muonroi.RuleEngine.NRules" />
paket add Muonroi.RuleEngine.NRules --version 1.0.0-alpha.16
#r "nuget: Muonroi.RuleEngine.NRules, 1.0.0-alpha.16"
#:package Muonroi.RuleEngine.NRules@1.0.0-alpha.16
#addin nuget:?package=Muonroi.RuleEngine.NRules&version=1.0.0-alpha.16&prerelease
#tool nuget:?package=Muonroi.RuleEngine.NRules&version=1.0.0-alpha.16&prerelease
Muonroi.RuleEngine.NRules
[FROZEN] NRules integration for the Muonroi rule-engine surface — no active development. Migrate to
Muonroi.RuleEngine.Runtime.
This package wired the NRules forward-chaining rule engine into the Muonroi building-block ecosystem. It provides a singleton NRulesEngine that scans assemblies for NRules Rule subclasses, respects per-rule enable/version configuration via RuleOptions, and optionally exposes an HTTP management surface through NRulesController.
This package is frozen. All public types carry [Obsolete]. For new projects use Muonroi.RuleEngine.Runtime, which supersedes it with the full Abstractions v1.7+ Saga-pattern execution model.
Installation
dotnet add package Muonroi.RuleEngine.NRules --prerelease
Quick Start
The following is grounded in samples/Quickstart.RuleEngine.NRules/src/Quickstart.RuleEngine.NRules.Api/Program.cs.
1. Register the engine
// Program.cs
builder.Services.AddNRulesEngine(
configure: options => builder.Configuration.GetSection("NRules").Bind(options),
assemblies: typeof(Program).Assembly); // scan this assembly for Rule subclasses
builder.Services.AddNRulesWeb(); // optional: registers NRulesController as an MVC part
// NRulesController requires IMDateTimeService from Muonroi.Core.
builder.Services.AddSingleton<IMDateTimeService, MDateTimeService>();
2. Define a rule
using NRules.Fluent.Dsl;
using Muonroi.RuleEngine.NRules;
[Rule("HighValueOrderDiscount", "1.0")]
public sealed class HighValueOrderDiscountRule : Rule
{
public override void Define()
{
Order order = null!;
When()
.Match(() => order, o => o.Amount > 1000m);
Then()
.Do(_ => order.ApplyDiscount(0.10m));
}
}
3. Fire rules
// Inject NRulesEngine and fire facts against it.
var order = new Order { Amount = 1500m };
nRulesEngine.Fire(order);
4. appsettings.json — enable/disable or pin a version
{
"NRules": {
"Rules": {
"HighValueOrderDiscount": {
"Enabled": true,
"Version": "1.0"
}
}
}
}
Features
- Assembly scanning — locates every
Rulesubclass in the supplied assemblies at startup and compiles them into a reusableISessionFactory. - Per-rule toggling — set
Enabled: falseinRuleOptions.Rulesto exclude a rule without removing the class. - Version pinning —
[Rule(name, version)]+RuleConfig.Versionlets you deploy multiple versions of a rule and activate only the one you need. - Duplicate detection — throws
MInternalExceptionat startup if two enabled rules resolve to the same name, preventing silent conflicts. - HTTP surface —
AddNRulesWeb()registersNRulesController(underapi/v1/rule-engine/nrules) as an MVC application part for runtime inspection. - Manifest contributor — registers
NRulesManifestContributorwith the Muonroi UI-engine manifest pipeline.
Configuration
AddNRulesEngine accepts an Action<RuleOptions> configure callback and one or more Assembly arguments.
RuleOptions
| Property | Type | Description |
|---|---|---|
Rules |
Dictionary<string, RuleConfig> |
Per-rule configuration keyed by rule name (case-insensitive). |
RuleConfig
| Property | Type | Default | Description |
|---|---|---|---|
Enabled |
bool |
true |
Set to false to exclude this rule from the compiled session factory. |
Version |
string? |
null |
If set, only the rule whose [Rule(..., version)] matches this value is compiled. |
API Reference
| Type | Purpose |
|---|---|
NRulesEngine |
Singleton rule executor. Scans assemblies, filters by RuleOptions, compiles via NRules RuleCompiler, and exposes Fire(params object[] facts). |
RuleAttribute |
Class-level attribute [Rule(name, version)] that gives a rule a stable identity for RuleOptions configuration. |
RuleOptions |
Options root bound from configuration; holds the Rules dictionary. |
RuleConfig |
Per-rule settings: Enabled and Version. |
ServiceCollectionExtensions |
AddNRulesEngine(configure, assemblies) — registers NRulesEngine as singleton. AddNRulesWeb() — adds NRulesController as MVC application part. |
Samples
- Quickstart.RuleEngine.NRules — demonstrates
AddNRulesEngine+AddNRulesWebwith aHighValueOrderDiscountRulefact-matching order discounts.
Compatibility
- Target framework:
net8.0 - License: Apache-2.0 (OSS)
Related Packages
Muonroi.RuleEngine.Runtime— recommended replacement; supersedes this package with full Saga-pattern andExecutionModesupport.Muonroi.RuleEngine.Abstractions— shared contracts consumed by both this package andMuonroi.RuleEngine.Runtime.
License
Apache-2.0. See LICENSE-APACHE.
Deprecation notice: this package is frozen and receives no new features or bug fixes. Migrate to
Muonroi.RuleEngine.Runtime.
| Product | Versions 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.3)
- Microsoft.Extensions.Options (>= 10.0.3)
- Muonroi.Core.Abstractions (>= 1.0.0-alpha.16)
- Muonroi.RuleEngine.Abstractions (>= 1.0.0-alpha.16)
- NRules (>= 1.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
v1.7.0: Updated to use new Abstractions v1.7.0 with Saga Pattern support. Compatible with ExecutionMode enum for flexible execution strategies.