InterfaceFactory.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.ComponentModel.Composition;
  5. using System.ComponentModel.Composition.Hosting;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using QWPlatform.SystemLibrary.LogManager;
  11. using QWPlatform.SystemLibrary.Utils;
  12. namespace PMS.Interface
  13. {
  14. /// <summary>
  15. /// 创 建 人:王海洋
  16. /// 创建日期:2018-12-10
  17. /// 功能描述:接口实例化工厂处理类
  18. /// </summary>
  19. public class InterfaceFactory
  20. {
  21. //缓存实例,如果在缓存中存在,直接返回
  22. private static ConcurrentDictionary<int, object> _cacheObject = new ConcurrentDictionary<int, object>();
  23. //private static Assembly businessAssembly = null;
  24. private static object locker = new object();
  25. /// <summary>
  26. /// 创建一个实例化工厂
  27. /// </summary>
  28. /// <typeparam name="T">指定泛型的接口</typeparam>
  29. /// <returns></returns>
  30. public static T CreateBusinessInstance<T>() where T : class
  31. {
  32. var obj = default(T);
  33. var t = typeof(T);
  34. var hashCode = t.GetHashCode();
  35. if (_cacheObject.ContainsKey(hashCode))
  36. {//缓存中存在。
  37. return _cacheObject[hashCode] as T;
  38. }
  39. else
  40. {//重新创建实例
  41. obj = CreateInstance<T>();
  42. if (obj != null && !_cacheObject.ContainsKey(hashCode))
  43. {//添加到缓存中
  44. _cacheObject.TryAdd(hashCode, obj);
  45. }
  46. else
  47. {
  48. //throw new NotImplementedException("业务对象创建为空:" + t.FullName);
  49. Logger.Instance.Error("业务对象创建为空");
  50. }
  51. }//end
  52. return obj;
  53. }
  54. //创建实例
  55. private static T CreateInstance<T>() where T : class
  56. {
  57. var obj = default(T);
  58. var t = typeof(T);
  59. try
  60. {
  61. var appPath = Path.Combine(Strings.AppDomainPath, "ZLSoft.PMS.BusinessService.dll");
  62. if (!File.Exists(appPath))
  63. {//未找到业务体dll
  64. throw new FileNotFoundException("未找到业务实现dll文件", appPath);
  65. }
  66. var businessAssembly = Assembly.Load("ZLSoft.PMS.BusinessService");
  67. var type = businessAssembly.GetTypes().First(p => p.GetInterface(t.Name) != null);
  68. if (type != null)
  69. {
  70. obj = (T)businessAssembly.CreateInstance(type.FullName);
  71. }
  72. return obj;
  73. //var types = businessAssembly.GetTypes();
  74. //foreach (var type in types)
  75. //{
  76. // var itype = type.GetInterface(t.Name);
  77. // if (itype != null)
  78. // {//找到实现的实体类
  79. // obj = Activator.CreateInstance(type) as T;
  80. // // obj = businessAssembly.CreateInstance(type.FullName) as T;
  81. // if (obj != null)
  82. // {//已到相应的实例,退出循环
  83. // return obj;
  84. // }
  85. // else
  86. // {
  87. // throw new QWPlatform.SystemLibrary.CustomException.DataConvertException("创建的业务实例为空对象", type.FullName, null);
  88. // }
  89. // }//end if
  90. //}//end foreach
  91. }
  92. catch (Exception ex)
  93. {
  94. Logger.Instance.Error("加载业务实现DLL异常", ex);
  95. throw new NotImplementedException("业务DLL没实现了最新定义的接口,可以尝试将业务层PMS.BusinessService.dll复制到运行的Bin目录下。" + ex.Message);
  96. }
  97. }
  98. /// <summary>
  99. /// 将所有实例对象清空
  100. /// </summary>
  101. public static void ClearCache()
  102. {//清空缓存
  103. _cacheObject.Clear();
  104. }
  105. }
  106. }