Code Monkey home page Code Monkey logo

microruleengine's Introduction

MicroRuleEngine is a single file rule engine

Fork of Fork Note: My CI process requires a Nuget Package. Forking to get this branch in NUGet as short term fix

Fork Note:

On this fork, I've added a new API, since the original is rather unwieldy. With the new API, the Rule defined below in ConditionalLogic() can be written as:

Rule rule = Rule.Create("Customer.LastName", mreOperator.Equal, "Doe")
         & (Rule.Create("Customer.FirstName", mreOperator.Equal, "John") | Rule.Create("Customer.FirstName", mreOperator.Equal, "Jane"));

NewApi.c in the UnitTest Project contains all the original unit tests re-written with the new API.

(end fork note)

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 2 hundred lines. 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. Below is 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);
		}

You'll want to look at the Test project but just to give another snippet here is an example of Conditional logic in a test

		[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 = "Jane", 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);
		}

How do I store Rules?

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

microruleengine's People

Contributors

jamescurran avatar mddos avatar runxc1 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.