Skip to content

Commit d79203a

Browse files
committed
Adding an extension method to allow inverse list for the PersistenceSpecification test.
This prevents the issue with Brownfield apps where check foreign key constraint is enabled
1 parent 9bdc9d3 commit d79203a

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/FluentNHibernate/Testing/PersistenceSpecification.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ public PersistenceSpecification<T> RegisterCheckedProperty(Property<T> property)
8787
return RegisterCheckedProperty(property, null);
8888
}
8989

90+
public PersistenceSpecification<T> RegisterCheckedPropertyWithoutTransactionalSave(Property<T> property, IEqualityComparer equalityComparer)
91+
{
92+
property.EntityEqualityComparer = equalityComparer ?? entityEqualityComparer;
93+
allProperties.Add(property);
94+
95+
return this;
96+
}
9097
public PersistenceSpecification<T> RegisterCheckedProperty(Property<T> property, IEqualityComparer equalityComparer)
9198
{
9299
property.EntityEqualityComparer = equalityComparer ?? entityEqualityComparer;

src/FluentNHibernate/Testing/PersistenceSpecificationExtensions.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,29 @@ public static PersistenceSpecification<T> CheckList<T, TListElement>(this Persis
148148
return spec.CheckList(expression, propertyValue, new FuncEqualityComparer<TListElement>(propertiesToCompare));
149149
}
150150

151+
public static PersistenceSpecification<T> CheckInverseList<T, TListElement>(this PersistenceSpecification<T> spec,
152+
Expression<Func<T, IEnumerable<TListElement>>> expression,
153+
IEnumerable<TListElement> propertyValue,
154+
IEqualityComparer elementComparer)
155+
{
156+
Accessor property = ReflectionHelper.GetAccessor(expression);
157+
158+
return spec.RegisterCheckedPropertyWithoutTransactionalSave(new ReferenceList<T, TListElement>(property, propertyValue), elementComparer);
159+
}
160+
161+
public static PersistenceSpecification<T> CheckInverseList<T, TListElement>(this PersistenceSpecification<T> spec,
162+
Expression<Func<T, IEnumerable<TListElement>>> expression,
163+
IEnumerable<TListElement> propertyValue,
164+
params Func<TListElement, object>[] propertiesToCompare)
165+
{
166+
// Because of the params keyword, the compiler can select this overload
167+
// instead of the one above, even when no funcs are supplied in the method call.
168+
if (propertiesToCompare == null || propertiesToCompare.Length == 0)
169+
return spec.CheckList(expression, propertyValue, (IEqualityComparer)null);
170+
171+
return spec.CheckInverseList(expression, propertyValue, new FuncEqualityComparer<TListElement>(propertiesToCompare));
172+
}
173+
151174
public static PersistenceSpecification<T> CheckList<T, TListElement>(this PersistenceSpecification<T> spec,
152175
Expression<Func<T, IEnumerable<TListElement>>> expression,
153176
IEnumerable<TListElement> propertyValue,
@@ -156,6 +179,34 @@ public static PersistenceSpecification<T> CheckList<T, TListElement>(this Persis
156179
return spec.CheckList(expression, propertyValue, null, listItemSetter);
157180
}
158181

182+
public static PersistenceSpecification<T> CheckInverseList<T, TListElement>(this PersistenceSpecification<T> spec,
183+
Expression<Func<T, IEnumerable<TListElement>>> expression,
184+
IEnumerable<TListElement> propertyValue,
185+
Action<T, TListElement> listItemSetter)
186+
{
187+
return spec.CheckInverseList(expression, propertyValue, null, listItemSetter);
188+
}
189+
190+
public static PersistenceSpecification<T> CheckInverseList<T, TListElement>(this PersistenceSpecification<T> spec,
191+
Expression<Func<T, IEnumerable<TListElement>>> expression,
192+
IEnumerable<TListElement> propertyValue,
193+
IEqualityComparer elementComparer,
194+
Action<T, TListElement> listItemSetter)
195+
{
196+
Accessor property = ReflectionHelper.GetAccessor(expression);
197+
198+
var list = new ReferenceList<T, TListElement>(property, propertyValue);
199+
list.ValueSetter = (target, propertyInfo, value) =>
200+
{
201+
foreach (var item in value)
202+
{
203+
listItemSetter(target, item);
204+
}
205+
};
206+
207+
return spec.RegisterCheckedPropertyWithoutTransactionalSave(list, elementComparer);
208+
}
209+
159210
public static PersistenceSpecification<T> CheckList<T, TListElement>(this PersistenceSpecification<T> spec,
160211
Expression<Func<T, IEnumerable<TListElement>>> expression,
161212
IEnumerable<TListElement> propertyValue,

0 commit comments

Comments
 (0)