Rules Engines

Rules Engines

What are Rules Engines?

Rules Engines are systems that allow a set of actions to be defined based on a set of specific conditions, these could be configured at runtime without needing to change the business logic of an application. Often, when implementing an application any business rules are coupled to those use-cases present at the time of development. However, requirements can and will change so Rules Engines allow you to create implementations that are the same regardless of any use-cases for the application.

Why should you use a Rules Engine?

Developers will create a rule-based system in their application now, even without realising it when you change the flow of an application with an if statement or a switch statement. However, this control flow is hardcoded into the system and ideally you may want to make changes to enforce new rules or even remove existing ones, but this can be difficult without impacting the functionality of an application. Implementing Rules Engines that support configuration-based rules or that consolidate these rules into one place make it easier to change those rules when they are used in an application, and it also makes it easier to test those rules with Unit Tests as conditions can be checked and the outcome can still be an if statement on the resulting outcome of a Rules Engine.

How should you use a Rules Engine?

Rules Engines will usually follow similar patterns and at their heart is the Rule, which is a business policy, it is a set of Conditions that are applied where the outcome of these will either Pass or Fail. In the case of the Microsoft Rules Engine each Rule contains a C# Lambda Expression that can be executed on Input Data to product a Boolean output and for the Rule to Succeed the expression would need to return true. These expressions will act as the Conditions such as only allowing Users of a certain Age to be able to see something or some other criteria that can be expressed in a C# Lambda expression.

Microsoft's Rules Engine can support many Rules within a Workflow that can be executed allowing you to chain Rules together, once any input has been executed against a Workflow a Collection of RulesResultTree models will be returned. The collection of RuleResultTree models will contain the Rule and a value to say if it were successful or not and as a developer you can decide whether to proceed on any or all rules passing, which can be achieved by using an All or Any LINQ expression on the resulting output.

Who can use a Rules Engine?

Rules Engines such Microsoft Rules Engines can be used by developers to implement the workflows for an application in their code and then have the rules either be contained in one place if they need that to be part of the code or fully configurable with a JSON based configuration that can even define a workflow itself. This flexibility could allow for anyone to be able to configure the Rules and Conditions for an application without needing to change the application at all or if as part of the application without affecting the rest of the application. This is especially useful if your domain rules change on a regular basis, you may be responding to regulatory changes than means a rule needs to be implemented swiftly or you may just have something you want to be able to easily change in the future when requirements change.

Where can you learn more about Rules Engines?

You can check out the article by Joseph Gefroh on How to Design Software - Rules Engines or you can check out Microsoft's Rules Engine. You can also check out the Talk on Rules Engine with .NET, which goes over what is a Rules Engine along with how to leverage Microsoft's Rule with .NET including a Demo that you can download and try yourself from GitHub.