C#winform程序能反射获得数据提供程序工厂及使用缓存吗?
要在哪里设置?具体要怎么做?

解决方案 »

  1.   

    // <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;
    }
    ………………(其它数据层接口)
    }
      

  2.   

    1楼说的是ASP.NET里的使用缓存吧?在WINFORM程序里要怎么调用?
      

  3.   

    研究asp.net缓存实现什么功能
    使用应用程序域的对象
    定义缓存类