Implement simple rules engine using DLR and IronPython.net

Here we have an application where we take some details from the customer who wants to apply for a loan. When the customer fills in a “LoanApplication“,we then evaluate it against some eligibility rules and decide whether to further process the application or not.

In the inherited classes we would override the method and fill in the business rules.

PublicOverridesSubBuildEligibilityRules()
EligibilityRules.Add(Function(loanApp) CustomerMustBeOver30(), "Customer must be over 30.")
End Sub

PrivateFunction CustomerMustBeOver30() AsBoolean

ReturnLoanApplication.Customer.Age > 30
End Function

We then had a runner that ran the rules and if any of them failed, we would reject the application.

These rules were ever-changing and we needed a better way to change the rules without deploying the system What we needed was a way to store these rules as string in the database. Then in c# runtime load the rules, parse them and finally execute them. (you get the idea!)

The solution we came up with was to use IronPython and dlr to execute it. So we would store the rule in the database as

This proved to be a very effective technique, as now we could give users “Enterprise library” like interface to construct these rules “safely” on the Fly and store them in the database. On the other side we would read these rules and simply execute them.