Merlin: Defining the Rule
This is part four of a series on Merlin. I recommend you read from the beginning.
Answering the "how" would come later. For now I decided that my focus should be on the "what". Again, pulling from the concepts of Outlook's Rules Wizard, I determined that a rule is run in some context. Keeping it simple (and singular...for now) I defined a rule as an action that is executed whenever a condition is evaluated to true. (You'll notice I excluded the concept of an exception at this time). Turning my thoughts into code produced something simple, like the following:
public class Rule
{
public void Run(Context context)
{
if (condition.Evaluate(context))
action.Execute(context);
}
}
Satisfied with my definitions and some pseudo-code to support them, I jumped back to problem at hand. What form should a rule take in order to keep it as flexible as possible. Following a similar pattern used throughout CommunityServer, I simply decided to store a rule as xml with pointers to the bits of code it required. My initial stab at a rule looked a bit like this:
<rule name="xxx">
<condition type="yyy" />
<action type="zzz" />
</rule>
It was at this time that I realized that to the degree that Merlin stayed generic, I would be free to leverage rules from any part of CS or any other application, for that matter.
I now had an idea brewing for how to tie this rule definition into CSModules, but I needed to run a few quick tests first... (continued)