WF Learning Series 5: state machine workflow Learning

Source: Internet
Author: User

1Introduction

A workflow is actually a modeling of the business processing process. When designing a workflow, we must first analyze the steps that need to be taken during the business processing process. Then, we can useWFCreate a workflow model to simulate the business processing process.

We know that,WFThere are two types of workflows: sequential workflows and state machine workflows. A sequential workflow provides a series of organized steps. Generally, the steps are executed one by one. Some steps may need to wait for the occurrence of some events to continue execution. However, sequential workflows are generally used for operations without manual intervention.

A state machine workflow provides a series of States. The workflow starts from the initial state to the end state. Defines the transition between two States. Generally, state machine workflows respond to events, and events change the status.

Which type of workflow is used depends on the specific business management process. For example, you need to create a workflow to simulate the order processing process. The procedure is as follows:

LThe store accepts the order, including the subuser, product, quantity, and other information;

LThe store checks whether the customer has a valid credit card. If yes, continue; otherwise, end;

LThe store checks whether the inventory list meets the order requirements. If yes, continue; otherwise, end;

LThe store requires a credit card company to pay the contract amount. If it succeeds, continue; otherwise it ends;

LShip the product to the customer. The process is complete.

From the above process, the sequential workflow can be implemented in one go.

Assume that the following changes occur:

LThe store accepts the order, including the subuser, product, quantity, and other information;

LThe store checks whether the customer has a valid credit card. If yes, continue; otherwise, end;

LThe store checks whether the inventory list meets the order requirements. If yes, continue; otherwise, end;

LIf the product is in stock, the product freight company will ship the product;

LBefore shipment, the store requires a credit card company to pay for the product. If it succeeds, continue; otherwise it ends;

LAfter the product is checked in, the processing process is waiting for the customer to confirm that the product has been received. If yes, the process ends. If the customer does not receive the message, the process will wait for the item to be resending. At this time, both the store and the customer can cancel the order and end the process.

The difference between this process and the previous process is that some processes need to be suspended temporarily and wait for the start of other processes. This suspension process usually takes a few days (about online shopping3-5Days ).

The advantage of a state machine workflow is that it can define the State and how the workflow is transferred from one State to another. When an external event occurs, the state machine workflow can be moved to different States. External behavior can be the hostProgramThe event that triggers an internal workflow event can also be the next State implemented by the Host Program.Setstate ActivityMove to the next status.

2Create a state machine Workflow

Participate in the original article. Here we will only introduce the activities unique to the state machine workflow:State activity, setstate activity, stateinitialization activity, statefinalization Activity.

3Define workflow status

Drag threeState activityTo the start, work, and end statuses.

4Status transition

ExploitationSetstate Activity, ThroughTargetstatenameAttribute.

5Define activity in each status

We can add four activitiesState activityMedium:

Stateinitialization Activity: If it exists, the first execution of the activity is changed after it enters the status.Stateinitialization ActivityIs a sequential activity, you can add all activities, includingSetstate Activity.

Eventdriven Activity: It can be used to control external events. It is also a sequential activity that can add all activities, includingSetstate Activity.

Statefinalization Activity: The last activity to be executed in the exit status. It is also a sequential activity that can be added to all activities, includingSetstate Activity.

State activity:State activityCan be embedded againState activityState Machine workflow with hierarchical structure. In this way, the sub-State activity can inherit the parent State activityDriven behavior event.

6Communication Service

For a State Machine workflow, a typical application scenario is that the workflow enters a state and waits for the host Program to trigger an event. PassEventdriven ActivityProcess external events. You can decide what the workflow executes, including the next status to be moved. Two activities are introduced here:

Handleexternaleventactivity:

LThe current process is suspended. When an external event is triggered, the process continues to run downward.

LTo loadExternaldataexchangeService

LExternaldataexchangeService, to load the class implementation of the bound Interface

LRunHandleexternaleventactivityAfter the nodeIdleStatus. The time of suyun is9999-12-31

LOnly when the Bound event is triggered in the host will the operation continue.

LPending or not arriving at the instanceHandleexternaleventactivityYou can also trigger an event externally when the instance is running to this node.HandleexternaleventactivityYou can directly go over without triggering any more.

 

Callexternalmethodactivity:

LCall external Methods inside a Workflow

LTo loadExternaldataexchangeService

LExternaldataexchangeService, to load the class implementation of the bound Interface

LRunCallexternalmethodactivityAutomatically call the methods in the instance after the node. Use the parameter settings in parameter ing to get the returned value.

 

Two activities:

[Externaldataexchangeattribute]

Public Interface Iordermanagement

{

Event Eventhandler<Ordereventargs> Ordershipped;

Event Eventhandler<Ordereventargs> Ordershippingcancel;

Event Eventhandler<Ordereventargs> Orderacknowledged;

Event Eventhandler<Ordereventargs> Orderreshipping;

 

BoolInstock (StringProductid,IntQuantity );

}

No matter which activity, you must have an interface with the external data exchange attribute. My personal objection lies in the workflowCodeTo call the Code related to the business logic. All business-related or database-related code should be written inIordermanagement.HandleexternaleventactivityThis API is used to call the workflow status externally so that the workflow can continue to run.CallexternalmethodactivityIt is used to call external code to access the Code of business concern, such as determining whether the inventory meets the requirements, and then determining the workflow running path through the return value.

7Implementation

Here, I have defined four States and it is easy to know the meaning of each state by name.

 

In the start state, an external method call activity is defined to check whether the inventory in the database meets the requirements of the order. The returned value isBoolType, and then executeIfelseActivity to switch to different States.

 

WaitingforshippingThe status contains two processing events: sending an order, and canceling an order. Is the status chart for sending orders.

 

References

This translation does not follow the original document. For details, refer

Http://social.msdn.microsoft.com/content/en-us/msft/netframework/wf/learn/Intro-StateMachineWorkflows

Http://www.cnblogs.com/foundation/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.