Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility w/ IConfiguration #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions MicroRuleEngine.Tests/ExampleUsage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public void IsInInput_SingleValue()
var rule = new Rule()
{
Operator = "IsInInput",
Inputs = new List<string> { "hello" }
Inputs = new List<object> { "hello" }
};

var mre = new MRE();
Expand All @@ -257,7 +257,7 @@ public void IsInInput_MultiValue()
var rule = new Rule()
{
Operator = "IsInInput",
Inputs = new List<string> { "hello", "World" }
Inputs = new List<object> { "hello", "World" }
};

var mre = new MRE();
Expand All @@ -275,7 +275,7 @@ public void IsInInput_NoExactMatch()
var rule = new Rule()
{
Operator = "IsInInput",
Inputs = new List<string> { "hello", "World" }
Inputs = new List<object> { "hello", "World" }
};

var mre = new MRE();
Expand Down
3 changes: 1 addition & 2 deletions MicroRuleEngine/MRE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -700,14 +700,13 @@ public class Rule
{
public Rule()
{
Inputs = Enumerable.Empty<object>();
}

[DataMember] public string MemberName { get; set; }
[DataMember] public string Operator { get; set; }
[DataMember] public object TargetValue { get; set; }
[DataMember] public IList<Rule> Rules { get; set; }
[DataMember] public IEnumerable<object> Inputs { get; set; }
[DataMember] public IList<object> Inputs { get; set; }


public static Rule operator |(Rule lhs, Rule rhs)
Expand Down
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Rule>(opt => Configuration.GetSection("Ruleset").Bind(opt));
}
```

Controller
```C#
public class HomeController : Controller
{
private Func<Order, bool> _compiledRule;

public HomeController(IOptions<Rule> ruleset)
{
_compiledRule = new MRE().CompileRule<Order>(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