C#winform程序能反射获得数据提供程序工厂及使用缓存吗? C#winform程序能反射获得数据提供程序工厂及使用缓存吗?要在哪里设置?具体要怎么做? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 // <summary>/// 抽象工厂模式创建DAL。/// web.config 需要加入配置:(利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口)/// DataCache类在导出代码的文件夹里/// 可以把所有DAL类的创建放在这个DataAccess类里/// <appSettings>/// <add key="DAL" value="SmsSystem.SQLServerDAL" /> (这里的命名空间根据实际情况更改为自己项目的命名空间)/// </appSettings>/// </summary>public sealed class DataAccess{private static readonly string path = ConfigurationManager.AppSettings["DAL"];/// <summary>/// 创建对象或从缓存获取/// </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);//反射创建 DataCache.SetCache(CacheKey, objType);// 写入缓存 } catch(System.Exception ex) { string str = ex.Message;// SmsSystem.Utility.SaveLog.SaveInfoToLog(str, "errorLog", "异常"); } } return objType;}/// <summary>/// 不使用缓存,创建对象/// </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>/// 创建CustEmployee数据层接口/// </summary>public static SmsSystem.IDAL.ICustEmployee CreateCustEmployee(){ string CacheKey = path + ".CustEmployee"; object objType = CreateObject(path, CacheKey); return (ICustEmployee)objType;}………………(其它数据层接口)} 1楼说的是ASP.NET里的使用缓存吧?在WINFORM程序里要怎么调用? 研究asp.net缓存实现什么功能使用应用程序域的对象定义缓存类 获取GridView 中的行数 winform程序打包 奇怪问题 有差别吗? 如何让在panel里的子窗体随panel的大小改变而变化? table 控件中我怎样得到指定的行呢? 怎么处理DataGridView按一定条件显示数据 c# WinForm中数据列表用什么控件,只是浏览不编辑数据,需要分页显示。 代理服务器切换的问题 C# ADSL拨号问题 [22]怎样才能隐藏一个基类的公共成员? C#中的预编译有没有c++宏中的复杂功能? 如何判定盘符是否存在
/// 抽象工厂模式创建DAL。
/// web.config 需要加入配置:(利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口)
/// DataCache类在导出代码的文件夹里
/// 可以把所有DAL类的创建放在这个DataAccess类里
/// <appSettings>
/// <add key="DAL" value="SmsSystem.SQLServerDAL" /> (这里的命名空间根据实际情况更改为自己项目的命名空间)
/// </appSettings>
/// </summary>
public sealed class DataAccess
{
private static readonly string path = ConfigurationManager.AppSettings["DAL"];
/// <summary>
/// 创建对象或从缓存获取
/// </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);//反射创建
DataCache.SetCache(CacheKey, objType);// 写入缓存
}
catch(System.Exception ex)
{
string str = ex.Message;//
SmsSystem.Utility.SaveLog.SaveInfoToLog(str, "errorLog", "异常");
}
}
return objType;
}
/// <summary>
/// 不使用缓存,创建对象
/// </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>
/// 创建CustEmployee数据层接口
/// </summary>
public static SmsSystem.IDAL.ICustEmployee CreateCustEmployee()
{
string CacheKey = path + ".CustEmployee";
object objType = CreateObject(path, CacheKey);
return (ICustEmployee)objType;
}
………………(其它数据层接口)
}
使用应用程序域的对象
定义缓存类