|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
| 3 | +using System.Linq; |
3 | 4 | using System.Linq.Expressions;
|
4 | 5 | using FluentNHibernate.Testing.Values;
|
5 | 6 | using FluentNHibernate.Utils;
|
@@ -81,6 +82,19 @@ public static PersistenceSpecification<T> CheckReference<T>(this PersistenceSpec
|
81 | 82 | return spec.RegisterCheckedProperty(new ReferenceProperty<T, object>(property, propertyValue), propertyComparer);
|
82 | 83 | }
|
83 | 84 |
|
| 85 | + public static PersistenceSpecification<T> CheckReference<T, TReference>(this PersistenceSpecification<T> spec, |
| 86 | + Expression<Func<T, object>> expression, |
| 87 | + TReference propertyValue, |
| 88 | + params Func<TReference, object>[] propertiesToCompare) |
| 89 | + { |
| 90 | + // Because of the params keyword, the compiler will select this overload |
| 91 | + // instead of the one above, even when no funcs are supplied in the method call. |
| 92 | + if (propertiesToCompare == null || propertiesToCompare.Length == 0) |
| 93 | + return spec.CheckReference(expression, propertyValue, (IEqualityComparer)null); |
| 94 | + |
| 95 | + return spec.CheckReference(expression, propertyValue, new FuncEqualityComparer<TReference>(propertiesToCompare)); |
| 96 | + } |
| 97 | + |
84 | 98 | public static PersistenceSpecification<T> CheckReference<T, TProperty>(this PersistenceSpecification<T> spec,
|
85 | 99 | Expression<Func<T, TProperty>> expression,
|
86 | 100 | TProperty propertyValue,
|
@@ -121,6 +135,19 @@ public static PersistenceSpecification<T> CheckList<T, TListElement>(this Persis
|
121 | 135 | return spec.RegisterCheckedProperty(new ReferenceList<T, TListElement>(property, propertyValue), elementComparer);
|
122 | 136 | }
|
123 | 137 |
|
| 138 | + public static PersistenceSpecification<T> CheckList<T, TListElement>(this PersistenceSpecification<T> spec, |
| 139 | + Expression<Func<T, IEnumerable<TListElement>>> expression, |
| 140 | + IEnumerable<TListElement> propertyValue, |
| 141 | + params Func<TListElement, object>[] propertiesToCompare) |
| 142 | + { |
| 143 | + // Because of the params keyword, the compiler can select this overload |
| 144 | + // instead of the one above, even when no funcs are supplied in the method call. |
| 145 | + if (propertiesToCompare == null || propertiesToCompare.Length == 0) |
| 146 | + return spec.CheckList(expression, propertyValue, (IEqualityComparer)null); |
| 147 | + |
| 148 | + return spec.CheckList(expression, propertyValue, new FuncEqualityComparer<TListElement>(propertiesToCompare)); |
| 149 | + } |
| 150 | + |
124 | 151 | public static PersistenceSpecification<T> CheckList<T, TListElement>(this PersistenceSpecification<T> spec,
|
125 | 152 | Expression<Func<T, IEnumerable<TListElement>>> expression,
|
126 | 153 | IEnumerable<TListElement> propertyValue,
|
@@ -254,5 +281,25 @@ public static PersistenceSpecification<T> CheckEnumerable<T, TItem>(this Persist
|
254 | 281 | {
|
255 | 282 | return spec.CheckList(expression, itemsToAdd, addAction);
|
256 | 283 | }
|
| 284 | + |
| 285 | + private class FuncEqualityComparer<T> : EqualityComparer<T> |
| 286 | + { |
| 287 | + readonly IEnumerable<Func<T, object>> comparisons; |
| 288 | + |
| 289 | + public FuncEqualityComparer(IEnumerable<Func<T, object>> comparisons) |
| 290 | + { |
| 291 | + this.comparisons = comparisons; |
| 292 | + } |
| 293 | + |
| 294 | + public override bool Equals(T x, T y) |
| 295 | + { |
| 296 | + return comparisons.All(func => object.Equals(func(x), func(y))); |
| 297 | + } |
| 298 | + |
| 299 | + public override int GetHashCode(T obj) |
| 300 | + { |
| 301 | + throw new NotSupportedException(); |
| 302 | + } |
| 303 | + } |
257 | 304 | }
|
258 | 305 | }
|
0 commit comments