?? dataaccess.cs
字號(hào):
using System;
using System.Reflection;
using System.Configuration;
using NETManage.IDAL;
namespace NETManage.DALFactory
{
/// <summary>
/// 抽象工廠模式創(chuàng)建DAL。
/// web.config 需要加入配置:(利用工廠模式+反射機(jī)制+緩存機(jī)制,實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建不同的數(shù)據(jù)層對(duì)象接口)
/// DataCache類(lèi)在導(dǎo)出代碼的文件夾里
/// 可以把所有DAL類(lèi)的創(chuàng)建放在這個(gè)DataAccess類(lèi)里
/// <appSettings>
/// <add key="WebDAL" value="NETManage.SQLServerDAL" /> (這里的命名空間根據(jù)實(shí)際情況更改為自己項(xiàng)目的命名空間)
/// </appSettings>
/// </summary>
public sealed class DataAccess
{
private static readonly string path = ConfigurationManager.AppSettings["WebDAL"];
/// <summary>
/// 創(chuàng)建對(duì)象或從緩存獲取
/// </summary>
public static object CreateObject(string path,string CacheKey)
{
object objType = DataCache.GetCache(CacheKey);//從緩存讀取
if (objType == null)
{
try
{
objType = Assembly.Load(path).CreateInstance(CacheKey);//反射創(chuàng)建
DataCache.SetCache(CacheKey, objType);// 寫(xiě)入緩存
}
catch
{}
}
return objType;
}
/// <summary>
/// 創(chuàng)建Manager數(shù)據(jù)層接口
/// </summary>
public static NETManage.IDAL.IManager CreateManager()
{
string CacheKey = path+".Manager";
object objType=CreateObject(path,CacheKey);
return (NETManage.IDAL.IManager)objType;
}
/// <summary>
/// 創(chuàng)建OnlinePC數(shù)據(jù)層接口
/// </summary>
public static NETManage.IDAL.IOnlinePC CreateOnlinePC()
{
string CacheKey = path+".OnlinePC";
object objType=CreateObject(path,CacheKey);
return (NETManage.IDAL.IOnlinePC)objType;
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -