State Machine workflow implementation in State Mode

Source: Internet
Author: User

Overview

A state machine workflow consists of a group of States. A status is indicatedInitial status. Each status can receive a group of specific events. Depending on the event, you can switch to another State. State Machine WorkflowThere can be final states. When the final state is switched, the workflow is complete.

Scenario

A simple state machine workflow designed for my previous blog is as follows:

Let's design a workflow using the state pattern in the design pattern.

Design

Let's take a look at its user case function design:

Let's take a look at its class digoal design:

 

Implementation

Create an enums class first:

/*************************************** **************************************** * ** Class Name: enums ** Author: Spring Yang ** create Date: 2013-3-13 ** modify: Spring Yang ** modify Date: 2012-3-13 ** summary: enums class ************************************** **************************************** * **/using system; namespace workflowcommon {public Enum applicationstate {none, draft, inprogress, complete,} public Enum workflowstate {none, common, Manager, done, refuse} [flags] public Enum activitystate {create = 1, edit = 2, resbmit = 4, submit = 8, revoke = 16, approve = 32, reject = 64, read = 128, none = 256 }}

 

Create an iactivity interface:

 
/*************************************** **************************************** * ** Class Name: iactivity ** Author: Spring Yang ** create Date: 2013-3-13 ** modify: Spring Yang ** modify Date: 2012-3-13 ** summary: iactivity interface ************************************** **************************************** * **/namespace workflowservice. idal {using workflowcommon; public interface iactivity {workflowstate execute (activitystate );}}

 

Create an iactivitystate Interface

/*************************************** **************************************** * ** Class Name: iactivitystate ** Author: Spring Yang ** create Date: 2013-3-13 ** modify: Spring Yang ** modify Date: 2012-3-13 ** summary: iactivitystate interface ************************************** **************************************** * **/namespace workflowservice. idal {using workflowcommon; public interface iactivitystate {activitystate getactivitystate ();}}

 

Create an istatebase Interface

/*************************************** **************************************** * ** Class Name: istatebase ** Author: Spring Yang ** create Date: 2013-3-13 ** modify: Spring Yang ** modify Date: 2012-3-13 ** summary: istatebase interface ************************************** **************************************** * **/namespace workflowservice. idal {using workflowcommon; public interface istatebase: iactivity, iactivitystate {workflowstate getcurrentstate ();}}

 

Create commonstate class again

/*************************************** **************************************** * ** Class Name: commonstate ** Author: Spring Yang ** create Date: 2013-3-13 ** modify: Spring Yang ** modify Date: 2012-3-13 ** summary: commonstate class ************************************** **************************************** * **/namespace workflowservice. dal {using workflowcommon; using idal; public class commonstate: istatebase {public workflowstate execute (activitystate) {If (activitystate = activitystate. submit | activitystate = activitystate. resbmit) return workflowstate. manager; return workflowstate. common;} public workflowstate getcurrentstate () {return workflowstate. common;} public activitystate getactivitystate () {return activitystate. submit | activitystate. read | activitystate. revoke | activitystate. resbmit ;}}}

 

Create a donestate class

/*************************************** **************************************** * ** Class Name: donestate ** Author: Spring Yang ** create Date: 2013-3-13 ** modify: Spring Yang ** modify Date: 2012-3-13 ** summary: donestate class ************************************** **************************************** * **/namespace workflowservice. dal {using workflowcommon; using idal; public class donestate: istatebase {public workflowstate execute (activitystate) {return workflowstate. done;} public workflowstate getcurrentstate () {return workflowstate. done;} public activitystate getactivitystate () {return activitystate. read ;}}}

 

Create a managestate class.

/*************************************** **************************************** * ** Class Name: managestate ** Author: Spring Yang ** create Date: 2013-3-13 ** modify: Spring Yang ** modify Date: 2012-3-13 ** summary: managestate class ************************************** **************************************** * **/namespace workflowservice. dal {using workflowcommon; using idal; public class managestate: istatebase {public workflowstate execute (activitystate) {If (activitystate = activitystate. approve) return workflowstate. done; If (activitystate = activitystate. revoke) return workflowstate. common; return workflowstate. refuse;} public workflowstate getcurrentstate () {return workflowstate. manager;} public activitystate getactivitystate () {return activitystate. approve | activitystate. reject | activitystate. read ;}}}

 

Create a refusestate class

/*************************************** **************************************** * ** Class Name: refusestate ** Author: Spring Yang ** create Date: 2013-3-13 ** modify: Spring Yang ** modify Date: 2012-3-13 ** summary: refusestate class ************************************** **************************************** * **/namespace workflowservice. dal {using workflowcommon; using idal; public class refusestate: istatebase {public workflowstate execute (activitystate) {return workflowstate. refuse;} public workflowstate getcurrentstate () {return workflowstate. refuse;} public activitystate getactivitystate () {return activitystate. read ;}}}

 

Create a statemapping class:

/*************************************** **************************************** * ** Class Name: statemapping ** Author: Spring Yang ** create Date: 2013-3-13 ** modify: Spring Yang ** modify Date: 2012-3-13 ** summary: statemapping class ************************************** **************************************** * **/namespace workflowservice. help {using system. collections. generic; using Dal; using idal; public class statemapping {public list <istatebase> statebaseslist; private statemapping () {Init ();} Private Static statemapping _ instance; public static statemapping instance {get {If (_ instance = NULL) _ instance = new statemapping (); Return _ instance ;}} private void Init () {statebaseslist = new list <istatebase> {New commonstate (), new managestate (), new donestate (), new refusestate ()};}}}

 

Create workflowengine class

/*************************************** **************************************** * ** Class Name: workflowengine ** Author: Spring Yang ** create Date: 2013-3-13 ** modify: Spring Yang ** modify Date: 2012-3-13 ** summary: workflowengine class ************************************** **************************************** * **/namespace workflowservice. bll {using workflowcommon; using Dal; using help; using idal; public class workflowengine {private istatebase getcurrentworkflowstatebyworkflowstate (workflowstate) {foreach (VAR istatebase in statemapping. instance. statebaseslist) {If (istatebase. getcurrentstate () = workflowstate) return istatebase;} return New commonstate ();} public workflowstate execute (workflowstate, activitystate) {return getcurrentworkflowstatebyworkflowstate (workflowstate ). execute (activitystate);} public activitystate getactivitystatebyworkflowstate (workflowstate) {return getcurrentworkflowstatebyworkflowstate (workflowstate ). getactivitystate ();}}}

 

Create nunit Test

/*************************************** **************************************** * ** Class Name: teststateworkflowtest ** Author: Spring Yang ** create Date: 2013-3-13 ** modify: Spring Yang ** modify Date: 2012-3-13 ** summary: teststateworkflowtest interface ************************************** **************************************** * **/namespace testcommunication. workflowservice {using common; using nunit. framework; using wfservice; public class teststateworkflowtest: baseactivity {private workflowserviceclient wfserviceinstance {get {return New workflowserviceclient ();} [test] public void testnewworkflow () {var appentity = new appinfomodel {activitystate = activitystate. submit. tostring (), appid = "001", workflowname = "teststateworkflow", userid = "001", currentstate = "common"}; var result = wfserviceinstance. newworkflow (appentity); assert. areequal (result, "manage");} [test] public void testmanageapproveworkflow () {var appentity = new appinfomodel {activitystate = activitystate. submit. tostring (), appid = "002", workflowname = "teststateworkflow", userid = "002", currentstate = "common"}; var result = wfserviceinstance. newworkflow (appentity); assert. areequal (result, "manage"); var manageentity = new appinfomodel {activitystate = activitystate. approve. tostring (), appid = "002", workflowname = "teststateworkflow", userid = "003", currentstate = "manage"}; var manageresult = wfserviceinstance. execute (manageentity); assert. areequal (manageresult, "done");} [test] public void testmanagerejectworkflow () {var appentity = new appinfomodel {activitystate = activitystate. submit. tostring (), appid = "004", workflowname = "teststateworkflow", userid = "004", currentstate = "common"}; var result = wfserviceinstance. newworkflow (appentity); assert. areequal (result, "manage"); var manageentity = new appinfomodel {activitystate = activitystate. reject. tostring (), appid = "004", workflowname = "teststateworkflow", userid = "005", currentstate = "manage"}; var manageresult = wfserviceinstance. execute (manageentity); assert. areequal (manageresult, "refuse");} [test] public void testsaveworkflow () {var appentity = new appinfomodel {activitystate = activitystate. save. tostring (), appid = "006", workflowname = "teststateworkflow", userid = "006", currentstate = "common"}; var result = wfserviceinstance. newworkflow (appentity); assert. areequal (result, "common");} [test] public void testrevokeworkflow () {var appentity = new appinfomodel {activitystate = activitystate. submit. tostring (), appid = "007", workflowname = "teststateworkflow", userid = "007", currentstate = "common"}; var result = wfserviceinstance. newworkflow (appentity); assert. areequal (result, "manage"); var commonentity = new appinfomodel {activitystate = activitystate. revoke. tostring (), appid = "007", workflowname = "teststateworkflow", userid = "007", currentstate = "common"}; var revokeresult = wfserviceinstance. execute (commonentity); assert. areequal (revokeresult, "common");} [test] public void testresubmitworkflow () {var appentity = new appinfomodel {activitystate = activitystate. submit. tostring (), appid = "007", workflowname = "teststateworkflow", userid = "007", currentstate = "common"}; var result = wfserviceinstance. newworkflow (appentity); assert. areequal (result, "manage"); var commonentity = new appinfomodel {activitystate = activitystate. revoke. tostring (), appid = "007", workflowname = "teststateworkflow", userid = "007", currentstate = "common"}; var revokeresult = wfserviceinstance. execute (commonentity); assert. areequal (revokeresult, "common"); var resubmitentity = new appinfomodel {activitystate = activitystate. resubmit. tostring (), appid = "007", workflowname = "teststateworkflow", userid = "007", currentstate = "common"}; var lastresult = wfserviceinstance. execute (resubmitentity); assert. areequal (lastresult, "manage ");}}}

 

View the running result:

You are welcome to participate in the discussion. If you find it helpful, please clickRecommended. Thank you very much.

Author: Spring Yang

Source: http://www.cnblogs.com/springyangwc/

The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent andArticleThe original text connection is clearly displayed on the page. Otherwise, the legal liability is retained.

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.