百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

MicroRuleEngine

> 编程语言
开源

用于动态评估即时编译的业务规则的 .Net 规则引擎。

187 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

用于动态评估即时编译的业务规则的 .Net 规则引擎。

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:

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);
		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:

csharp

	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:

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

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

csharp
	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 TargetValue:

csharp
	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:

…

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

csharp
	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 #NOW keyword. The member must be a DataTime or DateTime?, and the target value must be a string in the form :#NOW+90D (The sign can be plus or minus, but must be given. The Suffix can be 'S' for Seconds, M for Minutes, H for Hours, D for Days, or Y for Years. The number must be an integer.)

examples:

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

OrderDate must be within the last 90 minutes.

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

ExpirationDate must be within the next year.

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

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言