Skip to content

Commit 5dba2d4

Browse files
authored
Merge pull request #15 from aldass/master
Misc markdown and formatting changes
2 parents 7523630 + 8461716 commit 5dba2d4

File tree

1 file changed

+61
-66
lines changed

1 file changed

+61
-66
lines changed

README.md

Lines changed: 61 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,102 @@
11
MicroRuleEngine is a single file rule engine
22
============================================
33

4-
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
5-
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
6-
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
7-
hard coded solution.
4+
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.
85

96
How To Install It?
107
------------------
118
Drop the code file into your app and change it as you wish.
129

1310
How Do You Use It?
1411
------------------
15-
The best examples of how to use the MicroRuleEngine (MRE) can be found in the Test project included in the Solution.
16-
Below is one of the tests.
12+
The best examples of how to use the `MicroRuleEngine (MRE)` can be found in the Test project included in the Solution.
1713

14+
One of the tests:
1815
```csharp
19-
[TestMethod]
20-
public void ChildProperties()
16+
[TestMethod]
17+
public void ChildProperties()
18+
{
19+
Order order = this.GetOrder();
20+
Rule rule = new Rule()
2121
{
22-
Order order = this.GetOrder();
23-
Rule rule = new Rule()
24-
{
25-
MemberName = "Customer.Country.CountryCode",
26-
Operator = System.Linq.Expressions.ExpressionType.Equal.ToString("g"),
27-
TargetValue = "AUS"
28-
};
29-
MRE engine = new MRE();
30-
var compiledRule = engine.CompileRule<Order>(rule);
31-
bool passes = compiledRule(order);
32-
Assert.IsTrue(passes);
33-
34-
order.Customer.Country.CountryCode = "USA";
35-
passes = compiledRule(order);
36-
Assert.IsFalse(passes);
37-
}
22+
MemberName = "Customer.Country.CountryCode",
23+
Operator = System.Linq.Expressions.ExpressionType.Equal.ToString("g"),
24+
TargetValue = "AUS"
25+
};
26+
MRE engine = new MRE();
27+
var compiledRule = engine.CompileRule<Order>(rule);
28+
bool passes = compiledRule(order);
29+
Assert.IsTrue(passes);
30+
31+
order.Customer.Country.CountryCode = "USA";
32+
passes = compiledRule(order);
33+
Assert.IsFalse(passes);
34+
}
3835
```
3936

4037
What Kinds of Rules can I express
4138
--------------------------------
42-
In addition to comparative operators such as Equals, GreaterThan, LessThan etc. You can also call methods on the object that return a boolean value
43-
such as Contains or StartsWith On a string. In addition to comparative operators additional operators such as "IsMatch" or "IsInteger" have been added
44-
and demonstrates how you could edit the code and add your own operator. Rules can also be ANDed or ORed together as shown below.
45-
39+
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:
4640
```csharp
4741

48-
Rule rule = Rule.Create("Customer.LastName", "Contains", "Do")
49-
& (Rule.Create("Customer.FirstName", "StartsWith", "Jo"));
50-
42+
Rule rule =
43+
Rule.Create("Customer.LastName", "Contains", "Do")
44+
& (
45+
Rule.Create("Customer.FirstName", "StartsWith", "Jo")
46+
| Rule.Create("Customer.FirstName", "StartsWith", "Bob")
47+
);
5148
```
5249

53-
You can reference member properties which are Arrays or List<> by their index
50+
You can reference member properties which are `Arrays` or `List<>` by their index:
5451
```csharp
55-
Rule rule = Rule.Create("Items[1].Cost", mreOperator.GreaterThanOrEqual, "5.25");
52+
Rule rule = Rule.Create("Items[1].Cost", mreOperator.GreaterThanOrEqual, "5.25");
5653
```
5754

58-
You can also compare an object to itself indicated by the "*." at the beginning of the TargetValue shown below
55+
You can also compare an object to itself indicated by the `*.` at the beginning of the `TargetValue`:
5956
```csharp
60-
Rule rule = Rule.Create("Items[1].Cost", mreOperator.Equal, "*.Items[0].Cost");
57+
Rule rule = Rule.Create("Items[1].Cost", mreOperator.Equal, "*.Items[0].Cost");
6158
```
6259

63-
There are a lot of examples in the Test cases but here is another snippet demonstrating nested OR logic
64-
60+
There are a lot of examples in the test cases but, here is another snippet demonstrating nested `OR` logic:
6561
```csharp
66-
[TestMethod]
67-
public void ConditionalLogic()
62+
[TestMethod]
63+
public void ConditionalLogic()
64+
{
65+
Order order = this.GetOrder();
66+
Rule rule = new Rule()
6867
{
69-
Order order = this.GetOrder();
70-
Rule rule = new Rule()
68+
Operator = "AndAlso",
69+
Rules = new List<Rule>()
7170
{
72-
Operator = "AndAlso",
73-
Rules = new List<Rule>()
74-
{
75-
new Rule(){ MemberName = "Customer.LastName", TargetValue = "Doe", Operator = "Equal"},
76-
new Rule(){
77-
Operator = "Or",
78-
Rules = new List<Rule>(){
79-
new Rule(){ MemberName = "Customer.FirstName", TargetValue = "John", Operator = "Equal"},
80-
new Rule(){ MemberName = "Customer.FirstName", TargetValue = "Jane", Operator = "Equal"}
81-
}
71+
new Rule() { MemberName = "Customer.LastName", TargetValue = "Doe", Operator = "Equal"},
72+
new Rule() {
73+
Operator = "Or",
74+
Rules = new List<Rule>() {
75+
new Rule(){ MemberName = "Customer.FirstName", TargetValue = "John", Operator = "Equal"},
76+
new Rule(){ MemberName = "Customer.FirstName", TargetValue = "Judy", Operator = "Equal"}
8277
}
8378
}
84-
};
85-
MRE engine = new MRE();
86-
var fakeName = engine.CompileRule<Order>(rule);
87-
bool passes = fakeName(order);
88-
Assert.IsTrue(passes);
79+
}
80+
};
81+
MRE engine = new MRE();
82+
var fakeName = engine.CompileRule<Order>(rule);
83+
bool passes = fakeName(order);
84+
Assert.IsTrue(passes);
85+
86+
order.Customer.FirstName = "Philip";
87+
passes = fakeName(order);
88+
Assert.IsFalse(passes);
89+
}
8990

90-
order.Customer.FirstName = "Philip";
91-
passes = fakeName(order);
92-
Assert.IsFalse(passes);
93-
}
9491
```
9592

96-
If you need to run your comparison against an ADO.NET DataSet you can also do that as well.
97-
93+
If you need to run your comparison against an ADO.NET DataSet you can also do that as well:
9894
```csharp
99-
Rule rule = Rule.Create("Items[1].Cost", mreOperator.Equal, "*.Items[0].Cost");
95+
Rule rule = Rule.Create("Items[1].Cost", mreOperator.Equal, "*.Items[0].Cost");
10096
```
10197

102-
How do I store Rules?
98+
How Can I Store Rules?
10399
---------------------
104-
The Rule Class is just a POCO so you can store your rules as serialized XML, JSON etc.
100+
The `Rule` Class is just a **POCO** so you can store your rules as serialized `XML`, `JSON`, etc.
105101

106102
#### 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
107-

0 commit comments

Comments
 (0)