-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathGridViewDataSetExtensions.cs
More file actions
143 lines (126 loc) · 8.23 KB
/
GridViewDataSetExtensions.cs
File metadata and controls
143 lines (126 loc) · 8.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace DotVVM.Framework.Controls
{
public static class GridViewDataSetExtensions
{
/// <summary>
/// Loads dataset from the specified <paramref name="queryable" />: Applies filtering, sorting and paging options, and collects the results. If <see cref="PagingOptions"/> is used, the total number of items (after applying filtering) is retrieved and stored in the PagingOptions property.
/// </summary>
public static void LoadFromQueryable<T>(this IGridViewDataSet<T> dataSet, IQueryable<T> queryable)
{
if (dataSet.FilteringOptions is not IApplyToQueryable filteringOptions)
{
throw new ArgumentException($"The FilteringOptions of {dataSet.GetType()} must implement IApplyToQueryable!");
}
if (dataSet.SortingOptions is not IApplyToQueryable sortingOptions)
{
throw new ArgumentException($"The SortingOptions of {dataSet.GetType()} must implement IApplyToQueryable!");
}
if (dataSet.PagingOptions is not IApplyToQueryable pagingOptions)
{
throw new ArgumentException($"The PagingOptions of {dataSet.GetType()} must implement IApplyToQueryable!");
}
var filtered = filteringOptions.ApplyToQueryable(queryable);
var sorted = sortingOptions.ApplyToQueryable(filtered);
var paged = pagingOptions.ApplyToQueryable(sorted);
dataSet.Items = paged.ToList();
if (pagingOptions is IPagingOptionsLoadingPostProcessor pagingOptionsLoadingPostProcessor)
{
pagingOptionsLoadingPostProcessor.ProcessLoadedItems(filtered, dataSet.Items);
}
dataSet.IsRefreshRequired = false;
}
#if NET6_0_OR_GREATER
/// <summary>
/// Loads dataset from the specified <paramref name="queryable" />: Applies filtering, sorting and paging options, and collects the results using IAsyncEnumerable. If <see cref="PagingOptions"/> is used, the total number of items (after applying filtering) is retrieved and stored in the PagingOptions property.
/// </summary>
/// <exception cref="ArgumentException">The specified IQueryable does not support async enumeration.</exception>
public static async Task LoadFromQueryableAsync<T>(this IGridViewDataSet<T> dataSet, IQueryable<T> queryable, CancellationToken cancellationToken = default)
{
if (dataSet.FilteringOptions is not IApplyToQueryable filteringOptions)
{
throw new ArgumentException($"The FilteringOptions of {dataSet.GetType()} must implement IApplyToQueryable!");
}
if (dataSet.SortingOptions is not IApplyToQueryable sortingOptions)
{
throw new ArgumentException($"The SortingOptions of {dataSet.GetType()} must implement IApplyToQueryable!");
}
if (dataSet.PagingOptions is not IApplyToQueryable pagingOptions)
{
throw new ArgumentException($"The PagingOptions of {dataSet.GetType()} must implement IApplyToQueryable!");
}
var filtered = filteringOptions.ApplyToQueryable(queryable);
var sorted = sortingOptions.ApplyToQueryable(filtered);
var paged = pagingOptions.ApplyToQueryable(sorted);
if (paged is not IAsyncEnumerable<T> asyncPaged)
{
throw new ArgumentException($"The specified IQueryable ({queryable.GetType().FullName}), does not support async enumeration. Please use the LoadFromQueryable method.", nameof(queryable));
}
var result = new List<T>();
await foreach (var item in asyncPaged.WithCancellation(cancellationToken))
{
result.Add(item);
}
dataSet.Items = result;
if (pagingOptions is IPagingOptionsLoadingPostProcessor pagingOptionsLoadingPostProcessor)
{
cancellationToken.ThrowIfCancellationRequested();
await pagingOptionsLoadingPostProcessor.ProcessLoadedItemsAsync(filtered, result, cancellationToken);
}
dataSet.IsRefreshRequired = false;
}
#endif
/// <summary> Sets <see cref="IPagingOptions"/> to the first page, and sets the <see cref="IRefreshableGridViewDataSet.IsRefreshRequired"/> property to true. </summary>
/// <remarks> To reload the dataset, you must then call <see cref="LoadFromQueryable{T}(IGridViewDataSet{T}, IQueryable{T})"/> or a similar method. </remarks>
public static void GoToFirstPageAndRefresh(this IPageableGridViewDataSet<IPagingFirstPageCapability> dataSet)
{
dataSet.PagingOptions.GoToFirstPage();
(dataSet as IRefreshableGridViewDataSet)?.RequestRefresh();
}
/// <summary> Sets <see cref="IPagingOptions"/> to the last page, and sets the <see cref="IRefreshableGridViewDataSet.IsRefreshRequired"/> property to true. </summary>
/// <remarks> To reload the dataset, you must then call <see cref="LoadFromQueryable{T}(IGridViewDataSet{T}, IQueryable{T})"/> or a similar method. </remarks>
public static void GoToLastPageAndRefresh(this IPageableGridViewDataSet<IPagingLastPageCapability> dataSet)
{
dataSet.PagingOptions.GoToLastPage();
(dataSet as IRefreshableGridViewDataSet)?.RequestRefresh();
}
/// <summary> Sets <see cref="IPagingOptions"/> to the previous page, and sets the <see cref="IRefreshableGridViewDataSet.IsRefreshRequired"/> property to true. </summary>
/// <remarks> To reload the dataset, you must then call <see cref="LoadFromQueryable{T}(IGridViewDataSet{T}, IQueryable{T})"/> or a similar method. </remarks>
public static void GoToPreviousPageAndRefresh(this IPageableGridViewDataSet<IPagingPreviousPageCapability> dataSet)
{
dataSet.PagingOptions.GoToPreviousPage();
(dataSet as IRefreshableGridViewDataSet)?.RequestRefresh();
}
/// <summary> Sets <see cref="IPagingOptions"/> to the next page, and sets the <see cref="IRefreshableGridViewDataSet.IsRefreshRequired"/> property to true. </summary>
/// <remarks> To reload the dataset, you must then call <see cref="LoadFromQueryable{T}(IGridViewDataSet{T}, IQueryable{T})"/> or a similar method. </remarks>
public static void GoToNextPageAndRefresh(this IPageableGridViewDataSet<IPagingNextPageCapability> dataSet)
{
dataSet.PagingOptions.GoToNextPage();
(dataSet as IRefreshableGridViewDataSet)?.RequestRefresh();
}
/// <summary> Sets <see cref="IPagingOptions"/> to the page number <paramref name="pageIndex"/> (indexed from 0), and sets the <see cref="IRefreshableGridViewDataSet.IsRefreshRequired"/> property to true. </summary>
/// <remarks> To reload the dataset, you must then call <see cref="LoadFromQueryable{T}(IGridViewDataSet{T}, IQueryable{T})"/> or a similar method. </remarks>
public static void GoToPageAndRefresh(this IPageableGridViewDataSet<IPagingPageIndexCapability> dataSet, int pageIndex)
{
dataSet.PagingOptions.GoToPage(pageIndex);
(dataSet as IRefreshableGridViewDataSet)?.RequestRefresh();
}
public static async Task LoadAsync<T, TFilteringOptions, TSortingOptions, TPagingOptions, TRowInsertOptions, TRowEditOptions>(
this GenericGridViewDataSet<T, TFilteringOptions, TSortingOptions, TPagingOptions, TRowInsertOptions, TRowEditOptions> dataSet,
Func<GridViewDataSetOptions<TFilteringOptions, TSortingOptions, TPagingOptions>, Task<GridViewDataSetResult<T, TFilteringOptions, TSortingOptions, TPagingOptions>>> loadMethod)
where TFilteringOptions : IFilteringOptions
where TSortingOptions : ISortingOptions
where TPagingOptions : IPagingOptions
where TRowInsertOptions : IRowInsertOptions
where TRowEditOptions : IRowEditOptions
{
var options = dataSet.GetOptions();
var result = await loadMethod(options);
dataSet.ApplyResult(result);
}
}
}