Muonroi.RuleEngine.Abstractions
2.0.2
dotnet add package Muonroi.RuleEngine.Abstractions --version 2.0.2
NuGet\Install-Package Muonroi.RuleEngine.Abstractions -Version 2.0.2
<PackageReference Include="Muonroi.RuleEngine.Abstractions" Version="2.0.2" />
<PackageVersion Include="Muonroi.RuleEngine.Abstractions" Version="2.0.2" />
<PackageReference Include="Muonroi.RuleEngine.Abstractions" />
paket add Muonroi.RuleEngine.Abstractions --version 2.0.2
#r "nuget: Muonroi.RuleEngine.Abstractions, 2.0.2"
#:package Muonroi.RuleEngine.Abstractions@2.0.2
#addin nuget:?package=Muonroi.RuleEngine.Abstractions&version=2.0.2
#tool nuget:?package=Muonroi.RuleEngine.Abstractions&version=2.0.2
Muonroi.RuleEngine.Abstractions
Core contracts for evaluating complex business logic, dynamic decision tables, and saga-based compensations.
Overview
The Muonroi.RuleEngine.Abstractions package provides the foundational contracts for the Muonroi Rule Engine. In complex enterprise applications—especially multi-tenant systems—business rules (e.g., pricing calculations, eligibility checks, validation logic) change rapidly and often differ per tenant. Hardcoding these rules in application logic leads to brittle systems.
This package defines the interfaces required to decouple business logic from application flow. It supports compiled C# rules, dynamic Decision Tables (DMN-lite), and Saga-style compensation strategies. By standardizing around an IMRuleOrchestrator, rules can be evaluated centrally with full observability, tenant isolation, and predictable execution modes.
Features
- Core Rule Contracts: Defines
IRuleandICompensatableRulefor implementing discrete units of business logic with optional rollback capabilities. - Rule Orchestration: The
IMRuleOrchestratorinterface defines how collections of rules are executed against a specificIRuleContextandFactBag. - Execution Modes: Supports robust error handling strategies via the
ExecutionModeenum (AllOrNothing,BestEffort,CompensateOnFailure). - Dynamic Authoring: Provides metadata attributes (
[MRuleContextDescription],[MRuleCatalogEntry]) and theIRuleAuthoringManifestProviderto allow UIs to dynamically generate rule-builder interfaces. - Canary Rollouts: Contracts for managing rule lifecycle, approvals (
IRuleSetApprovalService), and safe canary deployments (ICanaryRolloutService). - Hooks & Telemetry: Exposes
IHookHandlerandIRuleEventListenerfor intercepting rule execution lifecycle events.
Installation
dotnet add package Muonroi.RuleEngine.Abstractions
Quick Start
Defining a Rule Context
A Rule Context represents the state required to evaluate a set of rules. It acts as the strongly-typed payload passed through the engine.
using Muonroi.RuleEngine.Abstractions;
using Muonroi.RuleEngine.Abstractions.Authoring;
[MRuleContextDescription("Discount Context", "Context used for calculating order discounts.")]
public class OrderDiscountContext : IRuleContext
{
[MRuleFactDescription("Total Order Amount", "The total monetary value of the order before discounts.")]
public decimal TotalAmount { get; set; }
[MRuleFactDescription("Customer Tier", "The loyalty tier of the customer (e.g., Gold, Silver).")]
public string CustomerTier { get; set; } = string.Empty;
public bool IsValid() => TotalAmount >= 0;
}
Implementing a Code-Based Rule
While rules can be defined dynamically (e.g., via decision tables), you can also write compiled rules by implementing IRule.
using Muonroi.RuleEngine.Abstractions;
using System.Threading;
using System.Threading.Tasks;
[MRuleCatalogEntry("Gold Tier Discount", "Applies a 10% discount to Gold tier customers.", RuleType.Validation)]
public class GoldTierDiscountRule : IRule
{
public string Name => "GoldTierDiscount";
public string Group => "Pricing";
public Task<RuleResult> EvaluateAsync(IRuleContext context, FactBag factBag, CancellationToken cancellationToken)
{
if (context is OrderDiscountContext orderContext && orderContext.CustomerTier == "Gold")
{
// Apply discount logic here or mutate the FactBag
factBag.Set(new FactKey("DiscountPercentage", typeof(decimal)), 10m);
return Task.FromResult(RuleResult.SuccessResult());
}
return Task.FromResult(RuleResult.SkippedResult("Customer is not Gold tier."));
}
}
Saga Pattern Support (Compensatable Rules)
For operations that mutate state across distributed systems, implement ICompensatableRule. If a subsequent rule fails, the orchestrator (if configured for ExecutionMode.CompensateOnFailure) will call CompensateAsync on previously successful rules.
public class ReserveInventoryRule : ICompensatableRule
{
public string Name => "ReserveInventory";
public string Group => "OrderFulfillment";
public async Task<RuleResult> EvaluateAsync(IRuleContext context, FactBag factBag, CancellationToken token)
{
return RuleResult.SuccessResult();
}
public async Task CompensateAsync(IRuleContext context, FactBag factBag, CancellationToken token)
{
// Compensate logic
}
}
API Reference
Execution Models
IMRuleOrchestrator: The primary entry point for executing rule sets.IRuleContext: The strongly-typed data passed into the rule execution pipeline.FactBag: A thread-safe, loosely-typed dictionary for sharing transient state between rules during execution.OrchestratorResult: Contains detailed feedback about the execution.
Adapters
IContextProjector: Projects domain entities (like a Database Model) into anIRuleContext.IContextFactory: Instantiates rule contexts.
Ecosystem Combinations
+ Muonroi.RuleEngine.Core → Concrete Orchestration
The core engine binds to these abstractions, implementing IMRuleOrchestrator to evaluate the rules according to the selected ExecutionMode.
+ Muonroi.Quota.Abstractions → Execution Budgets
An implementation of IHookHandler can inject ITenantQuotaTracker to decrement a tenant's evaluation limits each time IMRuleOrchestrator.EvaluateAsync is called, throwing a QuotaExceededException if they exceed their SaaS plan.
+ Muonroi.Observability → Lifecycle Metrics
Implementing IRuleEventListener allows you to export rule execution durations, cache hits, and validation failures directly to RuleEngineTelemetryDescriptor OTel meters.
Full Rule Engine Stack
builder.Services
.AddRuleEngineCore(config)
.AddTenantContext(config)
.AddMuonroiObservability(config)
.AddInMemoryTenantQuotas();
Samples
License
Apache 2.0 — see LICENSE-APACHE.
| 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.Abstractions (>= 10.0.3)
- Muonroi.Core.Abstractions (>= 2.0.2)
- Muonroi.Quota.Abstractions (>= 2.0.2)
NuGet packages (13)
Showing the top 5 NuGet packages that depend on Muonroi.RuleEngine.Abstractions:
| Package | Downloads |
|---|---|
|
Muonroi.RuleEngine.Core
Rule Engine Core implementation for Muonroi.BuildingBlock |
|
|
Muonroi.Mediator
Mediator pattern implementation for Muonroi: command/query dispatching, pipeline behaviors, and validation integration. |
|
|
Muonroi.Data.EntityFrameworkCore
Entity Framework Core infrastructure for Muonroi: MDbContext with audit, soft-delete, multi-tenant filters, and repository base. |
|
|
Muonroi.Integration.Abstractions
Abstractions for the Muonroi Connector Registry — IServiceTaskConnector, IConnectorRegistry, IConnectorCredentialStore. |
|
|
Muonroi.RuleEngine.DecisionTable
Decision table models, converters, validators, and serializers for Muonroi Rule Engine. |
GitHub repositories
This package is not used by any popular GitHub repositories.
v1.7.0: Added Saga Pattern support (ICompensatableRule), ExecutionMode enum (AllOrNothing/BestEffort/CompensateOnFailure), and OrchestratorResult for detailed execution feedback. Added 84 comprehensive unit tests.