简单合同审批流程
假设一个简单的合同审批流程,对每一个提交的合同,根据合同的不同金额自动发往不同的审批人(部门),审批人可以对合同进行通过或不通过的审批。使用WF设计的流程序如下。
第一个节点(CallExtenalMethod)用于将合同信息保存到数据库中。紧接着的IFELSE用于判断合同的金额,以便将合同发往不的审批人。下面的Listen用于等待审事件(通过或不通过)的发生。
业务服务类的设计
<Serializable()> _
Public Class ContractEventArgs
Inherits ExternalDataEventArgs
Private mOperatorName As String
Public Sub New(ByVal instanceId As Guid, ByVal operatorName As String)
MyBase.New(instanceId)
mOperatorName = operatorName
End Sub
Public ReadOnly Property OperatorName() As String
Get
Return mOperatorName
End Get
End Property
End Class
''' <summary>
''' 合同审批服务
''' </summary>
''' <remarks></remarks>
<ExternalDataExchange()> _
Public Interface IContractService
''' <summary>
''' 合同已经通过审批
''' </summary>
Event ContractPassed As EventHandler(Of ContractEventArgs)
''' <summary>
''' 合同没有通过审批
''' </summary>
Event ContractNoPassed As EventHandler(Of ContractEventArgs)
''' <summary>
''' 提交合同审批申请,将合同内容写入数据库
''' </summary>
Function CreateContract(ByVal contractDate As Date, ByVal money As Double, ByVal content As String) As Contract
''' <summary>
''' 通过合同
''' </summary>
Sub PassContract(ByVal id As String)
''' <summary>
''' 拒绝合同
''' </summary>
Sub NoPassContract(ByVal id As String)
End Interface
业务服务类的定义要点
接口上必须添加<ExternalDataExchange()>属性,否则不能作为ExternalDataExChangeService中的服务,事件参数必须从ExternalDataEventArgs类继承而来,否则不能被EventDrivenActivity捕获事件。引发事件时必须使用异步方式,代码如下:
''' <summary>
''' 引发PassContract事件
''' </summary>
Public Sub RaisePassContraCTEvent(ByVal instanceId As Guid)
' Raise the event to the workflow
ThreadPool.QueueUserWorkItem(AddressOf JustCloseTheTicket, _
New ContractEventArgs(instanceId, "shangfc"))
End Sub
Public Sub JustCloseTheTicket(ByVal o As Object)
Dim args As ContractEventArgs
args = CType(o, ContractEventArgs)
RaiseEvent ContractPassed(Nothing, args)
End Sub
在合同审批表单中应获得对应的WorkflowInstanceID,然后将此调用它的Load方法将它加载到内存中,然后从WorkflowRunTime中获取对应的业务服务(IContractService),并调用业务服务的方法来引发事件,从而驱动流程向下前进。代码如下:
''' <summary>
''' 通过合同的审批
''' </summary>
Protected Sub btnPass_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPass.Click
Dim rt As WorkflowRuntime = Application.Get("Workflow")
Dim inst As WorkflowInstance = rt.GetWorkflow(New Guid(Label1.Text))
inst.Load()
Dim es As Workflow.Activities.ExternalDataExchangeService = rt.GetService(GetType(Workflow.Activities.ExternalDataExchangeService))
Dim cs As ContractWorkFlow.IContractService = es.GetService(GetType(ContractWorkFlow.IContractService))
cs.PassContract(Label1.Text)
End Sub
文章来源:http://blog.163.com/jhdzkfb22@126/blog/static/4584347520081111127378