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.
 
 
 
 
 
 

133 lines
4.1 KiB

  1. using Microsoft.Practices.Unity;
  2. using Microsoft.Practices.Unity.Configuration;
  3. using System.Configuration;
  4. namespace Learun.Ioc
  5. {
  6. /// <summary>
  7. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  8. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  9. /// 创建人:陈彬彬
  10. /// 日 期:2017.03.03
  11. /// 描 述:UnityIoc
  12. /// </summary>
  13. public class UnityIocHelper
  14. {
  15. #region 构造方法
  16. /// <summary>
  17. /// 构造方法
  18. /// </summary>
  19. /// <param name="containerName">容器名称</param>
  20. private UnityIocHelper(string containerName)
  21. {
  22. UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
  23. if (containerName == "IOCcontainer")
  24. {
  25. _container = new UnityContainer();
  26. section.Configure(_container, containerName);
  27. }
  28. else if (section.Containers.Count > 1)
  29. {
  30. _container = new UnityContainer();
  31. section.Configure(_container, containerName);
  32. }
  33. }
  34. #endregion
  35. #region 属性
  36. /// <summary>
  37. /// 容器
  38. /// </summary>
  39. private readonly IUnityContainer _container;
  40. /// <summary>
  41. /// 容器实例
  42. /// </summary>
  43. private static readonly UnityIocHelper instance = new UnityIocHelper("IOCcontainer");
  44. /// <summary>
  45. /// 容器实例
  46. /// </summary>
  47. private static readonly UnityIocHelper wfInstance = new UnityIocHelper("WfIOCcontainer");
  48. /// <summary>
  49. /// 容器实例
  50. /// </summary>
  51. private static readonly UnityIocHelper tsInstance = new UnityIocHelper("TsIOCcontainer");
  52. /// <summary>
  53. /// UnityIoc容器实例
  54. /// </summary>
  55. public static UnityIocHelper Instance
  56. {
  57. get { return instance; }
  58. }
  59. public static UnityIocHelper WfInstance
  60. {
  61. get { return wfInstance; }
  62. }
  63. /// <summary>
  64. /// 定时任务
  65. /// </summary>
  66. public static UnityIocHelper TsInstance
  67. {
  68. get { return tsInstance; }
  69. }
  70. #endregion
  71. #region 获取对应接口的具体实现类
  72. /// <summary>
  73. /// 获取实现类(默认映射)
  74. /// </summary>
  75. /// <typeparam name="T">接口类型</typeparam>
  76. /// <returns>接口</returns>
  77. public T GetService<T>()
  78. {
  79. return _container.Resolve<T>();
  80. }
  81. /// <summary>
  82. /// 获取实现类(默认映射)带参数的
  83. /// </summary>
  84. /// <typeparam name="T">接口类型</typeparam>
  85. /// <param name="parameter">参数</param>
  86. /// <returns>接口</returns>
  87. public T GetService<T>(params ParameterOverride[] parameter)
  88. {
  89. return _container.Resolve<T>(parameter);
  90. }
  91. /// <summary>
  92. /// 获取实现类(指定映射)带参数的
  93. /// </summary>
  94. /// <typeparam name="T"></typeparam>
  95. /// <param name="name"></param>
  96. /// <param name="parameter"></param>
  97. /// <returns>接口</returns>
  98. public T GetService<T>(string name, params ParameterOverride[] parameter)
  99. {
  100. return _container.Resolve<T>(name, parameter);
  101. }
  102. #endregion
  103. #region 判断接口是否被注册了
  104. /// <summary>
  105. /// 判断接口是否被实现了
  106. /// </summary>
  107. /// <typeparam name="T">接口类型</typeparam>
  108. /// <returns>bool</returns>
  109. public bool IsResolve<T>()
  110. {
  111. return _container.IsRegistered<T>();
  112. }
  113. /// <summary>
  114. /// 判断接口是否被实现了
  115. /// </summary>
  116. /// <typeparam name="T">接口类型</typeparam>
  117. /// <param name="name">映射名称</param>
  118. /// <returns></returns>
  119. public bool IsResolve<T>(string name)
  120. {
  121. return _container.IsRegistered<T>(name);
  122. }
  123. #endregion
  124. }
  125. }