一、创建团组时会开启流程
1 protected override void Insert() 2 { 3 GZWGroupInfo group = new GZWGroupInfo(); 4 group.CreatedBy = User.Identity.Name; 5 group.CreatedTime = DateTime.Now; 6 group.ApplyUnit = UnitService.GetUnitByCode(Profile.UnitCode); 7 Attach(group); 8 GroupService service = new GroupService(); 9 service.AddNew(group); 10 CreateBudgetEmpty(service, group); 11 LogHelper.LogOperation("增加报批件", group); 12 ApprovalHelper.InitWorkflow(group.Id); 13 }
二、主要是在ApprovalHelper.InitWorkflow(group.Id);行代码中开启的流程,我们进行InitWorkflow方法中
1 public static Guid InitWorkflow(int eaId) 2 { 3 WorkflowInstance workflowInstance = WorkflowRuntime.Current.CreateInstance(ConfigHelper.CurrentConfig.MainWorkflowName, eaId); 4 string createrUserId = HttpContext.Current.User.Identity.Name; 5 workflowInstance.Properties.Add("CreaterUserId", createrUserId); 6 workflowInstance.Properties.Add("UnitCode", Profile.GetUnitCode()); 7 workflowInstance.Start(); 8 return workflowInstance.Id; 9 }
三、在这个方法中我们创建了实例 WorkflowInstance
1 public WorkflowInstance CreateInstance(string workflowName, int eaId) 2 { 3 return new StateMachineWorkflowInstance(workflowName, eaId); 4 }
四、继续进去这个StateMachineWorkflowInstance构造函数中,在这里面我们对工作流的初始化了工作流的id,eaid,workflowName和工作流
1 internal StateMachineWorkflowInstance(string workflowName, int eaId) : this() 2 { 3 this.id = Guid.NewGuid(); 4 this.eaId = eaId; 5 this.workflowName = workflowName; 6 this.workflow = (StateMachineWorkflow) WorkflowRuntime.Current.GetService<IWorkFlowDefinePersistService>().GetWorkflowDefine(workflowName); 7 }
五、在获取这个工作流时实例时我们是通过调用接口IWorkFlowDefinePersistService中的GetWorkflowDefine(workflowName)获取的,我们发现是XmlWorkFlowDefinePersistService实现这个接口,我看再看看GetWorkflowDefine(workflowName)方法
1 public WorkFlowDefine GetWorkflowDefine(string workflowName) 2 { 3 WorkflowConfig config; int num; string fileName; 4 if (HttpContext.Current == null) 5 { config = this.GetConfig(); 6 foreach (WorkflowApplication application in config.Applications) 7 { num = 0; 8 while (num < application.Defines.Count) 9 { 10 if (application.Defines[num].Name.Equals(workflowName, StringComparison.OrdinalIgnoreCase)) 11 { fileName = this.GetFileName(application.Defines[num].File); 12 return this.ReadFromFile(application, fileName); 13 } 14 num++;}} 15 throw new ArgumentException(string.Format("workflowName ‘{0}‘ must in config", workflowName));} 16 string cacheName = this.GetCacheName(workflowName); 17 if (HttpContext.Current.Cache[cacheName] == null) 18 { config = this.GetConfig(); bool flag = false; 19 foreach (WorkflowApplication application in config.Applications) 20 { for (num = 0; num < application.Defines.Count; num++) 21 { if (application.Defines[num].Name.Equals(workflowName, StringComparison.OrdinalIgnoreCase)) 22 { fileName = this.GetFileName(application.Defines[num].File); 23 CacheDependency dependencies = new CacheDependency(fileName); 24 HttpContext.Current.Cache.Add(cacheName, this.ReadFromFile(application, fileName), dependencies, DateTime.MaxValue, Cache.NoSlidingExpiration, CacheItemPriority.High, null); 25 flag = true; 26 break; 27 } 28 } 29 } 30 if (!flag) 31 { 32 throw new ArgumentException(string.Format("workflowName ‘{0}‘ must in config", workflowName)); 33 } 34 } 35 return (WorkFlowDefine)HttpContext.Current.Cache[cacheName]; 36 } 37
六、WorkflowInstance的实例创建之后,我们回到步骤一中,继续往下面走,走到workflowInstance.Start();方法,我们进去看看这个方法
1 public override void Start() 2 { 3 StateMachineWorkflow workflow = (StateMachineWorkflow)this.Workflow; 4 ApprovalState stateByName = workflow.GetStateByName(workflow.InitState); 5 this.stateName = stateByName.Name; 6 this.persistTime = DateTime.Now; 7 WorkflowRuntime.Current.GetService<IWorkflowPersistService>().InsertWorkflowInstance(this); 8 }
这里的this,就是指的workflowInstance对象,在这个方法中调用了IWorkflowPersistService中的InsertWorkflowInstance方法,这是一个接口,它的实现类是WorkflowPersistService,实现了方法InsertWorkflowInstance,在这个方法中又调用了类WorkflowPersistService的抽象方法InsertWorkflowInstance(),而这个类是NHWorkflowPersistService继承了他,下面我们看看在这个类中的这个方法
七、 在OilDigital.Workflows.DAO程序集中的NHWorkflowPersistService类中实现了抽象方法InsertWorkflowInstance(),在这个方法中将StateMachineWorkflowInstance类转换成了我们数据库要存的类NHStateMachineInstance。
1 protected override void InsertWorkflowInstance(StateMachineWorkflowInstance instance) 2 { 3 foreach (Activity activity in instance.ExecutedActivities) 4 { 5 this.AddApprovalActivity(activity); 6 } 7 NHStateMachineInstanceDao dao = new NHStateMachineInstanceDao(); 8 NHStateMachineInstance nHInstance = new NHStateMachineInstance { 9 Id = instance.Id 10 }; 11 this.ConvertNHFromInstance(instance, nHInstance); 12 dao.Save(nHInstance); 13 }
至此,我们已经将创建的工作流类存到了数据库中。
八、我们看看数据库中工作流这个表存的哪些字段和这些字段大概存了哪些数据吧,这就是当我们创建一个团组时工作流表中记录的数据。