Simple Rules Engine

Here is a rules engine that is internal to the .Net Framework.

This is the kind of rule that I can use:

<RuleSet.Text Name=’Text Ruleset’ Description=’Test Ruleset’ ChainingBehavior=’None’>
<Rule Active=’True’ Name=’R1′ Description=’Checks Name’ Priority=’1′ ReevaluationBehavior=’Never’ >
<Condition>this.Name == null</Condition>
<Action>this.ValidationMessages.Add(“Name should not be empty”)</Action>

The following is a unit test that executes the above:

public void FileStringRule()
TextRuleSetSerializer rs = new TextRuleSetSerializer();
RuleSet Rules = rs.Deserialize(typeof(Person), File.ReadAllText(@”.Ruleset.Rule”) );
Person p = new Person();
RuleExecution execution = new RuleExecution(new RuleValidation(p.GetType(), null), p);

Assert.AreEqual(1, p.ValidationMessages.Count);

Here is the definition of Person:

public class Person
public string Name { get; set;}
public int Age { get; set; }
private List<string> _ValidationMessages = new List<string>();
public List<string> ValidationMessages { get { return _ValidationMessages; } }

The beauty of this is that we have externalised the business rules in an efficient, readable compact form that makes no assumptions about the specific types.  The RuleEngine is passed a POCO to act as a Fact.  The Fact object is used to provide the details to the rules and collect the results.

By keeping the business rules externally to the application allows suitable changes to the logic to be made without requiring recompilation.  It is even possible to update the rules used by an application without requiring a redeploy!

However should you be prepared to take a dependency on a specific base class can I suggest that you look at the PerfectStorm.Model. This provides a base class that forms an Aggregate with a fully defined type system. It has the unusual property of being designed to permit invalid input to be entered. This allows an integer field to be set to the string XXX. This allows a single rules interface to perform complete declarative error handling – we don’t use one set of rules for parsing and another for buisiness logic. We can use rules to state what is an isn’t valid by providing an external ruleset. PerefectStorm.Model is intended to be a pure model class. It knows nothing of databases or user interfaces. It can hold data that an external class can use to map to these, but that is not it’s responsibility.

You need to be careful as to the specific goals of the rules engine. They are typically used to answer questions (Is this data valid? Could I save this to my database?) or to provide sensible defaults or to perform calculations (localised tax code calculations). These are also eminently suitable for unit testing and code generation.

Entity Framework Code First Fluent API – Can I create a fluent rules engine.

I am currently working through a lot of training courses (due to my employer being in special administration).

I have started on the Entity Framework Code First modules.
EF Code first seems to provide a declarative way of having a set of business entities that do not know about the database be persisted and loaded.

I am not interested in the annotation api since that is tying the business entities to the database.
[Rule #1 The model should not know about the view or the database.]

However the fluent api looks very interesting in that it can be used as an ORM (albeit a very simple one).
This means that a model could have two databases that it maps to (say old system and new system, or production and archive) that have distinct schema’s.

What would be interesting would be to see if this fluent api could not also be used as a validating rules engine. A given object can have different validation rules depending upon context.