Skip to content
A .Net Rule Engine for dynamically evaluating business rules compiled on the fly.
C# Batchfile
Branch: master
Clone or download
runxc1 Merge pull request #15 from aldass/master
Misc markdown and formatting changes
Latest commit 5dba2d4 Nov 8, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
MicroRuleEngine.Core.Tests Exposing the Expression used beneath Oct 13, 2019
MicroRuleEngine.Tests Coerce inputs for method calls and find available operators for an ob… Sep 17, 2019
MicroRuleEngine
Nuget updating the nuspec Sep 13, 2019
.gitignore updating the nuspec Sep 13, 2019
MicroRuleEngine.sln Coerce inputs for method calls and find available operators for an ob… Sep 17, 2019
README.md
license.txt Create license.txt May 16, 2017

README.md

MicroRuleEngine is a single file rule engine

A .Net 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 MicroRuleEngine is your friend. The rule engine is easy to groc and is only about 200 lines of code. Under the covers it creates a Linq 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:

	[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<Order>(rule);
		bool passes = compiledRule(order);
		Assert.IsTrue(passes);

		order.Customer.Country.CountryCode = "USA";
		passes = compiledRule(order);
		Assert.IsFalse(passes);
	}

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 Arrays or List<> by their index:

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

You can also compare an object to itself indicated by the *. at the beginning of the TargetValue:

	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 OR logic:

	[TestMethod]
	public void ConditionalLogic()
	{
		Order order = this.GetOrder();
		Rule rule = new Rule()
		{
			Operator = "AndAlso",
			Rules = new List<Rule>()
			{
				new Rule() { MemberName = "Customer.LastName", TargetValue = "Doe", Operator = "Equal"},
				new Rule() { 
					Operator = "Or",
					Rules = new List<Rule>() {
						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<Order>(rule);
		bool passes = fakeName(order);
		Assert.IsTrue(passes);

		order.Customer.FirstName = "Philip";
		passes = fakeName(order);
		Assert.IsFalse(passes);
	}

If you need to run your comparison against an ADO.NET DataSet you can also do that as well:

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

How Can I Store Rules?

The Rule Class is just a POCO so you can store your rules as serialized XML, JSON, 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

You can’t perform that action at this time.