Skip to content

Commit e390f4f

Browse files
committed
Merge pull request #91 from hazzik/many-to-many-where
Added generic version of `ChildWhere` for many-to-many part.
2 parents 714b290 + fb2d288 commit e390f4f

File tree

5 files changed

+34
-12
lines changed

5 files changed

+34
-12
lines changed

src/FluentNHibernate.Testing/FluentInterfaceTests/ManyToManyMutablePropertyModelGenerationTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,14 @@ public void ChildWhereShouldSetAttributeOnRelationshipModel()
272272
.ModelShouldMatch(x => ((ManyToManyMapping)x.Relationship).Where.ShouldEqual("some condition"));
273273
}
274274

275+
[Test]
276+
public void GenericChildWhereShouldSetAttributeOnRelationshipModel()
277+
{
278+
ManyToMany(x => x.BagOfChildren)
279+
.Mapping(m => m.ChildWhere(x => x.Name == "Name"))
280+
.ModelShouldMatch(x => ((ManyToManyMapping)x.Relationship).Where.ShouldEqual("Name = 'Name'"));
281+
}
282+
275283
[Test]
276284
public void EntityNameShouldSetModelValue()
277285
{

src/FluentNHibernate.Testing/FluentInterfaceTests/WhereTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Linq;
44
using System.Linq.Expressions;
55
using FluentNHibernate.Mapping;
6-
using FluentNHibernate.Testing.DomainModel.Mapping;
76
using NUnit.Framework;
87

98
namespace FluentNHibernate.Testing.FluentInterfaceTests

src/FluentNHibernate/Mapping/ManyToManyPart.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Linq.Expressions;
45
using FluentNHibernate.Mapping.Providers;
56
using FluentNHibernate.MappingModel;
67
using FluentNHibernate.MappingModel.Collections;
8+
using FluentNHibernate.Utils;
79

810
namespace FluentNHibernate.Mapping
911
{
@@ -327,6 +329,17 @@ public ManyToManyPart<TChild> ChildWhere(string where)
327329
return this;
328330
}
329331

332+
/// <summary>
333+
/// Sets the where clause for this relationship, on the many-to-many element.
334+
/// Note: This only supports simple cases, use the string overload for more complex clauses.
335+
/// </summary>
336+
public ManyToManyPart<TChild> ChildWhere(Expression<Func<TChild, bool>> where)
337+
{
338+
var sql = ExpressionToSql.Convert(where);
339+
340+
return ChildWhere(sql);
341+
}
342+
330343
protected override CollectionMapping GetCollectionMapping()
331344
{
332345
var collection = base.GetCollectionMapping();

src/FluentNHibernate/Mapping/OneToManyPart.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Linq.Expressions;
45
using FluentNHibernate.MappingModel;
56
using FluentNHibernate.MappingModel.Collections;
7+
using FluentNHibernate.Utils;
68

79
namespace FluentNHibernate.Mapping
810
{
@@ -217,5 +219,16 @@ void EnsureGenericDictionary()
217219
if (!(childType.IsGenericType && childType.GetGenericTypeDefinition() == typeof(IDictionary<,>)))
218220
throw new ArgumentException(member.Name + " must be of type IDictionary<> to be used in a ternary assocation. Type was: " + childType);
219221
}
222+
223+
/// <summary>
224+
/// Sets the where clause for this one-to-many relationship.
225+
/// Note: This only supports simple cases, use the string overload for more complex clauses.
226+
/// </summary>
227+
public OneToManyPart<TChild> Where(Expression<Func<TChild, bool>> where)
228+
{
229+
var sql = ExpressionToSql.Convert(where);
230+
231+
return Where(sql);
232+
}
220233
}
221234
}

src/FluentNHibernate/Mapping/ToManyBase.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -537,17 +537,6 @@ public T Generic()
537537
return (T)this;
538538
}
539539

540-
/// <summary>
541-
/// Sets the where clause for this one-to-many relationship.
542-
/// Note: This only supports simple cases, use the string overload for more complex clauses.
543-
/// </summary>
544-
public T Where(Expression<Func<TChild, bool>> where)
545-
{
546-
var sql = ExpressionToSql.Convert(where);
547-
548-
return Where(sql);
549-
}
550-
551540
/// <summary>
552541
/// Sets the where clause for this one-to-many relationship.
553542
/// </summary>

0 commit comments

Comments
 (0)