You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

50 lines
1.7 KiB

using System;
using System.Reflection;
using System.Configuration;
using OT.IDAL;
namespace OT.DALFactory
{
/// <summary>
/// 抽象工廠模式創建DAL
/// web.config 需要加入配置:(利用工程模式+反射機制+緩存機制,實現動態創建不同的數據層對象接口)
/// DataCache類在導出代碼的檔案夾裏
/// <appSettings>
/// <add key="DAL" value="OT.SQLServerDAL" />
/// </appSettings>
/// </summary>
public sealed class DataAccess
{
private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];
/// <summary>
/// 創建對象或從緩存獲取
/// </summary>
public static T CreateObject<T>(string AssemblyPath, string ClassNamespace)
{
T objType = default(T);//= DataCache.GetCache(ClassNamespace);//從緩存讀取
if (objType == null)
{
try
{
objType = (T)Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射創建
//DataCache.SetCache(ClassNamespace, objType);// 寫入緩存
}
catch
{ }
}
return objType;
}
/// <summary>
/// 通過泛型實例化數據層接口
/// </summary>
/// <typeparam name="T">數據層接口對象</typeparam>
/// <param name="ClassName">數據層名稱</param>
/// <returns>數據層接口實例化對象</returns>
public static T Create<T>(string ClassName)
{
string ClassNamespace = AssemblyPath + "." + ClassName;
return CreateObject<T>(AssemblyPath, ClassNamespace);
}
}
}