using System;
using System.Reflection;
using System.Configuration;
using OT.IDAL;
namespace OT.DALFactory
{
///
/// 抽象工廠模式創建DAL
/// web.config 需要加入配置:(利用工程模式+反射機制+緩存機制,實現動態創建不同的數據層對象接口)
/// DataCache類在導出代碼的檔案夾裏
///
///
///
///
public sealed class DataAccess
{
private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];
///
/// 創建對象或從緩存獲取
///
public static T CreateObject(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;
}
///
/// 通過泛型實例化數據層接口
///
/// 數據層接口對象
/// 數據層名稱
/// 數據層接口實例化對象
public static T Create(string ClassName)
{
string ClassNamespace = AssemblyPath + "." + ClassName;
return CreateObject(AssemblyPath, ClassNamespace);
}
}
}