Discovering System.Linq.Expressions

I’m officially a fan: System.Linq.Expressions is one of the coolest namespaces.

I’ve been working on a simple workflow engine as an example codebase for exploring different aspects of object oriented programming (inheritance, encapsulation, polymorphism, design patterns, etc.) for training purposes.

As a side note, I’ve found that it’s actually very difficult to describe good object oriented code; I can just kind of feel it when I’m writing it and I know it when I see it…but it’s really, really hard to describe. I was asked by an associate consultant why it mattered. Why bother with good object oriented design? For me (at least), more than anything, it’s about organization of code, readability, maintainability, and usability. Good object oriented code makes it easy to think about the models and how the interactions between the different moving parts are executed. But that’s a bigger topic for a different post.

Back on topic 😀 The basic design scenario that I was trying to solve in this case is that the simple workflow engine (SWE) would have the capability of hydrating a workflow template instance from an XML definition file (much like WF does with .xoml files, but on a much more basic level, of course). I thought this would be a good exercise for teaching purposes as it would cover various aspects of reflection. Somewhere along the line, inspired by a comment by Richard Deeming (see bullet #4) on Rick Strahl’s DataContextFactory implementation, I decided to see if I could do it using expression trees instead.

Here is a sample XML template definition:

Here are the classes which this XML deserializes to:

I’ve done something similar in a workflow engine that I put together for Zorch Software before the days of WF. Back then I used reflection to assemble a workflow instance from an XML template as well. (In that case, the engine supported state machine workflows; for this training sample, I thought that just sequential was good enough)

Here is the invocation code to get a new instance of a workflow from a workflow template definition:

The basics are that the factory has to create an instance by iterating through each WorkflowTemplateAction and creating a concrete WorkflowAction instance and set property values using the WorkflowTemplateParameter associated with the WorkflowTemplateAction.

Here is the implementation of the factory method (cool stuff in bold):

 

The outer for loop iterates the actions to create and the inner for loop collects the properties to set on the action. Object creation and initializaiton is done in one shot using Expression.Lambda.

For reference, here is the target class for the XML in the sample above:

Admittedly, I didn’t expect this to work…I had a hell of a time figuring out how to create the expression to set the property values.

You may also like...

1 Response

  1. Q says:

    A fascinating approach.

    Good read over the morning Ice Coffee.

    (Found it via Dew Drop)