From: <ric...@us...> - 2010-01-19 12:53:27
|
Revision: 4921 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4921&view=rev Author: ricbrown Date: 2010-01-19 12:53:21 +0000 (Tue, 19 Jan 2010) Log Message: ----------- Improvement to projection list syntax in QueryOver. (Thanks to maciejk for suggesting improvement in NH-Forge blog reply) Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Criterion/Lambda/QueryOverProjectionBuilder.cs trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs trunk/nhibernate/src/NHibernate/IQueryOver.cs trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/ProjectionsFixture.cs Modified: trunk/nhibernate/src/NHibernate/Criterion/Lambda/QueryOverProjectionBuilder.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Criterion/Lambda/QueryOverProjectionBuilder.cs 2010-01-19 10:31:20 UTC (rev 4920) +++ trunk/nhibernate/src/NHibernate/Criterion/Lambda/QueryOverProjectionBuilder.cs 2010-01-19 12:53:21 UTC (rev 4921) @@ -9,18 +9,14 @@ namespace NHibernate.Criterion.Lambda { - public class QueryOverProjectionBuilder<TReturn, TRoot, TSubType> + public class QueryOverProjectionBuilder<T> { - private TReturn fluentReturn; - private IQueryOver<TRoot,TSubType> criteria; private ProjectionList projectionList; private IProjection lastProjection = null; - public QueryOverProjectionBuilder(TReturn fluentReturn, IQueryOver<TRoot,TSubType> criteria) + public QueryOverProjectionBuilder() { - this.fluentReturn = fluentReturn; - this.criteria = criteria; projectionList = Projections.ProjectionList(); } @@ -36,23 +32,19 @@ lastProjection = projection; } - /// <summary> - /// Create the ProjectionList and return to the query - /// </summary> - public TReturn EndSelect + internal ProjectionList ProjectionList { get { AddLastProjection(); - criteria.Select(projectionList); - return fluentReturn; + return projectionList; } } /// <summary> /// Create an alias for the previous projection /// </summary> - public QueryOverProjectionBuilder<TReturn, TRoot, TSubType> WithAlias(Expression<Func<object>> alias) + public QueryOverProjectionBuilder<T> WithAlias(Expression<Func<object>> alias) { string aliasContainer = ExpressionProcessor.FindMemberExpression(alias.Body); lastProjection = Projections.Alias(lastProjection, aliasContainer); @@ -62,7 +54,7 @@ /// <summary> /// Select an arbitrary projection /// </summary> - public QueryOverProjectionBuilder<TReturn, TRoot, TSubType> Select(IProjection projection) + public QueryOverProjectionBuilder<T> Select(IProjection projection) { PushProjection(projection); return this; @@ -71,7 +63,7 @@ /// <summary> /// A property average value /// </summary> - public QueryOverProjectionBuilder<TReturn, TRoot, TSubType> SelectAvg(Expression<Func<TSubType, object>> expression) + public QueryOverProjectionBuilder<T> SelectAvg(Expression<Func<T, object>> expression) { PushProjection(Projections.Avg(expression)); return this; @@ -80,7 +72,7 @@ /// <summary> /// A property average value /// </summary> - public QueryOverProjectionBuilder<TReturn, TRoot, TSubType> SelectAvg(Expression<Func<object>> expression) + public QueryOverProjectionBuilder<T> SelectAvg(Expression<Func<object>> expression) { PushProjection(Projections.Avg(expression)); return this; @@ -89,7 +81,7 @@ /// <summary> /// A property value count /// </summary> - public QueryOverProjectionBuilder<TReturn, TRoot, TSubType> SelectCount(Expression<Func<TSubType, object>> expression) + public QueryOverProjectionBuilder<T> SelectCount(Expression<Func<T, object>> expression) { PushProjection(Projections.Count(expression)); return this; @@ -98,7 +90,7 @@ /// <summary> /// A property value count /// </summary> - public QueryOverProjectionBuilder<TReturn, TRoot, TSubType> SelectCount(Expression<Func<object>> expression) + public QueryOverProjectionBuilder<T> SelectCount(Expression<Func<object>> expression) { PushProjection(Projections.Count(expression)); return this; @@ -107,7 +99,7 @@ /// <summary> /// A distinct property value count /// </summary> - public QueryOverProjectionBuilder<TReturn, TRoot, TSubType> SelectCountDistinct(Expression<Func<TSubType, object>> expression) + public QueryOverProjectionBuilder<T> SelectCountDistinct(Expression<Func<T, object>> expression) { PushProjection(Projections.CountDistinct(expression)); return this; @@ -116,7 +108,7 @@ /// <summary> /// A distinct property value count /// </summary> - public QueryOverProjectionBuilder<TReturn, TRoot, TSubType> SelectCountDistinct(Expression<Func<object>> expression) + public QueryOverProjectionBuilder<T> SelectCountDistinct(Expression<Func<object>> expression) { PushProjection(Projections.CountDistinct(expression)); return this; @@ -125,7 +117,7 @@ /// <summary> /// A grouping property value /// </summary> - public QueryOverProjectionBuilder<TReturn, TRoot, TSubType> SelectGroup(Expression<Func<TSubType, object>> expression) + public QueryOverProjectionBuilder<T> SelectGroup(Expression<Func<T, object>> expression) { PushProjection(Projections.Group(expression)); return this; @@ -134,7 +126,7 @@ /// <summary> /// A grouping property value /// </summary> - public QueryOverProjectionBuilder<TReturn, TRoot, TSubType> SelectGroup(Expression<Func<object>> expression) + public QueryOverProjectionBuilder<T> SelectGroup(Expression<Func<object>> expression) { PushProjection(Projections.Group(expression)); return this; @@ -143,7 +135,7 @@ /// <summary> /// A property maximum value /// </summary> - public QueryOverProjectionBuilder<TReturn, TRoot, TSubType> SelectMax(Expression<Func<TSubType, object>> expression) + public QueryOverProjectionBuilder<T> SelectMax(Expression<Func<T, object>> expression) { PushProjection(Projections.Max(expression)); return this; @@ -152,7 +144,7 @@ /// <summary> /// A property maximum value /// </summary> - public QueryOverProjectionBuilder<TReturn, TRoot, TSubType> SelectMax(Expression<Func<object>> expression) + public QueryOverProjectionBuilder<T> SelectMax(Expression<Func<object>> expression) { PushProjection(Projections.Max(expression)); return this; @@ -161,7 +153,7 @@ /// <summary> /// A property minimum value /// </summary> - public QueryOverProjectionBuilder<TReturn, TRoot, TSubType> SelectMin(Expression<Func<TSubType, object>> expression) + public QueryOverProjectionBuilder<T> SelectMin(Expression<Func<T, object>> expression) { PushProjection(Projections.Min(expression)); return this; @@ -170,7 +162,7 @@ /// <summary> /// A property minimum value /// </summary> - public QueryOverProjectionBuilder<TReturn, TRoot, TSubType> SelectMin(Expression<Func<object>> expression) + public QueryOverProjectionBuilder<T> SelectMin(Expression<Func<object>> expression) { PushProjection(Projections.Min(expression)); return this; @@ -179,7 +171,7 @@ /// <summary> /// A projected property value /// </summary> - public QueryOverProjectionBuilder<TReturn, TRoot, TSubType> Select(Expression<Func<TSubType, object>> expression) + public QueryOverProjectionBuilder<T> Select(Expression<Func<T, object>> expression) { PushProjection(Projections.Property(expression)); return this; @@ -188,13 +180,13 @@ /// <summary> /// A projected property value /// </summary> - public QueryOverProjectionBuilder<TReturn, TRoot, TSubType> Select(Expression<Func<object>> expression) + public QueryOverProjectionBuilder<T> Select(Expression<Func<object>> expression) { PushProjection(Projections.Property(expression)); return this; } - public QueryOverProjectionBuilder<TReturn, TRoot, TSubType> SelectSubQuery<U>(QueryOver<U> detachedQueryOver) + public QueryOverProjectionBuilder<T> SelectSubQuery<U>(QueryOver<U> detachedQueryOver) { PushProjection(Projections.SubQuery(detachedQueryOver)); return this; @@ -203,7 +195,7 @@ /// <summary> /// A property value sum /// </summary> - public QueryOverProjectionBuilder<TReturn, TRoot, TSubType> SelectSum(Expression<Func<TSubType, object>> expression) + public QueryOverProjectionBuilder<T> SelectSum(Expression<Func<T, object>> expression) { PushProjection(Projections.Sum(expression)); return this; @@ -212,7 +204,7 @@ /// <summary> /// A property value sum /// </summary> - public QueryOverProjectionBuilder<TReturn, TRoot, TSubType> SelectSum(Expression<Func<object>> expression) + public QueryOverProjectionBuilder<T> SelectSum(Expression<Func<object>> expression) { PushProjection(Projections.Sum(expression)); return this; Modified: trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs 2010-01-19 10:31:20 UTC (rev 4920) +++ trunk/nhibernate/src/NHibernate/Criterion/QueryOver.cs 2010-01-19 12:53:21 UTC (rev 4921) @@ -264,9 +264,10 @@ return this; } - public QueryOverProjectionBuilder<QueryOver<TRoot,TSubType>, TRoot, TSubType> SelectList + public QueryOver<TRoot, TSubType> Select(Func<QueryOverProjectionBuilder<TSubType>, QueryOverProjectionBuilder<TSubType>> list) { - get { return new QueryOverProjectionBuilder<QueryOver<TRoot,TSubType>, TRoot, TSubType>(this, this); } + criteria.SetProjection(list(new QueryOverProjectionBuilder<TSubType>()).ProjectionList); + return this; } public QueryOverOrderBuilder<TRoot,TSubType> OrderBy(Expression<Func<TSubType, object>> path) @@ -604,8 +605,8 @@ IQueryOver<TRoot,TSubType> IQueryOver<TRoot,TSubType>.Select(params IProjection[] projections) { return Select(projections); } - QueryOverProjectionBuilder<IQueryOver<TRoot,TSubType>, TRoot, TSubType> IQueryOver<TRoot,TSubType>.SelectList - { get { return new QueryOverProjectionBuilder<IQueryOver<TRoot,TSubType>,TRoot,TSubType>(this, this); } } + IQueryOver<TRoot, TSubType> IQueryOver<TRoot, TSubType>.Select(Func<QueryOverProjectionBuilder<TSubType>, QueryOverProjectionBuilder<TSubType>> list) + { return Select(list); } IQueryOverOrderBuilder<TRoot,TSubType> IQueryOver<TRoot,TSubType>.OrderBy(Expression<Func<TSubType, object>> path) { return new IQueryOverOrderBuilder<TRoot,TSubType>(this, path); } Modified: trunk/nhibernate/src/NHibernate/IQueryOver.cs =================================================================== --- trunk/nhibernate/src/NHibernate/IQueryOver.cs 2010-01-19 10:31:20 UTC (rev 4920) +++ trunk/nhibernate/src/NHibernate/IQueryOver.cs 2010-01-19 12:53:21 UTC (rev 4921) @@ -208,9 +208,9 @@ IQueryOver<TRoot,TSubType> Select(params IProjection[] projections); /// <summary> - /// Create a list of projections inline + /// Create a list of projections using a projection builder /// </summary> - QueryOverProjectionBuilder<IQueryOver<TRoot,TSubType>, TRoot, TSubType> SelectList { get; } + IQueryOver<TRoot, TSubType> Select(Func<QueryOverProjectionBuilder<TSubType>, QueryOverProjectionBuilder<TSubType>> list); /// <summary> /// Add order expressed as a lambda expression Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs 2010-01-19 10:31:20 UTC (rev 4920) +++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/IntegrationFixture.cs 2010-01-19 12:53:21 UTC (rev 4921) @@ -231,9 +231,7 @@ QueryOver<Child> averageChildAge = QueryOver.Of<Child>() - .SelectList - .SelectAvg(c => c.Age) - .EndSelect; + .Select(p => p.SelectAvg(c => c.Age)); QueryOver<Child> childCountQuery = QueryOver.Of<Child>() @@ -243,11 +241,10 @@ var nameAndChildCount = s.QueryOver<Person>(() => personAlias) .WithSubquery.Where(p => p.Age <= averageChildAge.As<int>()) - .SelectList + .Select(list => list .Select(p => p.Name) - .SelectSubQuery(childCountQuery).WithAlias(() => childCountAlias) - .EndSelect - .OrderBy(() => childCountAlias).Desc + .SelectSubQuery(childCountQuery).WithAlias(() => childCountAlias)) + .OrderBy(() => childCountAlias).Desc .List<object[]>() .Select(props => new { Name = (string)props[0], Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/ProjectionsFixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/ProjectionsFixture.cs 2010-01-19 10:31:20 UTC (rev 4920) +++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/ProjectionsFixture.cs 2010-01-19 12:53:21 UTC (rev 4921) @@ -114,7 +114,7 @@ Person personAgeProjectionAlias = null; var actual = CreateTestQueryOver<Person>(() => personAlias) - .SelectList + .Select(list => list .SelectAvg(p => p.Age).WithAlias(() => personAgeProjectionAlias) .Select(Projections.Avg("Age")) // allows private properties .SelectAvg(() => personAlias.Age) @@ -132,8 +132,7 @@ .Select(() => personAlias.Age) .SelectSubQuery(DetachedQueryOverAge) .SelectSum(p => p.Age) - .SelectSum(() => personAlias.Age) - .EndSelect; + .SelectSum(() => personAlias.Age)); AssertCriteriaAreEqual(expected, actual); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |