Implementing user-defined business rules with DDD
<p>Let's say If I have an application which let's user create business rules to be applied on a domain entity. A rule can be a combination of a condition and multiple actions where if condition evaluates to true then corresponding actions are executed. This rule is created by users in free-form text format which is then converted to a proprietary format which rule engine can understand and execute.</p>

<p>E.g. For an employee management system, if there is business rule to check if an employee is working in current Role for more than an year and has performed better than expected then can be promoted to next role with a 10% salary increment. This business rule can be entered by users as below.</p>

<p><strong>Condition</strong>: Employee.CurrentRoleLength > 1 &amp;&amp; Employee.ExceededExpectations()<br>
<strong>Action</strong>: Employee.PromoteToNextRole() <strong>|</strong> Employee.GiveSalaryIncrement(10)</p>

<p>Note that multiple Actions are delimited with a <strong>|</strong>. Also in order to execute this rule, application uses a separate rule engine class library to parse this condition and both actions to a proprietary format, say, <strong>ExecutableScript</strong> also defined in the rule engine class library.</p>

<p>Now in order to model this requirement using DDD; I have come up with following Domain objects.</p>

<p>Rule (Entity)<br>
Condition (Value Object)<br>
Action (Value Object)</p>

<p>where Rule is an Entity which contains a Condition Value Object and a list of Action Value Objects as below.</p>

<pre><code>public class Rule : Entity
{
    public Condition Condition { get; private set; }
    public IList&lt;Action&gt; Actions { get; private set;}

    public Rule(Condition condition, IList&lt;Action&gt; actions)
    {
        Condition = condition;
        Actions = actions;
    }
}

public sealed class Condition : ValueObject&lt;Condition&gt;
{
    public string ConditionText { get; private set;}
    public ExecutableScript ExecutableCondition{ get; private set;}

    public Condition(string conditionText)
    {
        ConditionText = conditionText;            
    }     

    public Parse()
    {
        ExecutableCondition = // How to parse using external rule engine ??;            
    }

    public Execute()
    {
        // How to execute using external rule engine ??;            
    }
}      

public sealed class Action : ValueObject&lt;Action&gt;
{
    public string ActionText{ get; private set;}
    public ExecutableScript ExecutableAction{ get; private set;}

    public Action(string actionText)
    {
        ActionText = actionText;            
    }

    public Parse()
    {
        ExecutableAction = // How to parse using external rule engine ??;            
    }

    public Execute()
    {
        // How to execute using external rule engine ??;            
    }
}
</code></pre>

<p>Based on above domain model, I have following questions.</p>

<ol>
<li><p>How can I parse and execute Condition and Actions without having a dependency on external rule engine. I understand Domain layer should not have any dependency on outer layers and should be confined to it's own.   </p></li>
<li><p>Even if I Parse Condition and Actions outside their domain objects, still their parsed ExceutableScript value need to be present within them which will still need dependency on external rule engine.   </p></li>
<li><p>Is it just that DDD is not the right approach for this scenario and I am going into wrong direction.</p></li>
</ol>

<p>Sorry for the long post. Any help would be highly appreciated. </p>

<p>Thanks.</p>