@@ -16,6 +16,22 @@ public static class Paginator
16
16
internal const int DEF_PERPAGE = 10 ;
17
17
internal const bool DEF_SKIPCOUNT = false ;
18
18
19
+ /// <summary>
20
+ /// Asynchronously lists items in the pagination request.
21
+ /// </summary>
22
+ /// <typeparam name="TEntity"></typeparam>
23
+ /// <param name="query"></param>
24
+ /// <param name="page">Page</param>
25
+ /// <param name="perpage">Items per page.</param>
26
+ /// <param name="cancellationToken">Cancellation token.</param>
27
+ /// <returns>A task that represents the asynchronous operation which you can await.</returns>
28
+ /// <exception cref="ArgumentException"/>
29
+ /// <exception cref="ArgumentNullException"/>
30
+ /// <exception cref="OperationCanceledException"/>
31
+ /// <exception cref="ObjectDisposedException"/>
32
+ public static Task < PagedResult < TEntity > > PaginateAsync < TEntity > ( this IQueryable < TEntity > query , int page = DEF_PAGE , int perpage = DEF_PERPAGE , CancellationToken cancellationToken = default )
33
+ where TEntity : class
34
+ => query . ProcessPaginationAsync ( page , perpage , DEF_SKIPCOUNT , cancellationToken ) ;
19
35
/// <summary>
20
36
/// Asynchronously lists items in the pagination request.
21
37
/// </summary>
@@ -24,15 +40,16 @@ public static class Paginator
24
40
/// <param name="page">Page</param>
25
41
/// <param name="perpage">Items per page.</param>
26
42
/// <param name="skipCount">Specify whether to omit running a count operation on your query againt the data store.</param>
27
- /// <param name="token ">Cancellation token.</param>
43
+ /// <param name="cancellationToken ">Cancellation token.</param>
28
44
/// <returns>A task that represents the asynchronous operation which you can await.</returns>
29
45
/// <exception cref="ArgumentException"/>
30
46
/// <exception cref="ArgumentNullException"/>
31
47
/// <exception cref="OperationCanceledException"/>
48
+ /// <exception cref="ObjectDisposedException"/>
32
49
public static Task < PagedResult < TEntity > > PaginateAsync < TEntity > ( this IQueryable < TEntity > query ,
33
- int page = DEF_PAGE , int perpage = DEF_PERPAGE , bool skipCount = DEF_SKIPCOUNT , CancellationToken token = default )
50
+ int page , int perpage , bool skipCount , CancellationToken cancellationToken = default )
34
51
where TEntity : class
35
- => query . ProcessPaginationAsync ( page , perpage , skipCount , token ) ;
52
+ => query . ProcessPaginationAsync ( page , perpage , skipCount , cancellationToken ) ;
36
53
/// <summary>
37
54
/// Lists items in the sequence and only returns the specified number.
38
55
/// </summary>
@@ -49,21 +66,23 @@ public static PagedResult<TEntity> Paginate<TEntity>(this IQueryable<TEntity> qu
49
66
where TEntity : class
50
67
=> query . ProcessPagination ( page , perpage , skipCount ) ;
51
68
69
+
70
+
52
71
internal static async Task < PagedResult < TEntity > > ProcessPaginationAsync < TEntity > ( this IQueryable < TEntity > query ,
53
- int page = DEF_PAGE , int perpage = DEF_PERPAGE , bool skipCount = DEF_SKIPCOUNT , CancellationToken token = default )
72
+ int page = DEF_PAGE , int perpage = DEF_PERPAGE , bool skipCount = DEF_SKIPCOUNT , CancellationToken cancellationToken = default )
54
73
{
55
74
ValidateParams_IfInvalid_Throw ( page , perpage ) ;
56
75
57
76
int total = 0 ;
58
77
var list = new List < TEntity > ( ) ;
59
78
60
- token . ThrowIfCancellationRequested ( ) ;
79
+ cancellationToken . ThrowIfCancellationRequested ( ) ;
61
80
62
81
if ( ! skipCount )
63
- total = await query . CountEntitiesAsync ( token ) ;
82
+ total = await query . CountEntitiesAsync ( cancellationToken ) ;
64
83
65
84
if ( skipCount || ( ! skipCount && total > 0 ) )
66
- list = await query . Skip ( ( page - 1 ) * perpage ) . Take ( perpage ) . ToListAsync ( token ) ;
85
+ list = await query . Skip ( ( page - 1 ) * perpage ) . Take ( perpage ) . ToListAsync ( cancellationToken ) ;
67
86
68
87
if ( skipCount )
69
88
total = list . Count ;
@@ -104,19 +123,11 @@ internal static PagedResult<TEntity> ProcessPagination<TEntity>(this IQueryable<
104
123
} ;
105
124
}
106
125
107
- internal static void ValidateParams_IfInvalid_Throw ( int page , int perpage )
108
- {
109
- if ( page <= 0 )
110
- throw new ArgumentException ( "Page parameter must be greater than zero." , nameof ( page ) ) ;
111
126
112
- if ( perpage < 0 )
113
- throw new ArgumentException ( "Per-page parameter must be 0 or greater than 0." , nameof ( perpage ) ) ;
114
127
115
- return ;
116
- }
117
- internal static Task < int > CountEntitiesAsync < TEntity > ( this IQueryable < TEntity > query , CancellationToken token = default )
128
+ internal static Task < int > CountEntitiesAsync < TEntity > ( this IQueryable < TEntity > query , CancellationToken cancellationToken = default )
118
129
{
119
- return query . CountAsync ( token ) ;
130
+ return query . CountAsync ( cancellationToken ) ;
120
131
}
121
132
internal static int CalculateTotalPages ( int totalItems , int perpage )
122
133
{
@@ -128,5 +139,18 @@ internal static int CalculateTotalPages(int totalItems, int perpage)
128
139
}
129
140
return ans ;
130
141
}
142
+
143
+
144
+
145
+ internal static void ValidateParams_IfInvalid_Throw ( int page , int perpage )
146
+ {
147
+ if ( page <= 0 )
148
+ throw new ArgumentException ( "Page parameter must be greater than zero." , nameof ( page ) ) ;
149
+
150
+ if ( perpage < 0 )
151
+ throw new ArgumentException ( "Per-page parameter must be 0 or greater than 0." , nameof ( perpage ) ) ;
152
+
153
+ return ;
154
+ }
131
155
}
132
156
}
0 commit comments