MapperConfigurationCollectionContainer.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace Masuit.Tools.Mapping.Core
  6. {
  7. /// <summary>
  8. /// 单例存储映射器。
  9. /// </summary>
  10. /// <remarks>不需要单例,因为适用于所有线程。</remarks>
  11. public class MapperConfigurationCollectionContainer : IEnumerable<MapperConfigurationBase>
  12. {
  13. private readonly HashSet<MapperConfigurationBase> _items;
  14. private static MapperConfigurationCollectionContainer currentInstance;
  15. private MapperConfigurationCollectionContainer()
  16. {
  17. _items = new HashSet<MapperConfigurationBase>();
  18. }
  19. /// <summary>
  20. /// mapper映射容器
  21. /// </summary>
  22. public static MapperConfigurationCollectionContainer Instance => currentInstance ??= new MapperConfigurationCollectionContainer();
  23. /// <summary>
  24. /// count
  25. /// </summary>
  26. public int Count => _items.Count;
  27. /// <summary>
  28. /// 索引器
  29. /// </summary>
  30. /// <param name="index"></param>
  31. /// <returns></returns>
  32. internal MapperConfigurationBase this[int index]
  33. {
  34. get
  35. {
  36. if (index > _items.Count)
  37. throw new IndexOutOfRangeException();
  38. var enumerator = GetEnumerator();
  39. int i = 0;
  40. while (enumerator.MoveNext())
  41. {
  42. if (i == index)
  43. {
  44. return enumerator.Current;
  45. }
  46. i++;
  47. }
  48. return null;
  49. }
  50. }
  51. /// <summary>
  52. /// 查找指定的源。
  53. /// </summary>
  54. /// <param name="source">源类型</param>
  55. /// <param name="target">目标对象</param>
  56. /// <param name="name">别名</param>
  57. internal MapperConfigurationBase Find(Type source, Type target, string name = null)
  58. {
  59. foreach (var current in this)
  60. {
  61. string nameMapper = string.IsNullOrEmpty(name) ? current.paramClassSource.Name : name;
  62. if (current.SourceType == source && current.TargetType == target && current.Name == nameMapper)
  63. {
  64. return current;
  65. }
  66. }
  67. return null;
  68. }
  69. /// <summary>
  70. /// 是否存在谓词的映射。
  71. /// </summary>
  72. /// <param name="match">条件表达式</param>
  73. public bool Exists(Func<MapperConfigurationBase, bool> match)
  74. {
  75. return this.Any(match);
  76. }
  77. /// <summary>
  78. /// 移除指定的元素
  79. /// </summary>
  80. /// <param name="index"></param>
  81. public void RemoveAt(int index)
  82. {
  83. MapperConfigurationBase itemToDelete = this[index];
  84. if (itemToDelete != null)
  85. {
  86. _items.Remove(itemToDelete);
  87. }
  88. }
  89. /// <summary>
  90. /// 清除
  91. /// </summary>
  92. public void Clear()
  93. {
  94. _items.Clear();
  95. }
  96. /// <summary>
  97. /// 添加
  98. /// </summary>
  99. /// <param name="value"></param>
  100. public void Add(MapperConfigurationBase value)
  101. {
  102. _items.Add(value);
  103. }
  104. /// <summary>
  105. /// 迭代器
  106. /// </summary>
  107. /// <returns></returns>
  108. public IEnumerator<MapperConfigurationBase> GetEnumerator()
  109. {
  110. return _items.GetEnumerator();
  111. }
  112. /// <summary>
  113. /// 迭代器
  114. /// </summary>
  115. /// <returns></returns>
  116. IEnumerator IEnumerable.GetEnumerator()
  117. {
  118. return GetEnumerator();
  119. }
  120. }
  121. }