MicroRuleEngine icon indicating copy to clipboard operation
MicroRuleEngine copied to clipboard

Fluent interface

Open jamescurran opened this issue 4 years ago • 3 comments

I've been working on a alternate fluent/expression interface for creating rules. Basically, what you'd now write as

Rule rule = Rule.Create("Status", mreOperator.Equal, Status.Open);

you could now write as

Rule rule = Rule<Order>.Create(x=>x.Status).Equals(Status.Open);

By specifying the member as a lambda function, it verifies at coding time that the property exists on the source object, and even gives you Intellisense so you can't misspell it. It also verifies that the target value is the right type. (And it produces standard Rule objects, so you can mix & match the different syntaxes)

So, syntax question: Which do you prefer??

A) Rule rule = Rule<Order>.Create(x=>x.Status).Equals(Status.Open);

B) Rule rule = Rule<Order>.Equals(x=>x.Status, Status.Open);

One part of me prefers the shorter syntax of B), but another part prefers the way A have the operation between the member and the target.

jamescurran avatar Apr 03 '20 03:04 jamescurran

So I don't ever write the rules in code but instead have more of a query builder web UI that I use to build them but as far as syntax goes I'd vote foe B as it seems similar to they syntax in other libraries I've used such as that of the MongoDB QueryBuilder.

runxc1 avatar Apr 03 '20 13:04 runxc1

Funny, my other project was to create a desktop app to create Rules.

Do you have a Rules Collection class? I was thinking about something like this:

class RuleTypeCollection
       Type    Type
       Rule[]  Rules;

class RuleAssemblyCollection
         Assembly   Assembly;
         RuleTypeCollection[]   RulesForType;

class RuleCatalog
        RuleAssemblyCollection[]  RulesForAssembly;

jamescurran avatar Apr 03 '20 13:04 jamescurran

I would also go for B or change Create to Target Rule rule = Rule<Order>.Target(x=>x.Status).Equals(Status.Open);

RubberChickenParadise avatar Jul 21 '20 17:07 RubberChickenParadise