数据存储引擎是本项目里比较有特色的模块。
特色一,使用接口来对应不同的数据库。数据库可以是Oracle、Sqlserver、MogoDB、甚至是XML文件。采用接口进行对应:
public interface IWorkflowDB
{
List<Flow> GetFlows();
bool SaveFlow(Flow flow);
bool DeleteFlow(Guid flowId); FlowInstance GetFlowInstanceByInstanceId(Guid flowInstanceId);
List<FlowInstance> GetFlowInstanceByFlowId(Guid flowId);
bool SaveFlowInstance(FlowInstance flowInstance);
List<FlowInstance> GetFlowInstancesListByUser(Person person = null);
bool DeleteFlowInstanceByInstanceId(Guid flowInstanceId); bool SaveForm(Form form);
bool DeleteForm(Guid formId);
List<Form> GetFormList(); bool AddStep(WorkflowStep workflowStep);
bool DeleteStep(Guid stepId);
List<WorkflowStep> GetStepList(); bool AddBaseTable(WorkflowConstant.BaseTable baseTable, Dictionary<int, string> values);
Dictionary<int, string> GetBaseTableData(WorkflowConstant.BaseTable baseTable);
bool DeleteBaseTableValue(WorkflowConstant.BaseTable baseTable, int key);
}
特色二,使用Oracle的SYS.XMLTYPE来存储数据,查询时使用辅助方法,或者XML查询表达式
这样系统里一共只用了四个表,包括一个临时表,每个表不超过三个字段。
WORKFLOW_SETTINGS表:(存储系统设置参数、流程等)
WORKFLOW_FORM表:(存储系统表单)
WORKFLOW_FLOW表:(存储流程实例)
还有一个temp表,一行一列,字段为CLOB类型。
XMLType里面存储的是什么呢?不错,正是各个类的实例,序列化后的字符:
实际存储的方式:
保存时:类的实例-->XML序列化-->直接用Oracle的XMLtype存储;
读取时:读出数据-->反序列化-->类的实例直接可用。
这样就能方便的解决实体模型或领域模型与数据库存储之间“阻抗不匹配的”的问题。而且XMLType可以被SQLServer等主流数据库支持,所以迁移到其他数据库也很方便;如果你要使用常规的建表规则进行存储,只要实现IWorkflowDB接口即可。
序列化反序列化的方法,使用了扩展方法,可以放在项目里任何一个静态类里:
public static string ToSerializableXML<T>(this T t)
{
XmlSerializer mySerializer = new XmlSerializer(typeof (T));
StringWriter sw = new StringWriter();
mySerializer.Serialize(sw, t);
return sw.ToString();
} public static T ToEntity<T>(this string xmlString)
{
var xs = new XmlSerializer(typeof (T));
var srReader = new StringReader(xmlString);
var steplist = (T) xs.Deserialize(srReader);
return steplist;
}
注意:有些类不能被序列化,比如Dictionary<TKey,TValue>,需要自己写可序列化的类。
好啦,接下来是实现IWorkflowDB接口的OracleWorkFlowDBUtility类,以存储工作流表单为例:
存储:
public bool SaveForm(Form form)
{
var formContent = form.FormContent;
var formId = form.FormId;
form.FormContent = string.Empty; var sql = string.Empty; DbHelperOra.ExecuteSql("truncate table WORKFLOW_TEMP");
var para = new OracleParameter("formInfo", OracleType.Clob);
para.Value = form.ToSerializableXML();
DbHelperOra.ExecuteSql("insert into WORKFLOW_TEMP(content) values (:formInfo)", para); var paras = new[]
{
new OracleParameter("formId", formId.ToString()),
new OracleParameter("formContent", formContent)
};
if (DbHelperOra.GetSingle("select count(*) from WORKFLOW_FORM where form_id = :formId",
new OracleParameter("formId", formId.ToString())).ToString() == "")
{
sql =
"update WORKFLOW_FORM set FORM_INFO = (select sys.xmlType.createXML(WORKFLOW_TEMP.content) from WORKFLOW_TEMP), FORM_CONTENT = :formContent where FORM_ID = :formId";
}
else
{
sql =
"insert into WORKFLOW_FORM(FORM_ID,FORM_INFO,FORM_CONTENT) values (:formId,(select sys.xmlType.createXML(WORKFLOW_TEMP.content) from WORKFLOW_TEMP),:formContent)";
} return DbHelperOra.ExecuteSql(sql, paras).ToString() == "";
}
读取:
public List<Form> GetFormList()
{
var ds =
DbHelperOra.Query(
"select t.form_info.getclobval() form_info, form_content from WORKFLOW_FORM t");
if (ds != null)
{
var forms = new List<Form>(ds.Tables[].Rows.Count);
foreach (DataRow dr in ds.Tables[].Rows)
{
var str = dr["form_info"].ToString();
var form = str.ToEntity<Form>();
form.FormContent = dr["form_content"].ToString();
forms.Add(form);
}
return forms;
}
return null;
}
删除:
public bool DeleteForm(Guid formId)
{
try
{
if(DbHelperOra.ExecuteSql("delete from WORKFLOW_FORM where form_id = :formid",
new OracleParameter("formid", formId.ToString())).ToString(CultureInfo.InvariantCulture)=="")
return true;
return false;
}
catch (Exception ex)
{
return false;
}
}
一切都很简单,没有恼人的一列列字段名,也不用ORM、代码生成器等,开发、维护效率大幅度提高。
数据访问类实例化在WorkflowService类里面
先定义私有变量:
private readonly IWorkflowDB _iWorkflowDb;
然后在类的构造函数里这么写:
public WorkflowService(IWorkflowDB workflowDb, IWorkflowMethods workflowMethods)
{
_iWorkflowDb = workflowDb;
_iWorkflowMethods = workflowMethods; }
使用简单工厂返回类的实例:
/// <summary>
/// 程序主调用方法
/// </summary>
/// <returns></returns>
public static WorkflowService GetWorkflowService()
{
IWorkflowDB iWorkflowDb;
try
{
string dbSavingProvider = WorkFlowUtility.GetConfiguration("DataBaseProvider").ToLower();
switch (dbSavingProvider)
{
case "oracle":
iWorkflowDb = new OracleWorkFlowDBUtility();
break;
case "sqlserver":
//iWorkflowDb = new SqlServerWorkFlowDBUtility();
//break;
default:
iWorkflowDb = (IWorkflowDB) Assembly.Load(dbSavingProvider).CreateInstance(dbSavingProvider);
break;
} }
catch (Exception)
{
throw new WorkFlowConfigurationNotImplementException("数据库配置失败!");
}
这样能在不同的项目中*配置数据存储方式了。
如果要提高查询效率,比如报表,就可以这么写查询:
select rownum "序号", t.dw "单位",vt.groupname || vs.groupname "团支部",t.xh "学号",t.xm "姓名",t.xb "性别",t.csrq "出生年月", t.zzmm "政治面貌", t.zc "职称", t.gzsj "工作时间", t.lxdh "联系电话" from
(select f.instance_id,
f.instance_content.extract('//FlowInstance/Creator/@PersonId').getstringval() xh,
max(decode(x.key,'c57e4eb1-834c-491f-a1b5-b8b67f1ed160',x.value,null)) dw,
max(decode(x.key,'6008c07f-617b-4607-8fe2-6a384c75ea8a',x.value,null)) xm,
max(decode(x.key,'1153c641-7567-44ce-85c8-fcb0230d5cf7',x.value,null)) xb,
max(decode(x.key,'76936043-17e0-40b7-9adc-22e88d461082',x.value,null)) csrq,
max(decode(x.key,'22c6f1c5-d07b-45ba-9752-1118fdafee4c',x.value,null)) zzmm,
max(decode(x.key,'47b32ce0-828e-4179-bf11-2ace0028fda5',x.value,null)) zc,
max(decode(x.key,'a5cd9229-c8f9-4fb4-ba4f-e8157eeefa04',x.value,null)) gzsj,
max(decode(x.key,'9d2984f9-a5c5-474d-807a-cd756b961615',x.value,null)) lxdh
from WORKFLOW_FLOW f,
XMLTable( '/FlowInstance/FlowStepInstances/FlowInstanceStep/WriteValues/SerializableDictionary'
passing f.instance_content
COLUMNS key VARCHAR2(40) PATH 'key',
value VARCHAR2(100) PATH 'value') x
where f.instance_content.extract('//FlowInstance/Flow/@FlowId').getstringval() = 'e2f35ac0-d5aa-4413-af54-2cf4fe3759ef'
and to_date(substr(f.instance_content.extract('//FlowInstance/@CreateDate').getstringval(), 0, 10), 'YYYY-MM-DD')
between to_date('2015-01-01','yyyy-mm-dd') and to_date('2015-06-01','yyyy-mm-dd')
group by f.instance_id, f.instance_content.extract('//FlowInstance/Creator/@PersonId').getstringval()) t
left outer join vm_tuanwei_teachergr vt on t.xh = vt.gh
left outer join vm_tuanwei_studentgr vs on t.xh = vs.xh
查出来的结果如下图:
好啦,大家对我数据存储方式有什么意见,可以就此进行讨论。
本系列导航: