Skip to content

Commit f3caabb

Browse files
committed
Non compiling and maybe failing spike on distinct
1 parent 65a329b commit f3caabb

29 files changed

Lines changed: 998 additions & 32 deletions

SharpRepository.Repository/LinqRepositoryBase.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,29 @@ protected override IQueryable<T> GetAllQuery(IQueryOptions<T> queryOptions, IFet
7070
return query;
7171
}
7272

73+
protected override IQueryable<TResult> GetAllQuery<TResult>(IQueryOptions<T> queryOptions, IFetchStrategy<T> fetchStrategy, Expression<Func<T, TResult>> selector)
74+
{
75+
if (queryOptions is DistinctOption<T> || queryOptions is DistinctSortingOptions<T> || queryOptions is DistinctPagingOptions<T> || queryOptions is DistinctSortingOptions<TResult> || queryOptions is DistinctPagingOptions<T, TResult>)
76+
{
77+
if (queryOptions == null)
78+
return GetAllQuery(fetchStrategy).Select(selector);
79+
80+
var query = BaseQuery(fetchStrategy).Select(selector);
81+
82+
query = queryOptions.Apply(query);
83+
84+
SetTraceInfo("GetAll", query);
85+
86+
return query;
87+
} else
88+
{
89+
return GetAllQuery(queryOptions, fetchStrategy).Select(selector);
90+
}
91+
92+
93+
}
94+
95+
7396
protected override IQueryable<T> FindAllQuery(ISpecification<T> criteria)
7497
{
7598
var query = BaseQuery(criteria.FetchStrategy);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Linq;
2+
3+
namespace SharpRepository.Repository.Queries
4+
{
5+
/// <summary>
6+
/// Used to define the paging and/or sorting criteria on queries run against a repository.
7+
/// </summary>
8+
/// <typeparam name="T">The entity type of the repository.</typeparam>
9+
public class DistinctOption<T> : IQueryOptions<T>, IPostSelectorQueryOptions<T>
10+
{
11+
public IQueryable<T> Apply(IQueryable<T> query)
12+
{
13+
return query.Distinct();
14+
}
15+
16+
public IQueryable<TResult> Apply<TResult>(IQueryable<TResult> query)
17+
{
18+
return query.Distinct();
19+
}
20+
}
21+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using System;
2+
using System.Linq;
3+
using System.Linq.Expressions;
4+
5+
namespace SharpRepository.Repository.Queries
6+
{
7+
/// <summary>
8+
/// Used to define the paging criteria on queries run against a repository.
9+
/// </summary>
10+
/// <typeparam name="T">The entity type of the repository.</typeparam>
11+
/// <typeparam name="TSortKey">The type of the property that is being sorted.</typeparam>
12+
public class DistinctPagingOptions<T, TSortKey> : PagingOptions<T, TSortKey>
13+
{
14+
public DistinctPagingOptions(int pageNumber, int pageSize, Expression<Func<T, TSortKey>> sortExpression, bool isDescending = false)
15+
: base(pageNumber, pageSize, sortExpression, isDescending) { }
16+
17+
/// <summary>
18+
/// Applies paging to the specified query.
19+
/// </summary>
20+
/// <param name="query">The query.</param>
21+
/// <returns>Paged results.</returns>
22+
public override IQueryable<T> Apply(IQueryable<T> query)
23+
{
24+
var distinctQuery = query.Distinct();
25+
return base.Apply(distinctQuery);
26+
}
27+
28+
/// <summary>
29+
/// Used in compiling a unique key for a query
30+
/// </summary>
31+
/// <returns>Unique key for a query</returns>
32+
public override string ToString()
33+
{
34+
return "Distinct" + base.ToString();
35+
}
36+
}
37+
38+
/// <summary>
39+
/// Used to define the paging criteria on queries run against a repository.
40+
/// </summary>
41+
/// <typeparam name="T">The entity type of the repository.</typeparam>
42+
public class DistinctPagingOptions<T> : DistinctSortingOptions<T>, IPagingOptions
43+
{
44+
public int PageSize { get; set; }
45+
public int PageNumber { get; set; }
46+
public int Skip { get { return (PageNumber - 1) * PageSize; } }
47+
public int Take { get { return PageSize; } }
48+
public int TotalItems { get; set; }
49+
50+
public DistinctPagingOptions(int pageNumber, int pageSize, string sortProperty, bool isDescending = false)
51+
: base(sortProperty, isDescending)
52+
{
53+
PageSize = pageSize;
54+
PageNumber = pageNumber;
55+
}
56+
57+
58+
/// <summary>
59+
/// Applies paging to the specified query.
60+
/// </summary>
61+
/// <param name="query">The query.</param>
62+
/// <returns>Paged results.</returns>
63+
public override IQueryable<T> Apply(IQueryable<T> query)
64+
{
65+
query = base.Apply(query);
66+
67+
TotalItems = query.Count();
68+
69+
if (Skip > 0 || Take > 0)
70+
{
71+
return query.Skip(Skip).Take(Take);
72+
}
73+
74+
return query;
75+
}
76+
77+
/// <summary>
78+
/// Applies paging to the specified query.
79+
/// </summary>
80+
/// <param name="query">The query.</param>
81+
/// <returns>Paged results.</returns>
82+
public override IQueryable<TResult> Apply<TResult>(IQueryable<TResult> query)
83+
{
84+
query = base.Apply(query);
85+
86+
TotalItems = query.Count();
87+
88+
if (Skip > 0 || Take > 0)
89+
{
90+
return query.Skip(Skip).Take(Take);
91+
}
92+
93+
return query;
94+
}
95+
96+
/// <summary>
97+
/// Used in compiling a unique key for a query
98+
/// </summary>
99+
/// <returns>Unique key for a query</returns>
100+
public override string ToString()
101+
{
102+
return string.Format("DistinctPagingOptions<{0}>\nPageSize: {1}\nPageNumber: {2}\nSort: {3}",
103+
typeof(T).Name,
104+
PageSize,
105+
PageNumber,
106+
base.ToString()
107+
);
108+
}
109+
}
110+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Linq;
3+
using System.Linq.Expressions;
4+
5+
namespace SharpRepository.Repository.Queries
6+
{
7+
/// <summary>
8+
/// Used to define the sorting on queries with distinct run against a repository.
9+
/// </summary>
10+
/// <typeparam name="T">The entity type of the repository.</typeparam>
11+
/// <typeparam name="TSortKey">The type of the property that is being sorted.</typeparam>
12+
public class DistinctSortingOptions<T, TSortKey> : SortingOptions<T, TSortKey>
13+
{
14+
public DistinctSortingOptions(Expression<Func<T, TSortKey>> sortExpression, bool isDescending = false) : base(sortExpression, isDescending)
15+
{
16+
}
17+
18+
/// <summary>
19+
/// Applies sorting to the specified query.
20+
/// </summary>
21+
/// <param name="query">The query.</param>
22+
/// <returns>Sorted results.</returns>
23+
public override IQueryable<T> Apply(IQueryable<T> query)
24+
{
25+
return base.Apply(query).Distinct();
26+
}
27+
28+
/// <summary>
29+
/// Used in compiling a unique key for a query
30+
/// </summary>
31+
/// <returns>Unique key for a query</returns>
32+
public override string ToString()
33+
{
34+
return "Distinct" + base.ToString();
35+
}
36+
}
37+
38+
/// <summary>
39+
/// Used to define the sorting on queries with distinct run against a repository.
40+
/// </summary>
41+
/// <typeparam name="T">The entity type of the repository.</typeparam>
42+
public class DistinctSortingOptions<T> : SortingOptions<T>
43+
{
44+
public DistinctSortingOptions(string sortProperty, bool isDescending = false) : base(sortProperty, isDescending) { }
45+
46+
/// <summary>
47+
/// Applies sorting to the specified query.
48+
/// </summary>
49+
/// <param name="query">The query.</param>
50+
/// <returns>Sorted results.</returns>
51+
public override IQueryable<T> Apply(IQueryable<T> query)
52+
{
53+
return base.Apply(query).Distinct();
54+
}
55+
56+
/// <summary>
57+
/// Used in compiling a unique key for a query
58+
/// </summary>
59+
/// <returns>Unique key for a query</returns>
60+
public override string ToString()
61+
{
62+
return "Distinct" + base.ToString();
63+
}
64+
}
65+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Linq;
2+
3+
namespace SharpRepository.Repository.Queries
4+
{
5+
/// <summary>
6+
/// Used to define the paging and/or sorting criteria on queries run against a repository.
7+
/// </summary>
8+
/// <typeparam name="T">The entity type of the repository.</typeparam>
9+
public interface IPostSelectorQueryOptions<T>
10+
{
11+
IQueryable<TResult> Apply<TResult>(IQueryable<TResult> query);
12+
}
13+
}

SharpRepository.Repository/Queries/PagingOptions.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ public override IQueryable<T> Apply(IQueryable<T> query)
4949
/// <returns>Unique key for a query</returns>
5050
public override string ToString()
5151
{
52-
return String.Format("PagingOptions<{0},{1}>\nPageSize: {2}\nPageNumber: {3}\nSort: {4}",
53-
(typeof(T)).Name,
54-
(typeof(TSortKey)).Name,
52+
return string.Format("PagingOptions<{0},{1}>\nPageSize: {2}\nPageNumber: {3}\nSort: {4}",
53+
typeof(T).Name,
54+
typeof(TSortKey).Name,
5555
PageSize,
5656
PageNumber,
5757
base.ToString()
@@ -97,14 +97,33 @@ public override IQueryable<T> Apply(IQueryable<T> query)
9797
return query;
9898
}
9999

100+
/// <summary>
101+
/// Applies paging to the specified query.
102+
/// </summary>
103+
/// <param name="query">The query.</param>
104+
/// <returns>Paged results.</returns>
105+
public override IQueryable<TResult> Apply<TResult>(IQueryable<TResult> query)
106+
{
107+
query = base.Apply(query);
108+
109+
TotalItems = query.Count();
110+
111+
if (Skip > 0 || Take > 0)
112+
{
113+
return query.Skip(Skip).Take(Take);
114+
}
115+
116+
return query;
117+
}
118+
100119
/// <summary>
101120
/// Used in compiling a unique key for a query
102121
/// </summary>
103122
/// <returns>Unique key for a query</returns>
104123
public override string ToString()
105124
{
106-
return String.Format("PagingOptions<{0}>\nPageSize: {1}\nPageNumber: {2}\nSort: {3}",
107-
(typeof(T)).Name,
125+
return string.Format("PagingOptions<{0}>\nPageSize: {1}\nPageNumber: {2}\nSort: {3}",
126+
typeof(T).Name,
108127
PageSize,
109128
PageNumber,
110129
base.ToString()

SharpRepository.Repository/Queries/SortingOptions.cs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public SortingOptions(Expression<Func<T, TSortKey>> sortExpression, bool isDesce
3131
_primarySortAction = q => q.OrderBy(sortExpression);
3232
}
3333

34-
_primarySortToString = String.Format("{0}-{1}", sortExpression, isDescending);
34+
_primarySortToString = string.Format("{0}-{1}", sortExpression, isDescending);
3535
}
3636

3737
public void ThenSortBy<TNewSortKey>(Expression<Func<T, TNewSortKey>> sortExpression, bool isDescending = false)
@@ -48,7 +48,7 @@ public void ThenSortBy<TNewSortKey>(Expression<Func<T, TNewSortKey>> sortExpress
4848
}
4949

5050
_sortActions.Add(sortAction);
51-
_sortActionsToString.Add(String.Format("{0}-{1}", sortExpression, isDescending));
51+
_sortActionsToString.Add(string.Format("{0}-{1}", sortExpression, isDescending));
5252
}
5353

5454

@@ -75,11 +75,11 @@ public virtual IQueryable<T> Apply(IQueryable<T> query)
7575
/// <returns>Unique key for a query</returns>
7676
public override string ToString()
7777
{
78-
return String.Format("SortingOptions<{0},{1}>\nSort: {2}\nExtra: {3}",
79-
(typeof(T)).Name,
80-
(typeof(TSortKey)).Name,
81-
_primarySortToString ?? "null",
82-
String.Join("-", _sortActionsToString)
78+
return string.Format("SortingOptions<{0},{1}>\nSort: {2}\nExtra: {3}",
79+
typeof(T).Name,
80+
typeof(TSortKey).Name,
81+
_primarySortToString ?? "null",
82+
string.Join("-", _sortActionsToString)
8383
);
8484
}
8585
}
@@ -108,7 +108,7 @@ public SortingOptions(string sortProperty, bool isDescending = false)
108108
_primarySortAction = q => q.OrderByProperty(sortProperty);
109109
}
110110

111-
_primarySortToString = String.Format("{0}-{1}", sortProperty, isDescending);
111+
_primarySortToString = string.Format("{0}-{1}", sortProperty, isDescending);
112112
}
113113

114114
public void ThenSortBy(string sortProperty, bool isDescending = false)
@@ -125,7 +125,7 @@ public void ThenSortBy(string sortProperty, bool isDescending = false)
125125
}
126126

127127
_sortActions.Add(sortAction);
128-
_sortActionsToString.Add(String.Format("{0}-{1}", sortProperty, isDescending));
128+
_sortActionsToString.Add(string.Format("{0}-{1}", sortProperty, isDescending));
129129
}
130130

131131
/// <summary>
@@ -147,16 +147,35 @@ public virtual IQueryable<T> Apply(IQueryable<T> query)
147147
return _sortActions.Aggregate(sortedQuery, (current, sortAction) => sortAction(current));
148148
}
149149

150+
/// <summary>
151+
/// Applies sorting to the specified query.
152+
/// </summary>
153+
/// <param name="query">The query.</param>
154+
/// <returns>Sorted results.</returns>
155+
public virtual IQueryable<TResult> Apply<TResult>(IQueryable<TResult> query)
156+
{
157+
// TODO: do we need to deal with the case where the user passes in "Name desc", should we strip the desc out, or let it override the isDescending param, or not deal with it and blame it on the user?
158+
159+
IOrderedQueryable<TResult> sortedQuery = null;
160+
161+
if (_primarySortAction != null)
162+
{
163+
sortedQuery = _primarySortAction(query);
164+
}
165+
166+
return _sortActions.Aggregate(sortedQuery, (current, sortAction) => sortAction(current));
167+
}
168+
150169
/// <summary>
151170
/// Used in compiling a unique key for a query
152171
/// </summary>
153172
/// <returns>Unique key for a query</returns>
154173
public override string ToString()
155174
{
156-
var val = String.Format("SortingOptions<{0}>\nSort: {1}\nExtra: {2}",
157-
(typeof(T)).Name,
158-
_primarySortToString ?? "null",
159-
String.Join("-", _sortActionsToString)
175+
var val = string.Format("SortingOptions<{0}>\nSort: {1}\nExtra: {2}",
176+
typeof(T).Name,
177+
_primarySortToString ?? "null",
178+
string.Join("-", _sortActionsToString)
160179
);
161180
return val;
162181
}

0 commit comments

Comments
 (0)