diff --git a/MicroRuleEngine.Tests/ExampleUsage.cs b/MicroRuleEngine.Tests/ExampleUsage.cs index 7082d0e..1a905c4 100644 --- a/MicroRuleEngine.Tests/ExampleUsage.cs +++ b/MicroRuleEngine.Tests/ExampleUsage.cs @@ -239,7 +239,7 @@ public void IsInInput_SingleValue() var rule = new Rule() { Operator = "IsInInput", - Inputs = new List { "hello" } + Inputs = new List { "hello" } }; var mre = new MRE(); @@ -257,7 +257,7 @@ public void IsInInput_MultiValue() var rule = new Rule() { Operator = "IsInInput", - Inputs = new List { "hello", "World" } + Inputs = new List { "hello", "World" } }; var mre = new MRE(); @@ -275,7 +275,7 @@ public void IsInInput_NoExactMatch() var rule = new Rule() { Operator = "IsInInput", - Inputs = new List { "hello", "World" } + Inputs = new List { "hello", "World" } }; var mre = new MRE(); diff --git a/MicroRuleEngine/MRE.cs b/MicroRuleEngine/MRE.cs index 8117313..ca03534 100644 --- a/MicroRuleEngine/MRE.cs +++ b/MicroRuleEngine/MRE.cs @@ -700,14 +700,13 @@ public class Rule { public Rule() { - Inputs = Enumerable.Empty(); } [DataMember] public string MemberName { get; set; } [DataMember] public string Operator { get; set; } [DataMember] public object TargetValue { get; set; } [DataMember] public IList Rules { get; set; } - [DataMember] public IEnumerable Inputs { get; set; } + [DataMember] public IList Inputs { get; set; } public static Rule operator |(Rule lhs, Rule rhs) diff --git a/README.md b/README.md index 1ecd1d4..8939107 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,59 @@ examples: How Can I Store Rules? --------------------- + +If using .NET Core it is possible to use the options pattern to directly load rules from the application JSON configuration into the rule class. + +AppSettings +```JSON +"Ruleset": { + "Operator": "Or", + "Rules": [ + { + "MemberName": "Customer.FirstName", + "Operator": "Equal", + "TargetValue": "John" + }, + { + "MemberName": "Customer.FirstName", + "Operator": "Equal", + "TargetValue": "Judy" + } + ] +} +``` + +Startup +```C# +public void ConfigureServices(IServiceCollection services) +{ + services.Configure(opt => Configuration.GetSection("Ruleset").Bind(opt)); +} +``` + +Controller +```C# +public class HomeController : Controller +{ + private Func _compiledRule; + + public HomeController(IOptions ruleset) + { + _compiledRule = new MRE().CompileRule(ruleset.Value); + } + + public IActionResult Index() + { + var order = this.GetOrder(); + + if (_compiledRule(order)) + return View("SpecialOffers"); + else + return View(); + } +} +``` + The `Rule` Class is just a **POCO** so you can store your rules as serialized `XML`, `JSON`, etc. #### Forked many times and now updated to pull in a lot of the great work done by jamescurran, nazimkov and others that help improve the API