This project is read-only.

Serialize and deserialize rules

Sep 28, 2016 at 2:41 PM
Hi,

Thanks for the Rules Engine, it is great.

Is there a way to serialize the something like this -

FluentBuilder builder = new Fluent.FluentBuilder();
        builder.For<Person>()
            .Setup(p => p.Age)
            .MustBeLessThanOrEqualTo(25)
            .Setup(p => p.Name)
            .MustEqual("Dave");
My goal is to save rules to a database and load as needed.

I have tried with NewtonSoft, but no success. I added a custom contract resolver to serialize private fields, but then I hit an infinite loop problem.

Regards,

B
Sep 29, 2016 at 2:15 AM
Hi Bryan,

Unfortunately no, the rules cannot be serialized...

You would have to do that yourself
i.e.

1. Come up with a Model that is serializable
2. Come up with a Translator that translate the Model into Rules that you feed into the engine.

E.g. Model (in JSON)
{
"RuleGroup" : {
"Object" : "Person"
"Rules" : [
{
"Rule" : "MustBeEqual"
"Property" : "Name"
"ConstructorArguments" : [{"Dave"}]
},
{
"Rule" : "MustBeLessThanOrEqual"
"Property" : "Age"
"ConstructorArguments" : [{25}]
},
]
}
}

From that model, your translator should then (perhaps with the help of some Reflection) Create Rule objects and add them to a Rule Engine.