Vous êtes sur la page 1sur 5

ObjectFlow

http://objectflow.codeplex.com/documentation Getting started


Install using the visual studio package manager console install-package objectflow.core -pre Install using Nuget on the commandline nuget.exe install objectflow.core -pre Create a class to identify states public class CustomerState { public bool OrderPlaced { get; set; } public bool Paid { get; set; } public bool DrinkReceived { get; set; } } Define a simple workflow var customerWorkflow = Workflow<CustomerState>.Definition() .Configure() .On<Exception>(() => Console.WriteLine("Caught an exception")) .When<CustomerState>() .Do<PlaceOrder>() .Do<PayForCoffee>() .Do<PickUp>(); Define operations public class PlaceOrder : BasicOperation<CustomerState> { public override CustomerState Execute(CustomerState data) { Console.WriteLine("Place order;"); data.OrderPlaced = true; return data; } }

Run the workflow CustomerState endState = customerWorkflow.Start(new CustomerState()); Last edited Sep 19, 2012 at 12:00 PM by djnz_gea, version 6

Api Documentation
Defining workflow operations Workflows are composed of operations. These can be functions, defined using lamda syntax, or custom classes that are subclasses of the frameworks BasicOperation<T> abstract class. Defining workflows Workflow<T> Do Then And AsAWorkflow<T> Policies Policies define additional behaviour expected of operations. Retry Repeat Then<T> Exceptions Constraints Constraints can be applied to an operation to determine whether it should be executed. Helper classes are used to compose constraints.

Workflow patterns
Common design patterns for workflows

Sequence

Workflow operations are performed one after another. Operation B only starts when operation A has completed.

And Split

Performs operations concurrently. B and C wait for A to finish but execute concurrently.

And Join

Merges back to sequence pattern following an and-split. D will execute only when all parallel operations have completed. Last edited Aug

2, 2010 at 5:56 PM by djnz_gea, version 18

Example code
This example registers two operation instances to perform the logic. One requires a variable reference for use in the later constraint evaluation, the other is registered using the generic interface method. Finally, a function is registered for the simpler step of sending the result to the console. In this example the generic doublespace registration will not execute because the constraint will always evaluate to false (the first operation succeeded). static void Main(string[] args)

{ var doubleSpace = new DoubleSpace(); var result = Workflow<Colour>.Definition() .Do(doubleSpace) .Do<DoubleSpace>(If.Not.Successfull(doubleSpace)) .Do((c) => { Console.WriteLine(c.Name); return c; }) .Start(new Colour("Green")); Console.WriteLine("\r\nPress any key"); Console.ReadKey(); } We could also have encapsulated the workflow definition using the AsAWorkflow<T> abstract class; var workflow = new RainbowWorkflow(Workflow<Colour>.Definition()); IExecuteWorkflow<Colour> flow = workflow; var result = flow.Start(new Colour("Green")); The client code has become simpler. Two lines is all that is needed as I cast the workflow variable to an IExecuteWorkflow<Colour> simply to show the polymorphic behaviour. The workflow definition is encapsulated by the RainbowWorkflow class below. public class RainbowWorkflow : AsAWorkflow<Colour> { public override IWorkflow<Colour> Configure() { var doubleSpace = new DoubleSpace(); return Configurer .Do(doubleSpace) .Do<DoubleSpace>(If.Not.Successfull(doubleSpace)) .Do((c) => { Console.WriteLine(c.Name); return c; }); } }

I removed superfluous classes to conserve space. Here

Vous aimerez peut-être aussi