by runxc1

A .Net Rule Engine for dynamically evaluating business rules compiled on the fly.

130 Stars 61 Forks Last release: Not found MIT License 49 Commits 0 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

MicroRuleEngine is a single file rule engine


Rule Engine for dynamically evaluating business rules compiled on the fly. If you have business rules that you don't want to hard code then the
is your friend. The rule engine is easy to groc and is only about 200 lines of code. Under the covers it creates a
expression tree that is compiled so even if your business rules get pretty large or you run them against thousands of items the performance should still compare nicely with a hard coded solution.

How To Install It?

Drop the code file into your app and change it as you wish.

How Do You Use It?

The best examples of how to use the

MicroRuleEngine (MRE)
can be found in the Test project included in the Solution.

One of the tests: ```csharp [TestMethod] public void ChildProperties() { Order order = this.GetOrder(); Rule rule = new Rule() { MemberName = "Customer.Country.CountryCode", Operator = System.Linq.Expressions.ExpressionType.Equal.ToString("g"), TargetValue = "AUS" }; MRE engine = new MRE(); var compiledRule = engine.CompileRule(rule); bool passes = compiledRule(order); Assert.IsTrue(passes);

    order.Customer.Country.CountryCode = "USA";
    passes = compiledRule(order);
What Kinds of Rules can I express
In addition to comparative operators such as `Equals`, `GreaterThan`, `LessThan` etc.   You can also call methods on the object that return a `boolean` value such as `Contains` or `StartsWith` on a string. In addition to comparative operators, additional operators such as `IsMatch` or `IsInteger` have been added and demonstrates how you could edit the code to add your own operator(s). Rules can also be `AND`'d or `OR`'d together:

Rule rule =
    Rule.Create("Customer.LastName", "Contains", "Do")
    & (
        Rule.Create("Customer.FirstName", "StartsWith", "Jo")
        | Rule.Create("Customer.FirstName", "StartsWith", "Bob")

You can reference member properties which are

by their index:
    Rule rule = Rule.Create("Items[1].Cost", mreOperator.GreaterThanOrEqual, "5.25");

Similarly, you can reference element of a string- or integer-keyed dictionary:

    Rule rule = Rule.Create("Items['myKey'].Cost", mreOperator.GreaterThanOrEqual, "5.25");

You can also compare an object to itself indicated by the

at the beginning of the
    Rule rule = Rule.Create("Items[1].Cost", mreOperator.Equal, "*.Items[0].Cost");

There are a lot of examples in the test cases but, here is another snippet demonstrating nested

logic: ```csharp [TestMethod] public void ConditionalLogic() { Order order = this.GetOrder(); Rule rule = new Rule() { Operator = "AndAlso", Rules = new List() { new Rule() { MemberName = "Customer.LastName", TargetValue = "Doe", Operator = "Equal"}, new Rule() { Operator = "Or", Rules = new List() { new Rule(){ MemberName = "Customer.FirstName", TargetValue = "John", Operator = "Equal"}, new Rule(){ MemberName = "Customer.FirstName", TargetValue = "Judy", Operator = "Equal"} } } } }; MRE engine = new MRE(); var fakeName = engine.CompileRule(rule); bool passes = fakeName(order); Assert.IsTrue(passes);
    order.Customer.FirstName = "Philip";
    passes = fakeName(order);
If you need to run your comparison against an ADO.NET DataSet you can also do that as well:
    var dr = GetDataRow();
    // (int) dr["Column2"] == 123 &&  (string) dr["Column1"] == "Test"
    Rule rule = DataRule.Create("Column2", mreOperator.Equal, "123") & DataRule.Create("Column1", mreOperator.Equal, "Test");

#NOW and time-based rules.

You can test a property for a time range from the current time, using the special case

keyword. The member must be a
, and the target value must be a string in the form :
(The sign can be plus or minus, but must be given. The Suffix can be 'S' for Seconds,
for Minutes,
for Hours,
for Days, or
for Years. The number must be an integer.)


Rule rule = Rule.Create("OrderDate", mreOperator.GreaterThanOrEqual, "#NOW-90M");

must be within the last 90 minutes.

Rule rule = Rule.Create("ExpirationDate", mreOperator.LessThanOrEqual, "#NOW+1Y");

must be within the next year.

How Can I Store Rules?


Class is just a POCO so you can store your rules as serialized
, etc.

Forked many times and now updated to pull in a lot of the great work done by jamescurran, nazimkov and others that help improve the API

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.