項目DALFactory是采用工廠模式設計的,設計模式的書我也曾看過Java的設計模式,理解也不太深刻,但對工廠模式還是較為熟悉,使用它可以根據(jù)需求返回不同的實例對象,在DALFactory項目中使用反射機制來實現(xiàn)依賴注入,當然,它的實現(xiàn)還是沒有java中的spring那樣靈活,強大,部分代碼如下:
// <summary>
/// 抽象工廠模式創(chuàng)建DAL。
/// Web.config 需要加入配置:(利用工廠模式+反射機制+緩存機制,實現(xiàn)動態(tài)創(chuàng)建不同的數(shù)據(jù)層對象接口)
/// DataCache類在導出代碼的文件夾里
/// 可以把所有DAL類的創(chuàng)建放在這個DataAccess類里
/// <appSettings>
/// <add key="DAL" value="SmsSystem.SQLServerDAL" /> (這里的命名空間根據(jù)實際情況更改為自己項目的命名空間)
/// </appSettings>
/// </summary>
public sealed class DataAccess
{
private static readonly string path = ConfigurationManager.AppSettings["DAL"];
/// <summary>
/// 創(chuàng)建對象或從緩存獲取
/// </summary>
public static object CreateObject(string path, string CacheKey)
{
object objType = DataCache.GetCache(CacheKey);//從緩存讀取
if (objType == null)
{
try
{
//Assembly ass = new Assembly();
objType = Assembly.Load(path).CreateInstance(CacheKey);//反射創(chuàng)建
DataCache.SetCache(CacheKey, objType);// 寫入緩存
}
catch(System.Exception ex)
{
string str = ex.Message;//
SmsSystem.Utility.SaveLog.SaveInfoToLog(str, "errorLog", "異常");
}
}
return objType;
}
/// <summary>
/// 不使用緩存,創(chuàng)建對象
/// </summary>
private static object CreateObjectNoCache(string path, string CacheKey)
{
try
{
object objType = Assembly.Load(path).CreateInstance(CacheKey);
return objType;
}
catch//(System.Exception ex)
{
//string str=ex.Message;// 記錄錯誤日志
return null;
}
}
/// <summary>
/// 創(chuàng)建CustEmployee數(shù)據(jù)層接口
/// </summary>
public static SmsSystem.IDAL.ICustEmployee CreateCustEmployee()
{
string CacheKey = path + ".CustEmployee";
object objType = CreateObject(path, CacheKey);
return (ICustEmployee)objType;
}
………………(其它數(shù)據(jù)層接口)
}
// <summary>
/// 抽象工廠模式創(chuàng)建DAL。
/// Web.config 需要加入配置:(利用工廠模式+反射機制+緩存機制,實現(xiàn)動態(tài)創(chuàng)建不同的數(shù)據(jù)層對象接口)
/// DataCache類在導出代碼的文件夾里
/// 可以把所有DAL類的創(chuàng)建放在這個DataAccess類里
/// <appSettings>
/// <add key="DAL" value="SmsSystem.SQLServerDAL" /> (這里的命名空間根據(jù)實際情況更改為自己項目的命名空間)
/// </appSettings>
/// </summary>
public sealed class DataAccess
{
private static readonly string path = ConfigurationManager.AppSettings["DAL"];
/// <summary>
/// 創(chuàng)建對象或從緩存獲取
/// </summary>
public static object CreateObject(string path, string CacheKey)
{
object objType = DataCache.GetCache(CacheKey);//從緩存讀取
if (objType == null)
{
try
{
//Assembly ass = new Assembly();
objType = Assembly.Load(path).CreateInstance(CacheKey);//反射創(chuàng)建
DataCache.SetCache(CacheKey, objType);// 寫入緩存
}
catch(System.Exception ex)
{
string str = ex.Message;//
SmsSystem.Utility.SaveLog.SaveInfoToLog(str, "errorLog", "異常");
}
}
return objType;
}
/// <summary>
/// 不使用緩存,創(chuàng)建對象
/// </summary>
private static object CreateObjectNoCache(string path, string CacheKey)
{
try
{
object objType = Assembly.Load(path).CreateInstance(CacheKey);
return objType;
}
catch//(System.Exception ex)
{
//string str=ex.Message;// 記錄錯誤日志
return null;
}
}
/// <summary>
/// 創(chuàng)建CustEmployee數(shù)據(jù)層接口
/// </summary>
public static SmsSystem.IDAL.ICustEmployee CreateCustEmployee()
{
string CacheKey = path + ".CustEmployee";
object objType = CreateObject(path, CacheKey);
return (ICustEmployee)objType;
}
………………(其它數(shù)據(jù)層接口)
}