runxc1 / MicroRuleEngine

A .Net Rule Engine for dynamically evaluating business rules compiled on the fly.
MIT License
185 stars 73 forks source link

Fluent interface #22

Open jamescurran opened 4 years ago

jamescurran commented 4 years ago

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.

runxc1 commented 4 years ago

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.

jamescurran commented 4 years ago

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;
RubberChickenParadise commented 4 years ago

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