GETTING STARTED WITH WINDOWS WORKFLOW RULES ENGINE
- A quick and dirty Rules Engine using Windows Workflow by Rob Saunders
Following on from an experimentation with NxBRE, here’s a sample review of using the Windows Worflow Rules Engine that ships as part of .Net 4x.
System.Workflow namespace
.Net 4x ships with a namespace that provides a comprehensive list of Rules Engine functionality.
Namespace | Description |
---|---|
System.Workflow | The System.Workflow namespaces contain types used to develop applications that use Windows Workflow Foundation. These types provide design time and run-time support for rules and activities, to configure, control, host, and debug the workflow runtime engine. |
System.Workflow.Activities | Provides classes related to Windows Workflow Foundation activities. |
System.Workflow.Activites.Rules | Contains a set of classes that define the conditions and actions that form a rule. |
Workflow components
Component | Description |
---|---|
RuleEngine | Used to perform RuleSet evaluation. |
RuleExecution | Stores state information while executing RuleCondition or RuleAction classes. |
RuleValidation | Validates expression conditions. |
RuleSet | Contains a collection of Rule classes along with the semantics for forward-chaining execution of those rules. A RuleSet can be executed directly in code or using the PolicyActivity activity. |
Example implementation
To get started here’s a simple test method that fires up a set of rules and executes these rules on the incoming object
[TestMethod]
public void CreateNewRule()
{
Employee testEmployee = new Employee();
testEmployee.FirstName = "Joe";
testEmployee.Surname = "Smith";
testEmployee.Location = StateEnum.ACT;
testEmployee.Manager = null;
testEmployee.DateHired = DateTime.Now.AddYears(-1);
testEmployee.EmployeeNumber = 99;
string ruleName = String.Format("{0}UnitTestRule", DateTime.Now.Millisecond);
string path = Assembly.GetExecutingAssembly().Location.Replace(Assembly.GetExecutingAssembly().ManifestModule.Name, String.Empty);
RuleSet newRule = RulesManager<Employee>.LaunchNewRulesDialog(ruleName, path);
testEmployee = RulesManager<Employee>.ProcessRule(testEmployee, newRule);
Trace.WriteLine(testEmployee.FirstName);
Trace.WriteLine(testEmployee.Surname);
Trace.WriteLine(testEmployee.DateHired);
}
The ProcessRule looks like this…
/// <summary>
/// Execute a single rule on a single data object
/// </summary>
/// <param name="objectToProcess"></param>
/// <param name="rule"></param>
/// <returns></returns>
public static T ProcessRule(T objectToProcess, RuleSet rule)
{
RuleValidation validation = new RuleValidation(typeof(T), null);
RuleExecution execution = new RuleExecution(validation, objectToProcess);
rule.Execute(execution);
return objectToProcess;
}
This approach takes certain components form the Windows.Workflow namespace that ships with .Net 4x.