·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> .net之工作流工程展示及代码分享(四)主控制类
现在应该讲主控制类了,为了不把系统弄得太复杂,所以就用一个类作为主要控制类(服务类),作为前端、后端、业务逻辑的控制类。
WorkflowService类的类图如下:
该类的构造函数:
1 public WorkflowService(IWorkflowDB workflowDb, IWorkflowMethods workflowMethods)2 {3 _iWorkflowDb = workflowDb;4 _iWorkflowMethods = workflowMethods;5 }
通过简单工厂方法返回类的实例:
1 /// <summary> 2 /// 程序主调用方法 3 /// </summary> 4 /// <returns></returns> 5 public static WorkflowService GetWorkflowService() 6 { 7 IWorkflowDB iWorkflowDb; 8 try 9 {10 string dbSavingPRovider = WorkFlowUtility.GetConfiguration("DataBaseProvider").ToLower();11 switch (dbSavingProvider)12 {13 case "Oracle":14 iWorkflowDb = new OracleWorkFlowDBUtility();15 break;16 case "sqlserver":17 //iWorkflowDb = new SqlServerWorkFlowDBUtility();18 //break;19 default:20 iWorkflowDb = (IWorkflowDB) Assembly.Load(dbSavingProvider).CreateInstance(dbSavingProvider);21 break;22 }23 24 }25 catch (Exception)26 {27 throw new WorkFlowConfigurationNotImplementException("数据库配置失败!");28 }29 30 IWorkflowMethods iWorkflowMethods;31 try32 {33 string dbInterface = WorkFlowUtility.GetConfiguration("WorkflowConfigurationNameSpace");34 iWorkflowMethods = (IWorkflowMethods)Assembly.Load(dbInterface).CreateInstance(dbInterface + ".WorkflowConfiguration");35 }36 catch (Exception ex)37 {38 throw new WorkFlowConfigurationNotImplementException("应用程序实例配置失败!");39 }40 41 return new WorkflowService(iWorkflowDb, iWorkflowMethods);42 }View Code
所有方法返回的类型都是WorkflowServiceResponse类型,该类定义如下:
1 public class WorkflowServiceResponse 2 { 3 public WorkflowServiceResponse() 4 { 5 Success = false; 6 } 7 public bool Success { get; set; } 8 public string Message { get; set; } 9 public dynamic ReturnEntity { get; set; }10 }
使用了Request-Response模式,其中返回的实体使用了.net4.0新增的Dynamic类型,这样就不用做抽象类,然后各种不同的返回类型来继承基类了。
调用的方法如下:
1 var wfs = WorkflowService.GetWorkflowService();2 var wfsr = wfs.GetFlowById(flowId);3 if (!wfsr.Success)4 {5 this.ShowAndRedirects("参数错误", "FlowManager.aspx");6 return;7 }8 var flow = wfsr.ReturnEntity as Flow;9 .......
那GetFlowById是怎么写的呢?
1 /// <summary> 2 /// 根据流程Id得到一个流程的情况 3 /// </summary> 4 /// <param name="flowId"></param> 5 /// <returns></returns> 6 public WorkflowServiceResponse GetFlowById(Guid flowId) 7 { 8 var wfsr = new WorkflowServiceResponse(); 9 try10 {11 var flow = _iWorkflowDb.GetFlows().SingleOrDefault(f => f.FlowId == flowId);12 if (flow != null)13 {14 wfsr.ReturnEntity = flow;15 wfsr.Success = true;16 }17 }18 catch (Exception ex)19 {20 wfsr.Message = ex.ToString();21 }22 return wfsr;23 }
这样前后端都串起来了。
在IWorkflowDB接口定义里,GetFlows返回的是List<Flow>对象,所以这么做数据接口存在一定的性能问题,如果改成IQueryable<Flow>就能够返回Linq表达式,实现Lazy-Loaing的模式,但是Oracle不支持Linq查询,以后换EF看看......
大家注意到了WorkflowService类里面还有个IWorkflowMethods接口。对了,这个接口就是和其它系统交互的关键,定义如下:
1 public interface IWorkflowMethods2 {3 Person GetCurrentLoginPerson();4 IList<Person> GetPersonByCondition(SearchCriteria searchCriteria);5 bool SendMail(Person person, string message);6 bool SendMessage(Person person, string message);7 string GetAutoCompleteValueByType(string autoCompleteType);8 IList<string> GetUserRoleGroupNames(Person person);9 }
这些方法分别是获取当前登录用户、根据条件获取应该审核流程的用户、调用原系统方法法邮件、发短信,用于表单处理中的自动填入用户姓名、职务等,获取用户所在的组(有些流程只有特定组可以申请)。
在其中一个系统中的实现方法如下:
1 public IList<Person> GetPersonByCondition(SearchCriteria searchCriteria) 2 { 3 var flowLaunchPerson = searchCriteria.FlowLaunchPerson; 4 string sql = string.Empty; 5 IList<Person> persons = new List<Person>(); 6 switch (searchCriteria.NextPersonWorkGroupName) 7 { 8 case ".....": 9 break;10 case "xxx审核":11 {12 sql = string.Format(" jsgh in (select gh from VM_TUAN....ERGR where zbid in" +13 " (select zbid from TUA....ELATIONSHip where gh = '{0}' or xh = '{0}')" +14 " and lv = 4 and zwname = '辅导员)') ", flowLaunchPerson.PersonId);15 break;16 }17 case ".....审核":18 {19 sql = string.Format(" jsgh in (select gh from VM.....HERGR where zbid in" +20 " (select zbid from TUA....IONSHIP where gh = '{0}' or xh = '{0}')" +21 " and lv = 4 and zwname = '...') ", flowLaunchPerson.PersonId); 22 break;23 };24 case ".....审核":25 ........26 break;27 default:28 break;29 }30 31 var teachers = new B.....GetModelList(sql);32 33 if (teachers.Count > 0)34 {35 foreach (var teacher in teachers)36 {37 Person person = new Person()38 {39 PersonId = teacher.....,40 PersonName = teacher.J....,41 PersonEmail = teacher.E....,42 };43 persons.Add(person);44 }45 }46 else47 {48 throw new WorkFlowAuditPersonNotFoundException("没有找到审核对象!");49 }50 return persons;51 }View Code
最后分享服务类中两个核心的方法
新建流程:
1 /// <summary> 2 /// 根据流程Id建立新的流程 3 /// </summary> 4 /// <param name="workflowAddStepParameter"></param> 5