In the last post I showed how to define a workflow;

var workflow = Workflow<CustomerState>.Definition()
  .Configure()
  .On<Exception>(() => Console.WriteLine("Caught an exception"))
  .When<CustomerState>()
  .Do<PlaceOrder>()
  .Do<PayForCoffee>()
  .Do<PickUp>();

Although this is fine for defining small workflows, this would be cumbersome for more complex workflows.

The frameworks AsAWorkflow<T> abstract class solves this problem.  All we have to do is create a derived class and copy the above code into the Configure() method;

public class CoffeeCustomerService : AsAWorkflow<CustomerState>
{
    // Obsolete contructor
    public CoffeeCustomerService(IDefine<CustomerState> workflow) : base(workflow)
    {
    }

    // Obsolete method
    public override void Configure(IDefine<CustomerState> workflow)
    {
    }

    public override IWorkflow<CustomerState> Configure()
    {
        return Workflow<CustomerState>.Definition()
          .Configure()
            .On<Exception>(() => Console.WriteLine("Caught an exception"))
            .When<CustomerState>()
            .Do<PlaceOrder>()
            .Do<PayForCoffee>()
            .Do<PickUp>();
     }
}

The constructor and parameterised Configure method are obsolete and will be removed in a future release.  The workflow is defined in the Configure() method as usual.

All the client has to do is instantiate this class and pass the initial state to the start method;

var coffeeCustomerWorkflow = new CoffeeCustomerService(new Workflow<CustomerState>());
var customerState = new CustomerState();
coffeeCustomerWorkflow.Start(customerState);

This is a bit tidier and more re-useable.

The next minor release will have a default constructor for this class as we plan to obsolete the parameterized constructor.