PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/FluentNHibernate/Testing/PersistenceSpecificationExtensions.cs

https://github.com/signoredems/fluent-nhibernate
C# | 255 lines | 207 code | 42 blank | 6 comment | 0 complexity | 7ed0bb16736966a56cf00b0699d54fba MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5. using FluentNHibernate.Testing.Values;
  6. using FluentNHibernate.Utils;
  7. using System.Collections;
  8. namespace FluentNHibernate.Testing
  9. {
  10. public static class PersistenceSpecificationExtensions
  11. {
  12. public static PersistenceSpecification<T> CheckProperty<T>(this PersistenceSpecification<T> spec,
  13. Expression<Func<T, object>> expression, object propertyValue)
  14. {
  15. return spec.CheckProperty(expression, propertyValue, (IEqualityComparer)null);
  16. }
  17. public static PersistenceSpecification<T> CheckProperty<T>(this PersistenceSpecification<T> spec,
  18. Expression<Func<T, object>> expression, object propertyValue,
  19. IEqualityComparer propertyComparer)
  20. {
  21. Accessor property = ReflectionHelper.GetAccessor(expression);
  22. return spec.RegisterCheckedProperty(new Property<T, object>(property, propertyValue), propertyComparer);
  23. }
  24. public static PersistenceSpecification<T> CheckProperty<T, TListElement>(this PersistenceSpecification<T> spec,
  25. Expression<Func<T, Array>> expression,
  26. IEnumerable<TListElement> propertyValue)
  27. {
  28. return spec.CheckProperty(expression, propertyValue, null);
  29. }
  30. public static PersistenceSpecification<T> CheckProperty<T, TListElement>(this PersistenceSpecification<T> spec,
  31. Expression<Func<T, Array>> expression,
  32. IEnumerable<TListElement> propertyValue,
  33. IEqualityComparer elementComparer)
  34. {
  35. Accessor property = ReflectionHelper.GetAccessor(expression);
  36. return spec.RegisterCheckedProperty(new List<T, TListElement>(property, propertyValue), elementComparer);
  37. }
  38. public static PersistenceSpecification<T> CheckProperty<T, TProperty>(this PersistenceSpecification<T> spec,
  39. Expression<Func<T, TProperty>> expression,
  40. TProperty propertyValue,
  41. Action<T, TProperty> propertySetter)
  42. {
  43. return spec.CheckProperty(expression, propertyValue, null, propertySetter);
  44. }
  45. public static PersistenceSpecification<T> CheckProperty<T, TProperty>(this PersistenceSpecification<T> spec,
  46. Expression<Func<T, TProperty>> expression,
  47. TProperty propertyValue,
  48. IEqualityComparer propertyComparer,
  49. Action<T, TProperty> propertySetter)
  50. {
  51. Accessor propertyInfoFromExpression = ReflectionHelper.GetAccessor(expression);
  52. var property = new Property<T, TProperty>(propertyInfoFromExpression, propertyValue);
  53. property.ValueSetter = (target, propertyInfo, value) => propertySetter(target, value);
  54. return spec.RegisterCheckedProperty(property, propertyComparer);
  55. }
  56. public static PersistenceSpecification<T> CheckReference<T>(this PersistenceSpecification<T> spec,
  57. Expression<Func<T, object>> expression,
  58. object propertyValue)
  59. {
  60. return spec.CheckReference(expression, propertyValue, (IEqualityComparer)null);
  61. }
  62. public static PersistenceSpecification<T> CheckReference<T>(this PersistenceSpecification<T> spec,
  63. Expression<Func<T, object>> expression,
  64. object propertyValue,
  65. IEqualityComparer propertyComparer)
  66. {
  67. Accessor property = ReflectionHelper.GetAccessor(expression);
  68. return spec.RegisterCheckedProperty(new ReferenceProperty<T, object>(property, propertyValue), propertyComparer);
  69. }
  70. public static PersistenceSpecification<T> CheckReference<T, TProperty>(this PersistenceSpecification<T> spec,
  71. Expression<Func<T, TProperty>> expression,
  72. TProperty propertyValue,
  73. Action<T, TProperty> propertySetter)
  74. {
  75. return spec.CheckReference(expression, propertyValue, null, propertySetter);
  76. }
  77. public static PersistenceSpecification<T> CheckReference<T, TProperty>(this PersistenceSpecification<T> spec,
  78. Expression<Func<T, TProperty>> expression,
  79. TProperty propertyValue,
  80. IEqualityComparer propertyComparer,
  81. Action<T, TProperty> propertySetter)
  82. {
  83. Accessor propertyInfoFromExpression = ReflectionHelper.GetAccessor(expression);
  84. var property = new ReferenceProperty<T, TProperty>(propertyInfoFromExpression, propertyValue);
  85. property.ValueSetter = (target, propertyInfo, value) => propertySetter(target, value);
  86. return spec.RegisterCheckedProperty(property, propertyComparer);
  87. }
  88. public static PersistenceSpecification<T> CheckList<T, TListElement>(this PersistenceSpecification<T> spec,
  89. Expression<Func<T, IEnumerable<TListElement>>> expression,
  90. IEnumerable<TListElement> propertyValue)
  91. {
  92. return spec.CheckList(expression, propertyValue, (IEqualityComparer)null);
  93. }
  94. public static PersistenceSpecification<T> CheckList<T, TListElement>(this PersistenceSpecification<T> spec,
  95. Expression<Func<T, IEnumerable<TListElement>>> expression,
  96. IEnumerable<TListElement> propertyValue,
  97. IEqualityComparer elementComparer)
  98. {
  99. Accessor property = ReflectionHelper.GetAccessor(expression);
  100. return spec.RegisterCheckedProperty(new ReferenceList<T, TListElement>(property, propertyValue), elementComparer);
  101. }
  102. public static PersistenceSpecification<T> CheckList<T, TListElement>(this PersistenceSpecification<T> spec,
  103. Expression<Func<T, IEnumerable<TListElement>>> expression,
  104. IEnumerable<TListElement> propertyValue,
  105. Action<T, TListElement> listItemSetter)
  106. {
  107. return spec.CheckList(expression, propertyValue, null, listItemSetter);
  108. }
  109. public static PersistenceSpecification<T> CheckList<T, TListElement>(this PersistenceSpecification<T> spec,
  110. Expression<Func<T, IEnumerable<TListElement>>> expression,
  111. IEnumerable<TListElement> propertyValue,
  112. IEqualityComparer elementComparer,
  113. Action<T, TListElement> listItemSetter)
  114. {
  115. Accessor property = ReflectionHelper.GetAccessor(expression);
  116. var list = new ReferenceList<T, TListElement>(property, propertyValue);
  117. list.ValueSetter = (target, propertyInfo, value) =>
  118. {
  119. foreach(var item in value)
  120. {
  121. listItemSetter(target, item);
  122. }
  123. };
  124. return spec.RegisterCheckedProperty(list, elementComparer);
  125. }
  126. public static PersistenceSpecification<T> CheckList<T, TListElement>(this PersistenceSpecification<T> spec,
  127. Expression<Func<T, IEnumerable<TListElement>>> expression,
  128. IEnumerable<TListElement> propertyValue,
  129. Action<T, IEnumerable<TListElement>> listSetter)
  130. {
  131. return spec.CheckList(expression, propertyValue, null, listSetter);
  132. }
  133. public static PersistenceSpecification<T> CheckList<T, TListElement>(this PersistenceSpecification<T> spec,
  134. Expression<Func<T, IEnumerable<TListElement>>> expression,
  135. IEnumerable<TListElement> propertyValue,
  136. IEqualityComparer elementComparer,
  137. Action<T, IEnumerable<TListElement>> listSetter)
  138. {
  139. Accessor property = ReflectionHelper.GetAccessor(expression);
  140. var list = new ReferenceList<T, TListElement>(property, propertyValue);
  141. list.ValueSetter = (target, propertyInfo, value) => listSetter(target, value);
  142. return spec.RegisterCheckedProperty(list, elementComparer);
  143. }
  144. public static PersistenceSpecification<T> CheckComponentList<T, TListElement>(this PersistenceSpecification<T> spec,
  145. Expression<Func<T, object>> expression,
  146. IEnumerable<TListElement> propertyValue)
  147. {
  148. return spec.CheckComponentList(expression, propertyValue, null);
  149. }
  150. /// <summary>
  151. /// Checks a list of components for validity.
  152. /// </summary>
  153. /// <typeparam name="TListElement">Type of list element</typeparam>
  154. /// <param name="expression">Property</param>
  155. /// <param name="propertyValue">Value to save</param>
  156. public static PersistenceSpecification<T> CheckComponentList<T, TListElement>(this PersistenceSpecification<T> spec,
  157. Expression<Func<T, object>> expression,
  158. IEnumerable<TListElement> propertyValue,
  159. IEqualityComparer elementComparer)
  160. {
  161. Accessor property = ReflectionHelper.GetAccessor(expression);
  162. return spec.RegisterCheckedProperty(new List<T, TListElement>(property, propertyValue), elementComparer);
  163. }
  164. public static PersistenceSpecification<T> CheckComponentList<T, TListElement>(this PersistenceSpecification<T> spec,
  165. Expression<Func<T, IEnumerable<TListElement>>> expression,
  166. IEnumerable<TListElement> propertyValue,
  167. Action<T, TListElement> listItemSetter)
  168. {
  169. return spec.CheckComponentList(expression, propertyValue, null, listItemSetter);
  170. }
  171. public static PersistenceSpecification<T> CheckComponentList<T, TListElement>(this PersistenceSpecification<T> spec,
  172. Expression<Func<T, IEnumerable<TListElement>>> expression,
  173. IEnumerable<TListElement> propertyValue,
  174. IEqualityComparer elementComparer,
  175. Action<T, TListElement> listItemSetter)
  176. {
  177. Accessor property = ReflectionHelper.GetAccessor(expression);
  178. var list = new List<T, TListElement>(property, propertyValue);
  179. list.ValueSetter = (target, propertyInfo, value) => {
  180. foreach(var item in value) {
  181. listItemSetter(target, item);
  182. }
  183. };
  184. return spec.RegisterCheckedProperty(list, elementComparer);
  185. }
  186. public static PersistenceSpecification<T> CheckComponentList<T, TListElement>(this PersistenceSpecification<T> spec,
  187. Expression<Func<T, IEnumerable<TListElement>>> expression,
  188. IEnumerable<TListElement> propertyValue,
  189. Action<T, IEnumerable<TListElement>> listSetter)
  190. {
  191. return spec.CheckComponentList(expression, propertyValue, null, listSetter);
  192. }
  193. public static PersistenceSpecification<T> CheckComponentList<T, TListElement>(this PersistenceSpecification<T> spec,
  194. Expression<Func<T, IEnumerable<TListElement>>> expression,
  195. IEnumerable<TListElement> propertyValue,
  196. IEqualityComparer elementComparer,
  197. Action<T, IEnumerable<TListElement>> listSetter)
  198. {
  199. Accessor property = ReflectionHelper.GetAccessor(expression);
  200. var list = new List<T, TListElement>(property, propertyValue);
  201. list.ValueSetter = (target, propertyInfo, value) => listSetter(target, value);
  202. return spec.RegisterCheckedProperty(list, elementComparer);
  203. }
  204. [Obsolete("CheckEnumerable has been replaced with CheckList")]
  205. public static PersistenceSpecification<T> CheckEnumerable<T, TItem>(this PersistenceSpecification<T> spec,
  206. Expression<Func<T, IEnumerable<TItem>>> expression,
  207. Action<T, TItem> addAction,
  208. IEnumerable<TItem> itemsToAdd)
  209. {
  210. return spec.CheckList(expression, itemsToAdd, addAction);
  211. }
  212. }
  213. }