๐ Dynamic Business Logic Without Redeploys? Meet ASP .Net RulesEngine
A behavioral design pattern that defines the structure of an algorithm in a base class while allowing subclasses to override specific steps for flexible customization.
In the world of ever-changing business requirements, hardcoding business logic in your application is a recipe for technical debt.
What if you could update business rules โ like discount percentages, approval steps, or validations โ without modifying code or redeploying your app?
Enter: Rule Engines.
๐ What Is a Rule Engine?
A Rule Engine is a software component that separates business rules from application logic. Instead of writing if-else statements in C#, you define rules in a declarative format (like JSON), and the engine processes them dynamically.
This approach is perfect for scenarios where:
Logic changes frequently
Business users want control over rules
Rules need to be versioned/tested separately
One excellent open-source .NET tool for this is Microsoft RulesEngine.
๐งฉ ๐๐จ๐ฐ ๐๐ญ ๐๐จ๐ซ๐ค๐ฌ
1๏ธโฃ Input data (e.g. customer order) is passed to the Rule Engine
2๏ธโฃ The Rule Engine evaluates the defined JSON rules
3๏ธโฃ If conditions match, corresponding actions are triggered
4๏ธโฃ You receive output or results based on those actions
๐ฏ ๐๐ก๐ฒ ๐๐ฌ๐ ๐ ๐๐๐๐-๐๐๐ฌ๐๐ ๐๐ฎ๐ฅ๐ ๐๐ง๐ ๐ข๐ง๐?
โ
No redeploy required โ update rules without touching your application code
โ
Separation of concerns โ keep business logic clean and modular
โ
Easy to manage โ even non-developers can understand and edit rules
โ
Highly testable โ test rules in isolation
โ
Supports versioning โ track rule changes like config files
โ
Pluggable โ works with microservices, monoliths, and serverless apps
๐ ๏ธ Setting Up Microsoft RulesEngine in .NET
Letโs walk through a simple example where we apply a discount rule based on total order value.
๐ง 1. Install the NuGet Package
dotnet add package RulesEngine๐ 2. Define Your Rule in JSON
Create a file named discount-rules.json:
[
{
"WorkflowName": "Discount",
"Rules": [
{
"RuleName": "GiveDiscount10",
"SuccessEvent": "10",
"ErrorMessage": "One or more adjust rules failed.",
"ErrorType": "Error",
"RuleExpressionType": "LambdaExpression",
"Expression": "input1.country == \"india\" && input1.loyaltyFactor <= 2 && input1.totalPurchasesToDate >= 5000"
},
{
"RuleName": "GiveDiscount20",
"SuccessEvent": "20",
"ErrorMessage": "One or more adjust rules failed.",
"ErrorType": "Error",
"RuleExpressionType": "LambdaExpression",
"Expression": "input1.country == \"india\" && input1.loyaltyFactor >= 3 && input1.totalPurchasesToDate >= 10000"
}
]
}
]๐ฆ 3. Define Your Models
public class CustomerData
{
public string country { get; set; }
public int loyaltyFactor { get; set; }
public decimal totalPurchasesToDate { get; set; }
}โ๏ธ 4. Create the Rule Engine & Evaluate
using RulesEngine.Models;
using RulesEngine;
using System.Text.Json;
var json = File.ReadAllText("discount-rules.json");
var workflowRules = JsonConvert.DeserializeObject<Workflow[]>(json);
var re = new RulesEngine.RulesEngine(workflowRules, null);
var input = new CustomerData
{
country = "india",
loyaltyFactor = 3,
totalPurchasesToDate = 15000
};
var inputs = new RuleParameter("input1", input);
var results = await re.ExecuteAllRulesAsync("Discount", inputs);
foreach (var result in results)
{
Console.WriteLine($"Rule: {result.Rule.RuleName}, IsSuccess: {result.IsSuccess}, Event: {result.Rule.SuccessEvent}, Message: {result.ExceptionMessage}");
}โ Output:
Rule: GiveDiscount10, IsSuccess: False, Event: 10, Message: One or more adjust rules failed.
Rule: GiveDiscount20, IsSuccess: True, Event: 20, Message:โจ Benefits
๐ Change rules without code changes
๐งน Separation of concerns
๐งช Easily testable
๐ฉโ๐ผ Business-user friendly
๐ Version controllable
๐ Pro Tips
Keep your rules granular and readable
Store rules in external config (Azure Blob, DB, Git)
Validate rules in test/staging environments before production
Donโt overuse โ if logic doesnโt change frequently, keep it in code
๐ GitHub Example
๐ Full working code available at:
๐ http://sourcecode.kanaiyakatarmal.com/RulesEngine
๐ฌ Conclusion
In a world where business demands evolve faster than development cycles, Rule Engines like Microsoft RulesEngine help you stay agile, maintainable, and scalable.
Try it in your next .NET project โ and let business users own the rules.
I hope you found this guide helpful and informative.
Thanks for reading!
If you enjoyed this article, feel free to share it and follow me for more practical, developer-friendly content like this.


