🔄 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:
🔗 https://github.com/KanaiyaKatarmal/RulesEngineSample
💬 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.