MapperConfigurationCollectionContainer.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. {
  38. throw new IndexOutOfRangeException();
  39. }
  40. var enumerator = GetEnumerator();
  41. int i = 0;
  42. while (enumerator.MoveNext())
  43. {
  44. if (i == index)
  45. {
  46. return enumerator.Current;
  47. }
  48. i++;
  49. }
  50. return null;
  51. }
  52. }
  53. /// <summary>
  54. /// 查找指定的源。
  55. /// </summary>
  56. /// <param name="source">源类型</param>
  57. /// <param name="target">目标对象</param>
  58. /// <param name="name">别名</param>
  59. internal MapperConfigurationBase Find(Type source, Type target, string name = null)
  60. {
  61. return this.Select(current => new
  62. {
  63. current,
  64. nameMapper = string.IsNullOrEmpty(name) ? current.paramClassSource.Name : name
  65. }).Where(t => t.current.SourceType == source && t.current.TargetType == target && t.current.Name == t.nameMapper).Select(t => t.current).FirstOrDefault();
  66. }
  67. /// <summary>
  68. /// 是否存在谓词的映射。
  69. /// </summary>
  70. /// <param name="match">条件表达式</param>
  71. public bool Exists(Func<MapperConfigurationBase, bool> match)
  72. {
  73. return this.Any(match);
  74. }
  75. /// <summary>
  76. /// 移除指定的元素
  77. /// </summary>
  78. /// <param name="index"></param>
  79. public void RemoveAt(int index)
  80. {
  81. if (this[index] != null)
  82. {
  83. _items.Remove(this[index]);
  84. }
  85. }
  86. /// <summary>
  87. /// 清除
  88. /// </summary>
  89. public void Clear()
  90. {
  91. _items.Clear();
  92. }
  93. /// <summary>
  94. /// 添加
  95. /// </summary>
  96. /// <param name="value"></param>
  97. public void Add(MapperConfigurationBase value)
  98. {
  99. _items.Add(value);
  100. }
  101. /// <summary>
  102. /// 迭代器
  103. /// </summary>
  104. /// <returns></returns>
  105. public IEnumerator<MapperConfigurationBase> GetEnumerator()
  106. {
  107. return _items.GetEnumerator();
  108. }
  109. /// <summary>
  110. /// 迭代器
  111. /// </summary>
  112. /// <returns></returns>
  113. IEnumerator IEnumerable.GetEnumerator()
  114. {
  115. return GetEnumerator();
  116. }
  117. }
  118. }