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

  1. using System;
  2. using System.Reflection;
  3. using System.Configuration;
  4. using OT.IDAL;
  5. namespace OT.DALFactory
  6. {
  7. /// <summary>
  8. /// 抽象工廠模式創建DAL
  9. /// web.config 需要加入配置:(利用工程模式+反射機制+緩存機制,實現動態創建不同的數據層對象接口)
  10. /// DataCache類在導出代碼的檔案夾裏
  11. /// <appSettings>
  12. /// <add key="DAL" value="OT.SQLServerDAL" />
  13. /// </appSettings>
  14. /// </summary>
  15. public sealed class DataAccess
  16. {
  17. private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];
  18. /// <summary>
  19. /// 創建對象或從緩存獲取
  20. /// </summary>
  21. public static T CreateObject<T>(string AssemblyPath, string ClassNamespace)
  22. {
  23. T objType = default(T);//= DataCache.GetCache(ClassNamespace);//從緩存讀取
  24. if (objType == null)
  25. {
  26. try
  27. {
  28. objType = (T)Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射創建
  29. //DataCache.SetCache(ClassNamespace, objType);// 寫入緩存
  30. }
  31. catch
  32. { }
  33. }
  34. return objType;
  35. }
  36. /// <summary>
  37. /// 通過泛型實例化數據層接口
  38. /// </summary>
  39. /// <typeparam name="T">數據層接口對象</typeparam>
  40. /// <param name="ClassName">數據層名稱</param>
  41. /// <returns>數據層接口實例化對象</returns>
  42. public static T Create<T>(string ClassName)
  43. {
  44. string ClassNamespace = AssemblyPath + "." + ClassName;
  45. return CreateObject<T>(AssemblyPath, ClassNamespace);
  46. }
  47. }
  48. }