NetRuleEngine 2.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package NetRuleEngine --version 2.2.0
                    
NuGet\Install-Package NetRuleEngine -Version 2.2.0
                    
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="NetRuleEngine" Version="2.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NetRuleEngine" Version="2.2.0" />
                    
Directory.Packages.props
<PackageReference Include="NetRuleEngine" />
                    
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 NetRuleEngine --version 2.2.0
                    
#r "nuget: NetRuleEngine, 2.2.0"
                    
#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 NetRuleEngine@2.2.0
                    
#: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=NetRuleEngine&version=2.2.0
                    
Install as a Cake Addin
#tool nuget:?package=NetRuleEngine&version=2.2.0
                    
Install as a Cake Tool

NetRuleEngine

C# simple Rule Engine. High performance object rule matching. Support various complex grouped predicates. available on nuget.

(Input) An Object + Rule(s) ⇒ (Output) Is Match

NuGet Badge Build Status Downloads

Use cases
  • Bussiness rules that are dynamicaly generated by a user interface, to dermine Yes/No conditions
  • Audience Matching
  • Dynamic Alerts detection
Common Usage Scenario
flowchart LR
    User((Rules Admin<br/>User))
    Editor[Rule<br/>Editor UI]
    DB[(Rules DB)]
    Event[Business Event<br/><i>real time</i>]
    Logic[Business Logic]
    Engine[NetRuleEngine]
    
    User -->|configures rules| Editor
    Editor -->|SAVE AS JSON| DB
    Event -->|trigger| Logic
    Logic -->|1.fetch rules| DB
    Logic -->|2.check event<br/>against rules| Engine
    Engine -->|matching rules| Logic
  • Rules Admin - Actor Role - that sets the rules on design time to identify an event or desired state.
  • Rules DB - Database to store the rules
  • Business Event - an Event that occurs on the bussines flow, and changes a state on your backend, or a standalone event that needs to be tested for rule matching. can be anything as a site Visit, transaction, UI event, Login, Registration or whatever.
  • BUSSINESS LOGIC - this is the backend that referene the NetRuleEngine package, consumes the Rules from DB, and for each Event, run the Rule matchin and acts according to the result
this solution doesnt not provide any rules editor UX or DB.
Simple usage:
    IRulesService<TestModel> engine = RulesService<TestModel>.CreateDefault();
            
    var matching = engine.GetMatchingRules(
        new TestModel { NumericField = 5 },
        new[] {
            new RulesConfig {
                Id = Guid.NewGuid(),
                RulesOperator = Rule.InterRuleOperatorType.And,
                RulesGroups = new RulesGroup[] {
                    new RulesGroup {
                        RulesOperator = Rule.InterRuleOperatorType.And,
                        // every TestModel instance with NumericField Equal to 5 will match this rule
                        Rules = new[] {
                            new Rule { 
                            ComparisonOperator = Rule.ComparisonOperatorType.Equal,
                            ComparisonValue = 5.ToString(),
                            ComparisonPredicate = nameof(TestModel.NumericField) 
                            }
                        }
                    }
                }
            }
        });
  • depenent on LazyCache to store compiled rules for best performance.
  • compiles Expression Trees into dynamic cached code to support high performance usage.
  • dependency injection ready, inject Either IRulesService<> or its dependencies.
  • as the RulesService is statefull and is dependent on cache, it must be configured as singleton on your IOC (or at least, its cache dependency must be singleton)

Rule Editor UI Example (not included in this project):
editor
Rule Config JSON Format Example:

{
    "Id": "eff37f67-9279-4fff-b2ea-9ef6f92a5de7",
    "RulesOperator": "And",
    "RulesGroups": [
        {
            "RulesOperator": "Or",
            "Rules": [
                {
                    "ComparisonPredicate": "TextField",
                    "ComparisonOperator": "StringStartsWith",
                    "ComparisonValue": "NOT MATCHING PREFIX",
                    "PredicateType": null
                },
                {
                    "ComparisonPredicate": "NumericField",
                    "ComparisonOperator": "GreaterThan",
                    "ComparisonValue": "4",
                    "PredicateType": null
                }
            ]
        },
        {
            "RulesOperator": "Or",
            "Rules": [
                {
                    "ComparisonPredicate": "TextField",
                    "ComparisonOperator": "StringStartsWith",
                    "ComparisonValue": "SomePrefix",
                    "PredicateType": null
                },
                {
                    "ComparisonPredicate": "NumericField",
                    "ComparisonOperator": "GreaterThan",
                    "ComparisonValue": "55",
                    "PredicateType": null
                }
            ]
        }
    ]
}

this example represents a single rule consists on 2 groups with relation of AND (which means object must match both groups), on each group, at least 1 rule should match as both have OR operator and both have 2 criterias rules.


Features:

  • composite objects
  • enums
  • string
  • numbers
  • datetime
  • Dictionaries
  • collections

and many more. See units test for full usage scenarios.

use RulePredicatePropertyAttribute to name the property to be used as a predicate, otherwise the property name will be used as predicate name.


 [RulePredicateProperty("first_name")]
 public string FirstName { get; set; }

textField will be used as predicate name instead of the property name, and you will be able to rename the property name (FirstName) without breaking the rules.
as your rule will be written as:

{
    "ComparisonPredicate": "first_name",
    "ComparisonOperator": "Equal",
    "ComparisonValue": "John"
}

for example see TestModelWithComparisonPredicateNameAttribute for more examples

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
3.0.0 116 7/16/2025
2.2.0 72 7/11/2025
2.1.0 75 7/11/2025
2.0.0 137 7/10/2025
1.0.2 513 1/15/2021
1.0.1 427 1/15/2021
1.0.0 426 1/15/2021