PageRenderTime 51ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/FluentNHibernate/Testing/PersistenceSpecificationExtensions.cs

https://github.com/dotnetchris/fluent-nhibernate
C# | 162 lines | 126 code | 30 blank | 6 comment | 0 complexity | 7e745473dc6f4b202a86e46cc8cf0464 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. namespace FluentNHibernate.Testing
  8. {
  9. public static class PersistenceSpecificationExtensions
  10. {
  11. public static PersistenceSpecification<T> CheckProperty<T>(this PersistenceSpecification<T> spec,
  12. Expression<Func<T, object>> expression, object propertyValue)
  13. {
  14. PropertyInfo property = ReflectionHelper.GetProperty(expression);
  15. return spec.RegisterCheckedProperty(new Property<T, object>(property, propertyValue));
  16. }
  17. public static PersistenceSpecification<T> CheckProperty<T, TListElement>(this PersistenceSpecification<T> spec,
  18. Expression<Func<T, Array>> expression,
  19. IEnumerable<TListElement> propertyValue)
  20. {
  21. PropertyInfo property = ReflectionHelper.GetProperty(expression);
  22. return spec.RegisterCheckedProperty(new List<T, TListElement>(property, propertyValue));
  23. }
  24. public static PersistenceSpecification<T> CheckProperty<T, TProperty>(this PersistenceSpecification<T> spec,
  25. Expression<Func<T, TProperty>> expression,
  26. TProperty propertyValue,
  27. Action<T, TProperty> propertySetter)
  28. {
  29. PropertyInfo propertyInfoFromExpression = ReflectionHelper.GetProperty(expression);
  30. var property = new Property<T, TProperty>(propertyInfoFromExpression, propertyValue);
  31. property.ValueSetter = (target, propertyInfo, value) => propertySetter(target, value);
  32. return spec.RegisterCheckedProperty(property);
  33. }
  34. public static PersistenceSpecification<T> CheckReference<T>(this PersistenceSpecification<T> spec,
  35. Expression<Func<T, object>> expression,
  36. object propertyValue)
  37. {
  38. PropertyInfo property = ReflectionHelper.GetProperty(expression);
  39. return spec.RegisterCheckedProperty(new ReferenceProperty<T, object>(property, propertyValue));
  40. }
  41. public static PersistenceSpecification<T> CheckReference<T, TProperty>(this PersistenceSpecification<T> spec,
  42. Expression<Func<T, TProperty>> expression,
  43. TProperty propertyValue,
  44. Action<T, TProperty> propertySetter)
  45. {
  46. PropertyInfo propertyInfoFromExpression = ReflectionHelper.GetProperty(expression);
  47. var property = new ReferenceProperty<T, TProperty>(propertyInfoFromExpression, propertyValue);
  48. property.ValueSetter = (target, propertyInfo, value) => propertySetter(target, value);
  49. return spec.RegisterCheckedProperty(property);
  50. }
  51. public static PersistenceSpecification<T> CheckList<T, TListElement>(this PersistenceSpecification<T> spec,
  52. Expression<Func<T, IEnumerable<TListElement>>> expression,
  53. IEnumerable<TListElement> propertyValue)
  54. {
  55. PropertyInfo property = ReflectionHelper.GetProperty(expression);
  56. return spec.RegisterCheckedProperty(new ReferenceList<T, TListElement>(property, propertyValue));
  57. }
  58. public static PersistenceSpecification<T> CheckList<T, TListElement>(this PersistenceSpecification<T> spec,
  59. Expression<Func<T, IEnumerable<TListElement>>> expression,
  60. IEnumerable<TListElement> propertyValue,
  61. Action<T, TListElement> listItemSetter)
  62. {
  63. PropertyInfo property = ReflectionHelper.GetProperty(expression);
  64. var list = new ReferenceList<T, TListElement>(property, propertyValue);
  65. list.ValueSetter = (target, propertyInfo, value) =>
  66. {
  67. foreach (var item in value)
  68. {
  69. listItemSetter(target, item);
  70. }
  71. };
  72. return spec.RegisterCheckedProperty(list);
  73. }
  74. public static PersistenceSpecification<T> CheckList<T, TListElement>(this PersistenceSpecification<T> spec,
  75. Expression<Func<T, IEnumerable<TListElement>>> expression,
  76. IEnumerable<TListElement> propertyValue,
  77. Action<T, IEnumerable<TListElement>> listSetter)
  78. {
  79. PropertyInfo property = ReflectionHelper.GetProperty(expression);
  80. var list = new ReferenceList<T, TListElement>(property, propertyValue);
  81. list.ValueSetter = (target, propertyInfo, value) => listSetter(target, value);
  82. return spec.RegisterCheckedProperty(list);
  83. }
  84. /// <summary>
  85. /// Checks a list of components for validity.
  86. /// </summary>
  87. /// <typeparam name="TListElement">Type of list element</typeparam>
  88. /// <param name="expression">Property</param>
  89. /// <param name="propertyValue">Value to save</param>
  90. public static PersistenceSpecification<T> CheckComponentList<T, TListElement>(this PersistenceSpecification<T> spec,
  91. Expression<Func<T, object>> expression,
  92. IEnumerable<TListElement> propertyValue)
  93. {
  94. PropertyInfo property = ReflectionHelper.GetProperty(expression);
  95. return spec.RegisterCheckedProperty(new List<T, TListElement>(property, propertyValue));
  96. }
  97. public static PersistenceSpecification<T> CheckComponentList<T, TListElement>(this PersistenceSpecification<T> spec,
  98. Expression<Func<T, IEnumerable<TListElement>>> expression,
  99. IEnumerable<TListElement> propertyValue,
  100. Action<T, TListElement> listItemSetter)
  101. {
  102. PropertyInfo property = ReflectionHelper.GetProperty(expression);
  103. var list = new List<T, TListElement>(property, propertyValue);
  104. list.ValueSetter = (target, propertyInfo, value) =>
  105. {
  106. foreach (var item in value)
  107. {
  108. listItemSetter(target, item);
  109. }
  110. };
  111. return spec.RegisterCheckedProperty(list);
  112. }
  113. public static PersistenceSpecification<T> CheckComponentList<T, TListElement>(this PersistenceSpecification<T> spec,
  114. Expression<Func<T, IEnumerable<TListElement>>> expression,
  115. IEnumerable<TListElement> propertyValue,
  116. Action<T, IEnumerable<TListElement>> listSetter)
  117. {
  118. PropertyInfo property = ReflectionHelper.GetProperty(expression);
  119. var list = new List<T, TListElement>(property, propertyValue);
  120. list.ValueSetter = (target, propertyInfo, value) => listSetter(target, value);
  121. return spec.RegisterCheckedProperty(list);
  122. }
  123. [Obsolete("CheckEnumerable has been replaced with CheckList")]
  124. public static PersistenceSpecification<T> CheckEnumerable<T, TItem>(this PersistenceSpecification<T> spec,
  125. Expression<Func<T, IEnumerable<TItem>>> expression,
  126. Action<T, TItem> addAction,
  127. IEnumerable<TItem> itemsToAdd)
  128. {
  129. return spec.CheckList(expression, itemsToAdd, addAction);
  130. }
  131. }
  132. }