·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> .net之工作流工程展示及代码分享(三)数据存储引擎
数据存储引擎是本项目里比较有特色的模块。
特色一,使用接口来对应不同的数据库。数据库可以是Oracle、Sqlserver、MogoDB、甚至是xml文件。采用接口进行对应:
1 public interface IWorkflowDB 2 { 3 List<Flow> GetFlows(); 4 bool SaveFlow(Flow flow); 5 bool DeleteFlow(Guid flowId); 6 7 FlowInstance GetFlowInstanceByInstanceId(Guid flowInstanceId); 8 List<FlowInstance> GetFlowInstanceByFlowId(Guid flowId); 9 bool SaveFlowInstance(FlowInstance flowInstance);10 List<FlowInstance> GetFlowInstancesListByUser(Person person = null);11 bool DeleteFlowInstanceByInstanceId(Guid flowInstanceId);12 13 bool SaveForm(Form form);14 bool DeleteForm(Guid formId);15 List<Form> GetFormList();16 17 bool AddStep(WorkflowStep workflowStep);18 bool DeleteStep(Guid stepId);19 List<WorkflowStep> GetStepList();20 21 bool AddBaseTable(WorkflowConstant.BaseTable baseTable, Dictionary<int, string> values);22 Dictionary<int, string> GetBaseTableData(WorkflowConstant.BaseTable baseTable);23 bool DeleteBaseTableValue(WorkflowConstant.BaseTable baseTable, int key);24 }View Code
特色二,使用Oracle的SYS.XMLTYPE来存储数据,查询时使用辅助方法,或者XML查询表达式
这样系统里一共只用了四个表,包括一个临时表,每个表不超过三个字段。
WORKFLOW_SETTINGS表:(存储系统设置参数、流程等)
WORKFLOW_FORM表:(存储系统表单)
WORKFLOW_FLOW表:(存储流程实例)
还有一个temp表,一行一列,字段为CLOB类型。
XMLType里面存储的是什么呢?不错,正是各个类的实例,序列化后的字符:
实际存储的方式:
保存时:类的实例-->XML序列化-->直接用Oracle的XMLtype存储;
读取时:读出数据-->反序列化-->类的实例直接可用。
这样就能方便的解决实体模型或领域模型与数据库存储之间“阻抗不匹配的”的问题。而且XMLType可以被SQLServer等主流数据库支持,所以迁移到其他数据库也很方便;如果你要使用常规的建表规则进行存储,只要实现IWorkflowDB接口即可。
序列化反序列化的方法,使用了扩展方法,可以放在项目里任何一个静态类里:
1 public static string ToSerializableXML<T>(this T t) 2 { 3 XmlSerializer mySerializer = new XmlSerializer(typeof (T)); 4 StringWriter sw = new StringWriter(); 5 mySerializer.Serialize(sw, t); 6 return sw.ToString(); 7 } 8 9 public static T ToEntity<T>(this string xmlString)10 {11 var xs = new XmlSerializer(typeof (T));12 var srReader = new StringReader(xmlString);13 var steplist = (T) xs.Deserialize(srReader);14 return steplist;15 }View Code
注意:有些类不能被序列化,比如Dictionary<TKey,TValue>,需要自己写可序列化的类。
好啦,接下来是实现IWorkflowDB接口的OracleWorkFlowDBUtility类,以存储工作流表单为例:
存储:
1 public bool SaveForm(Form form) 2 { 3 var formContent = form.FormContent; 4 var formId = form.FormId; 5 form.FormContent = string.Empty; 6 7 var sql = string.Empty; 8 9 DbHelperOra.ExecuteSql("truncate table WORKFLOW_TEMP");10 var para = new OracleParameter("formInfo", OracleType.Clob);11 para.Value = form.ToSerializableXML();12 DbHelperOra.ExecuteSql("insert into WORKFLOW_TEMP(content) values (:formInfo)", para);13 14 var paras = new[]15 {16 new OracleParameter("formId", formId.ToString()),17 new OracleParameter("formContent", formContent)18 };19 if (DbHelperOra.GetSingle("select count(*) from WORKFLOW_FORM where form_id = :formId",20 new OracleParameter("formId", formId.ToString())).ToString() == "1")21 {22 sql =23 "update WORKFLOW_FORM set FORM_INFO = (select sys.xmlType.createXML(WORKFLOW_TEMP.content) from WORKFLOW_TEMP), FORM_CONTENT = :formContent where FORM_ID = :formId";24 }25 else26 {27 sql =28 "insert into WORKFLOW_FORM(FORM_ID,FORM_INFO,FORM_CONTENT) values (:formId,(select sys.xmlType.createXML(WORKFLOW_TEMP.content) from WORKFLOW_TEMP),:formContent)";29 }30 31 return DbHelperOra.ExecuteSql(sql, paras).ToString() == "1";32 }View Code
读取:
1 public List<Form> GetFormList() 2 { 3 var ds = 4 DbHelperOra.Query( 5 "select t.form_info.getclobval() form_info, form_content from WORKFLOW_FORM t"); 6 if (ds != null) 7 { 8 var forms = new List<Form>(ds.Tables[0].Rows.Count); 9 foreach (DataRow dr in ds.Tables[0].Rows)10 {11 var str = dr["form_info"].ToString();12 var form = str.ToEntity<Form>();13 form.FormContent = dr["form_content"].ToString();14 forms.Add(form);15 }16 return forms;17 }18 return null;19 }View Code
删除:
1 public bool DeleteForm(Guid formId) 2 { 3 try 4 { 5 if(DbHelperOra.ExecuteSql("delete from WORKFLOW_FORM where form_id = :formid", 6 new OracleParameter("formid", formId.ToString())).ToString(CultureInfo.InvariantCulture)=="1") 7 return true; 8 return false; 9 }10 catch (Exception ex)11 {12 return false;13 }14 }View Code
一切都很简单,没有恼人的一列列字段名,也不用ORM、代码生成器等,开发、维护效率大幅度提高。
数据访问类实例化在WorkflowService类里面
先定义私有变量:
PRivate readonly IWorkflowDB _iWorkflowDb;
然后在类的构造函数里这么写: