Skip to content

Commit 17e953c

Browse files
committed
Add strongly typed overload for OrderBy/ChildOrderBy
Fixes #289 +semver:feature
1 parent f518464 commit 17e953c

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

src/FluentNHibernate.Testing/DomainModel/Mapping/OneToManyTester.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,14 @@ public void CanSpecifyOrderByClause()
946946
.Element("class/bag").HasAttribute("order-by", "foo");
947947
}
948948

949+
[Test]
950+
public void CanSpecifyOrderByClauseExpression()
951+
{
952+
new MappingTester<OneToManyTarget>()
953+
.ForMapping(m => m.HasMany(x => x.BagOfChildren).OrderBy(c => c.Name))
954+
.Element("class/bag").HasAttribute("order-by", "Name");
955+
}
956+
949957
[Test]
950958
public void OrderByClauseIgnoredForUnorderableCollections()
951959
{

src/FluentNHibernate/Mapping/ManyToManyPart.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using FluentNHibernate.MappingModel;
77
using FluentNHibernate.MappingModel.Collections;
88
using FluentNHibernate.Utils;
9+
using NHibernate.Criterion;
910

1011
namespace FluentNHibernate.Mapping
1112
{
@@ -235,6 +236,17 @@ protected override ICollectionRelationshipMapping GetRelationship()
235236
/// <summary>
236237
/// Sets the order-by clause on the collection element.
237238
/// </summary>
239+
public ManyToManyPart<TChild> OrderBy(Expression<Func<TChild, object>> orderBy)
240+
{
241+
return OrderBy(ExpressionToSql.Convert(orderBy));
242+
}
243+
244+
/// <summary>
245+
/// Sets the order-by clause on the collection element.
246+
/// </summary>
247+
/// <remarks>
248+
/// Note: This only supports simple cases, use the string overload for more complex clauses.
249+
/// </remarks>
238250
public ManyToManyPart<TChild> OrderBy(string orderBy)
239251
{
240252
collectionAttributes.Set("OrderBy", Layer.UserSupplied, orderBy);
@@ -244,6 +256,17 @@ public ManyToManyPart<TChild> OrderBy(string orderBy)
244256
/// <summary>
245257
/// Sets the order-by clause on the many-to-many element.
246258
/// </summary>
259+
public ManyToManyPart<TChild> ChildOrderBy(Expression<Func<TChild, object>> orderBy)
260+
{
261+
return ChildOrderBy(ExpressionToSql.Convert(orderBy));
262+
}
263+
264+
/// <summary>
265+
/// Sets the order-by clause on the many-to-many element.
266+
/// </summary>
267+
/// <remarks>
268+
/// Note: This only supports simple cases, use the string overload for more complex clauses.
269+
/// </remarks>
247270
public ManyToManyPart<TChild> ChildOrderBy(string orderBy)
248271
{
249272
relationshipAttributes.Set("OrderBy", Layer.UserSupplied, orderBy);
@@ -335,9 +358,7 @@ public ManyToManyPart<TChild> ChildWhere(string where)
335358
/// </summary>
336359
public ManyToManyPart<TChild> ChildWhere(Expression<Func<TChild, bool>> where)
337360
{
338-
var sql = ExpressionToSql.Convert(where);
339-
340-
return ChildWhere(sql);
361+
return ChildWhere(ExpressionToSql.Convert(@where));
341362
}
342363

343364
protected override CollectionMapping GetCollectionMapping()

src/FluentNHibernate/Mapping/OneToManyPart.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ public OneToManyPart<TChild> OrderBy(string orderBy)
144144
return this;
145145
}
146146

147+
/// <summary>
148+
/// Sets the order-by clause for this one-to-many relationship.
149+
/// </summary>
150+
/// <remarks>
151+
/// Note: This only supports simple cases, use the string overload for more complex clauses.
152+
/// </remarks>
153+
public OneToManyPart<TChild> OrderBy(Expression<Func<TChild, object>> orderBy)
154+
{
155+
return OrderBy(ExpressionToSql.Convert(orderBy));
156+
}
157+
147158
/// <summary>
148159
/// Specify that this collection is read-only
149160
/// </summary>
@@ -226,13 +237,13 @@ void EnsureGenericDictionary()
226237

227238
/// <summary>
228239
/// Sets the where clause for this one-to-many relationship.
229-
/// Note: This only supports simple cases, use the string overload for more complex clauses.
230240
/// </summary>
241+
/// <remarks>
242+
/// Note: This only supports simple cases, use the string overload for more complex clauses.
243+
/// </remarks>
231244
public OneToManyPart<TChild> Where(Expression<Func<TChild, bool>> where)
232245
{
233-
var sql = ExpressionToSql.Convert(where);
234-
235-
return Where(sql);
246+
return Where(ExpressionToSql.Convert(where));
236247
}
237248
}
238249
}

0 commit comments

Comments
 (0)