Fixing compatibility with previous IContentQuery.OrderBy methods

--HG--
branch : NH3
This commit is contained in:
Sebastien Ros
2012-07-13 18:24:25 -07:00
parent 19e7f4af8d
commit b798effdd3
2 changed files with 50 additions and 8 deletions

View File

@@ -4,12 +4,10 @@ using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection.Emit; using System.Reflection.Emit;
using FluentNHibernate;
using NHibernate; using NHibernate;
using NHibernate.Criterion; using NHibernate.Criterion;
using Orchard.ContentManagement.Records; using Orchard.ContentManagement.Records;
using Orchard.Data; using Orchard.Data;
using Orchard.Utility.Extensions;
using NHibernate.Transform; using NHibernate.Transform;
using NHibernate.SqlCommand; using NHibernate.SqlCommand;
using Expression = System.Linq.Expressions.Expression; using Expression = System.Linq.Expressions.Expression;
@@ -38,20 +36,20 @@ namespace Orchard.ContentManagement {
return _session; return _session;
} }
IQueryOver<ContentItemVersionRecord, TRecord> BindQueryOverByPath<TRecord, U>(IQueryOver<ContentItemVersionRecord, U> queryOver, string name) { IQueryOver<ContentItemVersionRecord, TRecord> BindQueryOverByPath<TRecord, TU>(IQueryOver<ContentItemVersionRecord, TU> queryOver, string name) {
if (_joins.ContainsKey(typeof(TRecord).Name)) { if (_joins.ContainsKey(typeof(TRecord).Name)) {
return (IQueryOver<ContentItemVersionRecord, TRecord>)_joins[typeof(TRecord).Name]; return (IQueryOver<ContentItemVersionRecord, TRecord>)_joins[typeof(TRecord).Name];
} }
// public TPartRecord TPartRecord {get;set;} // public TPartRecord TPartRecord {get;set;}
var dynamicMethod = new DynamicMethod(name, typeof(TRecord), null, typeof(U)); var dynamicMethod = new DynamicMethod(name, typeof(TRecord), null, typeof(TU));
var syntheticMethod = new ContentItemAlteration.SyntheticMethodInfo(dynamicMethod, typeof(U)); var syntheticMethod = new ContentItemAlteration.SyntheticMethodInfo(dynamicMethod, typeof(TU));
var syntheticProperty = new ContentItemAlteration.SyntheticPropertyInfo(syntheticMethod); var syntheticProperty = new ContentItemAlteration.SyntheticPropertyInfo(syntheticMethod);
// record => record.TPartRecord // record => record.TPartRecord
var parameter = Expression.Parameter(typeof(U), "record"); var parameter = Expression.Parameter(typeof(TU), "record");
var syntheticExpression = (Expression<Func<U, TRecord>>)Expression.Lambda( var syntheticExpression = (Expression<Func<TU, TRecord>>)Expression.Lambda(
typeof(Func<U, TRecord>), typeof(Func<TU, TRecord>),
Expression.Property(parameter, syntheticProperty), Expression.Property(parameter, syntheticProperty),
parameter); parameter);
@@ -116,6 +114,24 @@ namespace Orchard.ContentManagement {
BindPartQueryOver<TRecord>().OrderBy(keySelector).Desc(); BindPartQueryOver<TRecord>().OrderBy(keySelector).Desc();
} }
private void OrderBy<TRecord, TKey>(Expression<Func<TRecord, TKey>> keySelector) where TRecord : ContentPartRecord {
BindPartQueryOver<TRecord>().OrderBy(AddBox(keySelector)).Asc();
}
private void OrderByDescending<TRecord, TKey>(Expression<Func<TRecord, TKey>> keySelector) where TRecord : ContentPartRecord {
BindPartQueryOver<TRecord>().OrderBy(AddBox(keySelector)).Desc();
}
private static Expression<Func<TInput, object>> AddBox<TInput, TOutput>
(Expression<Func<TInput, TOutput>> expression) {
// Add the boxing operation, but get a weakly typed expression
Expression converted = Expression.Convert
(expression.Body, typeof(object));
// Use Expression.Lambda to get back to strong typing
return Expression.Lambda<Func<TInput, object>>
(converted, expression.Parameters);
}
private IEnumerable<ContentItem> Slice(int skip, int count) { private IEnumerable<ContentItem> Slice(int skip, int count) {
var queryOver = BindItemVersionQueryOver(); var queryOver = BindItemVersionQueryOver();
@@ -205,6 +221,16 @@ namespace Orchard.ContentManagement {
_query.OrderByDescending(keySelector); _query.OrderByDescending(keySelector);
return new ContentQuery<T, TRecord>(_query); return new ContentQuery<T, TRecord>(_query);
} }
IContentQuery<T, TRecord> IContentQuery<T>.OrderBy<TRecord, TKey>(Expression<Func<TRecord, TKey>> keySelector) {
_query.OrderBy(AddBox(keySelector));
return new ContentQuery<T, TRecord>(_query);
}
IContentQuery<T, TRecord> IContentQuery<T>.OrderByDescending<TRecord, TKey>(Expression<Func<TRecord, TKey>> keySelector) {
_query.OrderByDescending(AddBox(keySelector));
return new ContentQuery<T, TRecord>(_query);
}
} }
@@ -225,6 +251,16 @@ namespace Orchard.ContentManagement {
return this; return this;
} }
IContentQuery<T, TR> IContentQuery<T, TR>.OrderBy<TKey>(Expression<Func<TR, TKey>> keySelector) {
_query.OrderBy(keySelector);
return this;
}
IContentQuery<T, TR> IContentQuery<T, TR>.OrderByDescending<TKey>(Expression<Func<TR, TKey>> keySelector) {
_query.OrderByDescending(keySelector);
return this;
}
IContentQuery<T, TR> IContentQuery<T, TR>.OrderBy(Expression<Func<TR, object>> keySelector) { IContentQuery<T, TR> IContentQuery<T, TR>.OrderBy(Expression<Func<TR, object>> keySelector) {
_query.OrderBy(keySelector); _query.OrderBy(keySelector);
return this; return this;

View File

@@ -23,6 +23,9 @@ namespace Orchard.ContentManagement {
IContentQuery<TPart, TRecord> Where<TRecord>(Expression<Func<TRecord, bool>> predicate) where TRecord : ContentPartRecord; IContentQuery<TPart, TRecord> Where<TRecord>(Expression<Func<TRecord, bool>> predicate) where TRecord : ContentPartRecord;
IContentQuery<TPart, TRecord> OrderBy<TRecord>(Expression<Func<TRecord, object>> keySelector) where TRecord : ContentPartRecord; IContentQuery<TPart, TRecord> OrderBy<TRecord>(Expression<Func<TRecord, object>> keySelector) where TRecord : ContentPartRecord;
IContentQuery<TPart, TRecord> OrderByDescending<TRecord>(Expression<Func<TRecord, object>> keySelector) where TRecord : ContentPartRecord; IContentQuery<TPart, TRecord> OrderByDescending<TRecord>(Expression<Func<TRecord, object>> keySelector) where TRecord : ContentPartRecord;
[Obsolete]IContentQuery<TPart, TRecord> OrderBy<TRecord, TKey>(Expression<Func<TRecord, TKey>> keySelector) where TRecord : ContentPartRecord;
[Obsolete]IContentQuery<TPart, TRecord> OrderByDescending<TRecord, TKey>(Expression<Func<TRecord, TKey>> keySelector) where TRecord : ContentPartRecord;
} }
public interface IContentQuery<TPart, TRecord> : IContentQuery<TPart> where TPart : IContent where TRecord : ContentPartRecord { public interface IContentQuery<TPart, TRecord> : IContentQuery<TPart> where TPart : IContent where TRecord : ContentPartRecord {
@@ -32,6 +35,9 @@ namespace Orchard.ContentManagement {
IContentQuery<TPart, TRecord> OrderBy(Expression<Func<TRecord, object>> keySelector); IContentQuery<TPart, TRecord> OrderBy(Expression<Func<TRecord, object>> keySelector);
IContentQuery<TPart, TRecord> OrderByDescending(Expression<Func<TRecord, object>> keySelector); IContentQuery<TPart, TRecord> OrderByDescending(Expression<Func<TRecord, object>> keySelector);
[Obsolete] IContentQuery<TPart, TRecord> OrderBy<TKey>(Expression<Func<TRecord, TKey>> keySelector);
[Obsolete] IContentQuery<TPart, TRecord> OrderByDescending<TKey>(Expression<Func<TRecord, TKey>> keySelector);
IContentQuery<TPart, TRecord> WithQueryHints(QueryHints hints); IContentQuery<TPart, TRecord> WithQueryHints(QueryHints hints);
IContentQuery<TPart, TRecord> WithQueryHintsFor(string contentType); IContentQuery<TPart, TRecord> WithQueryHintsFor(string contentType);
} }