E8.Net工作流架构及工作流引擎中有用到不少对象工厂模式,帮助具体业务逻辑的分别实现,比如工作流引擎中调用到业务接口对象,利用对象工厂模式实现具体的业务接口,传统方式下,我们会根据相关的配置信息,生成相应的实例.
![](https://i-blog.csdnimg.cn/blog_migrate/1d94a309e98c8befbe831093a401c715.jpeg)
这样工作流引擎就能实现到具体的业务规则了.
未经优化的传统做法中,会经常去获取配置信息,并动态生成对象实例,这样会比较耗时.
优化前代码:
public
ImplDataProcess(
long
lngAppID)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
this.mlngAppID = lngAppID;
//后期自动棒定,根据APPID,自动实例化对应的对象
object newInstance=Assembly.GetExecutingAssembly().CreateInstance(GetAppDataProcessAssembly(mlngAppID));
//object newInstance=Assembly.GetAssembly().CreateInstance(GetAppDataProcessAssembly(mlngAppID));
dp =(IDataProcess)newInstance;
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
private
static
string
GetAppDataProcessAssembly(
long
lngAppID)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string strSQL = "";
SqlDataReader dr;
![](/Images/OutliningIndicators/InBlock.gif)
string strConn = System.Configuration.ConfigurationSettings.AppSettings["SQLConnString"];;
![](/Images/OutliningIndicators/InBlock.gif)
string str = "appDataProcess.";
strSQL = "SELECT Project,AppCode FROM Es_XXXXX WHERE AppID=" + lngAppID.ToString();
dr = SqlTool.ExecuteReader(strConn,CommandType.Text,strSQL);
while(dr.Read())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
str = str + dr.GetString(0).Trim() + ".App_" + dr.GetString(0).Trim() + "_" + dr.GetString(1).Trim() + "_DP";
break;
}
dr.Close();
![](/Images/OutliningIndicators/InBlock.gif)
return str;
![](/Images/OutliningIndicators/InBlock.gif)
}
未经优化前的代码,每次调用到接口,都会产生一次数据库操作及对象加载的操作.
优化后代码:
private
static
Hashtable appsCache
=
Hashtable.Synchronized(
new
Hashtable());
![](/Images/OutliningIndicators/None.gif)
public
ImplDataProcess(
long
lngAppID)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
this.mlngAppID = lngAppID;
![](/Images/OutliningIndicators/InBlock.gif)
string hashKey = "ImplDP_" + lngAppID.ToString();
![](/Images/OutliningIndicators/InBlock.gif)
IDataProcess idp = appsCache[hashKey] as IDataProcess;
if (idp == null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
//后期自动棒定,根据APPID,自动实例化对应的对象
object newInstance = Assembly.GetExecutingAssembly().CreateInstance(GetAppDataProcessAssembly(mlngAppID));
//object newInstance=Assembly.GetAssembly().CreateInstance(GetAppDataProcessAssembly(mlngAppID));
dp = (IDataProcess)newInstance;
![](/Images/OutliningIndicators/InBlock.gif)
appsCache[hashKey] = dp;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dp = idp;
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
private
static
string
GetAppDataProcessAssembly(
long
lngAppID)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string strSQL = "";
SqlDataReader dr;
![](/Images/OutliningIndicators/InBlock.gif)
string strConn = System.Configuration.ConfigurationSettings.AppSettings["SQLConnString"]; ;
![](/Images/OutliningIndicators/InBlock.gif)
string str = "appDataProcess.";
//2008-02-10 添加SQL缓存依赖的处理方式,减少数据库连接次数
if (System.Configuration.ConfigurationSettings.AppSettings["SqlCacheModel"] == "1")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
DataTable dt = EpSqlCacheHelper.GetDataTableFromCache("app");
if (dt != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DataRow[] drs = dt.Select("Appid=" + lngAppID.ToString());
![](/Images/OutliningIndicators/InBlock.gif)
if (drs.Length > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
str = str + drs[0]["Project"].ToString() + ".App_" + drs[0]["Project"].ToString() + "_" + drs[0]["AppCode"].ToString() + "_DP";
}
}
![](/Images/OutliningIndicators/InBlock.gif)
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strSQL = "SELECT Project,AppCode FROM Es_XXXX WHERE AppID=" + lngAppID.ToString();
dr = SqlTool.ExecuteReader(strConn, CommandType.Text, strSQL);
while (dr.Read())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
str = str + dr.GetString(0).Trim() + ".App_" + dr.GetString(0).Trim() + "_" + dr.GetString(1).Trim() + "_DP";
break;
}
dr.Close();
}
![](/Images/OutliningIndicators/InBlock.gif)
return str;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
return str;
![](/Images/OutliningIndicators/InBlock.gif)
}
优化后的代码主要有以下几个方面的改善.
通过静态变量保存业务对象实例为HashTable. 如果不存在则创建,存在则直接使用. 配置表的读取,改为了数据库依赖的缓存,避免多次的数据库操作.
经过一系列的优化措施, 在E8.NET平台下开发的 深圳11185综合服务管理系统,目前B/S架构下轻松处理每日12000单以上的 物流配送 揽收 及电子商务等工作流应用
E8.Net
工作流平台为
.Net
企业应用开发提供了一个起点。
E8.Net
工作流提升企业战略执行力,欢迎访问:
http://***/productFlow.htm