Simple Business Rules
- Create/update Facts from UI
- Create/update rules for fact from UI
- Assign a rule group name to rules, to execute set of rules against a Fact
- Rest based services to execute the rules
Pending Items:
- Add more test cases
- All custom methods should be added in BusinessRules.BasicMethodsLibrary class library project
- All custom methods should be part of class "BasicMethods" and should be in namespace "M". As this is the pattern used in "Core" to identify the custom methods. So new set of methods can be added to existing class or a new partial class file can be created.
- HelperMethod attribute should be present with all custom methods for example
[HelperMethod("StringConcatenate", 2, "string, string")]
First parameter is dummy name, second is number of parameter this function accepts and third is type of parameters this method accepts.
Custom methods are useful for complicated formulas, database lookups, using data from REST APIs or any custom feature.
Such as to implement an Excel like "IF" method implement a method using following signature
T IF<T>(bool condition, T ActionIftrue, T ActionIfFalse);
BusinessRules.Console project shows a sample usage of how to execute rules via rest call. There are 2 end points to execute rules
- api/async/executerule - To execute a single rule
- api/async/executerulegroup - To execute set of rules with in a same rule group
All rules in a Rule Group should have same Facts associated.
Write rules in a similar way you would write a Linq query or regular conditions
For example, Lets assume we have fact Person
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
To write a rule condition where age is between 20 and 30
(Person.Age > 20 And Person.Age < 30)
Now to add the execution (Action) part
First assign a property to the action and its order of execution as well (because a single rule can have multiple actions)
Now lets say if above condition is true, then we have to multiple age by 2 and concatenate "Updated" in the Name.
So we will add 2 executions to rule.
First one would look like this
Property = Age, Order = 1 and rule execution should look like this
Person.Age * 2
Second would look like this
Property = Name, Order = 2 and rule execution should look like this
Person.Name + "Updated"
In below screenshot M.Multiply is custom method which accepts 2 int type parameter
For client library usage, i.e. how to call rules from client application, please see BusinessRules.Client and BusinessRules.Console project.