| | 1 | | using Fluorite.Strainer.Models; |
| | 2 | | using Fluorite.Strainer.Services; |
| | 3 | |
|
| | 4 | | namespace Fluorite.Extensions; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// Provides extension methods for appling Strainer processing to <see cref="IQueryable{T}"/>. |
| | 8 | | /// </summary> |
| | 9 | | public static class StrainerProcessorQueryableExtensions |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Processes current <see cref="IQueryable{T}"/> (applies filtering, |
| | 13 | | /// sorting and pagination) using provided processor and model data. |
| | 14 | | /// </summary> |
| | 15 | | /// <typeparam name="T"> |
| | 16 | | /// The type of entity being processed. |
| | 17 | | /// </typeparam> |
| | 18 | | /// <param name="source"> |
| | 19 | | /// The current <see cref="IQueryable{T}"/> instance. |
| | 20 | | /// </param> |
| | 21 | | /// <param name="strainerModel"> |
| | 22 | | /// The <see cref="IStrainerModel"/> containing i.a. filtering data. |
| | 23 | | /// </param> |
| | 24 | | /// <param name="strainerProcessor"> |
| | 25 | | /// The see <see cref="IStrainerProcessor"/> which will apply filtering. |
| | 26 | | /// </param> |
| | 27 | | /// <param name="applyFiltering"> |
| | 28 | | /// A <see cref="bool"/> value indicating whether provided |
| | 29 | | /// <see cref="IQueryable{T}"/> should be filtered. |
| | 30 | | /// </param> |
| | 31 | | /// <param name="applySorting"> |
| | 32 | | /// A <see cref="bool"/> value indicating whether provided |
| | 33 | | /// <see cref="IQueryable{T}"/> should be ordered. |
| | 34 | | /// </param> |
| | 35 | | /// <param name="applyPagination"> |
| | 36 | | /// A <see cref="bool"/> value indicating whether provided |
| | 37 | | /// <see cref="IQueryable{T}"/> should be paginated. |
| | 38 | | /// </param> |
| | 39 | | /// <returns> |
| | 40 | | /// An instance of <see cref="IQueryable{T}"/> with filtering applied. |
| | 41 | | /// </returns> |
| | 42 | | /// <exception cref="ArgumentNullException"> |
| | 43 | | /// <paramref name="source"/> is <see langword="null"/>. |
| | 44 | | /// </exception> |
| | 45 | | /// <exception cref="ArgumentNullException"> |
| | 46 | | /// <paramref name="strainerProcessor"/> is <see langword="null"/>. |
| | 47 | | /// </exception> |
| | 48 | | public static IQueryable<T> Apply<T>( |
| | 49 | | this IQueryable<T> source, |
| | 50 | | IStrainerModel strainerModel, |
| | 51 | | IStrainerProcessor strainerProcessor, |
| | 52 | | bool applyFiltering = true, |
| | 53 | | bool applySorting = true, |
| | 54 | | bool applyPagination = true) |
| | 55 | | { |
| 2 | 56 | | Guard.Against.Null(source); |
| 2 | 57 | | Guard.Against.Null(strainerProcessor); |
| | 58 | |
|
| 2 | 59 | | if (strainerModel is null) |
| | 60 | | { |
| 1 | 61 | | return source; |
| | 62 | | } |
| | 63 | |
|
| 1 | 64 | | source = strainerProcessor.Apply(strainerModel, source, applyFiltering, applySorting, applyPagination); |
| | 65 | |
|
| 1 | 66 | | return source; |
| | 67 | | } |
| | 68 | |
|
| | 69 | | /// <summary> |
| | 70 | | /// Applies filtering to current <see cref="IQueryable{T}"/> using |
| | 71 | | /// provided processor and model data. |
| | 72 | | /// </summary> |
| | 73 | | /// <typeparam name="T"> |
| | 74 | | /// The type of entity being processed. |
| | 75 | | /// </typeparam> |
| | 76 | | /// <param name="source"> |
| | 77 | | /// The current <see cref="IQueryable{T}"/> instance. |
| | 78 | | /// </param> |
| | 79 | | /// <param name="strainerModel"> |
| | 80 | | /// The <see cref="IStrainerModel"/> containing i.a. filtering data. |
| | 81 | | /// </param> |
| | 82 | | /// <param name="strainerProcessor"> |
| | 83 | | /// The see <see cref="IStrainerProcessor"/> which will apply filtering. |
| | 84 | | /// </param> |
| | 85 | | /// <returns> |
| | 86 | | /// An instance of <see cref="IQueryable{T}"/> with filtering applied. |
| | 87 | | /// </returns> |
| | 88 | | /// <exception cref="ArgumentNullException"> |
| | 89 | | /// <paramref name="source"/> is <see langword="null"/>. |
| | 90 | | /// </exception> |
| | 91 | | /// <exception cref="ArgumentNullException"> |
| | 92 | | /// <paramref name="strainerProcessor"/> is <see langword="null"/>. |
| | 93 | | /// </exception> |
| | 94 | | public static IQueryable<T> ApplyFiltering<T>( |
| | 95 | | this IQueryable<T> source, |
| | 96 | | IStrainerModel strainerModel, |
| | 97 | | IStrainerProcessor strainerProcessor) |
| | 98 | | { |
| 2 | 99 | | Guard.Against.Null(source); |
| 2 | 100 | | Guard.Against.Null(strainerProcessor); |
| | 101 | |
|
| 2 | 102 | | if (strainerModel is null) |
| | 103 | | { |
| 1 | 104 | | return source; |
| | 105 | | } |
| | 106 | |
|
| 1 | 107 | | return strainerProcessor.ApplyFiltering(strainerModel, source); |
| | 108 | | } |
| | 109 | |
|
| | 110 | | /// <summary> |
| | 111 | | /// Applies pagination to current <see cref="IQueryable{T}"/> using |
| | 112 | | /// provided processor and model data. |
| | 113 | | /// </summary> |
| | 114 | | /// <typeparam name="T"> |
| | 115 | | /// The type of entity being processed. |
| | 116 | | /// </typeparam> |
| | 117 | | /// <param name="source"> |
| | 118 | | /// The current <see cref="IQueryable{T}"/> instance. |
| | 119 | | /// </param> |
| | 120 | | /// <param name="strainerModel"> |
| | 121 | | /// The <see cref="IStrainerModel"/> containing i.a. pagination data. |
| | 122 | | /// </param> |
| | 123 | | /// <param name="strainerProcessor"> |
| | 124 | | /// The see <see cref="IStrainerProcessor"/> which will apply pagination. |
| | 125 | | /// </param> |
| | 126 | | /// <returns> |
| | 127 | | /// An instance of <see cref="IQueryable{T}"/> with pagination applied. |
| | 128 | | /// </returns> |
| | 129 | | /// <exception cref="ArgumentNullException"> |
| | 130 | | /// <paramref name="source"/> is <see langword="null"/>. |
| | 131 | | /// </exception> |
| | 132 | | /// <exception cref="ArgumentNullException"> |
| | 133 | | /// <paramref name="strainerProcessor"/> is <see langword="null"/>. |
| | 134 | | /// </exception> |
| | 135 | | public static IQueryable<T> ApplyPagination<T>( |
| | 136 | | this IQueryable<T> source, |
| | 137 | | IStrainerModel strainerModel, |
| | 138 | | IStrainerProcessor strainerProcessor) |
| | 139 | | { |
| 2 | 140 | | Guard.Against.Null(source); |
| 2 | 141 | | Guard.Against.Null(strainerProcessor); |
| | 142 | |
|
| 2 | 143 | | if (strainerModel is null) |
| | 144 | | { |
| 1 | 145 | | return source; |
| | 146 | | } |
| | 147 | |
|
| 1 | 148 | | return strainerProcessor.ApplyPagination(strainerModel, source); |
| | 149 | | } |
| | 150 | |
|
| | 151 | | /// <summary> |
| | 152 | | /// Applies sorting to current <see cref="IQueryable{T}"/> using |
| | 153 | | /// provided processor and model data. |
| | 154 | | /// </summary> |
| | 155 | | /// <typeparam name="T"> |
| | 156 | | /// The type of entity being processed. |
| | 157 | | /// </typeparam> |
| | 158 | | /// <param name="source"> |
| | 159 | | /// The current <see cref="IQueryable{T}"/> instance. |
| | 160 | | /// </param> |
| | 161 | | /// <param name="strainerModel"> |
| | 162 | | /// The <see cref="IStrainerModel"/> containing i.a. sorting. |
| | 163 | | /// </param> |
| | 164 | | /// <param name="strainerProcessor"> |
| | 165 | | /// The see <see cref="IStrainerProcessor"/> which will apply sorting. |
| | 166 | | /// </param> |
| | 167 | | /// <returns> |
| | 168 | | /// An instance of <see cref="IQueryable{T}"/> with sorting applied. |
| | 169 | | /// </returns> |
| | 170 | | /// <exception cref="ArgumentNullException"> |
| | 171 | | /// <paramref name="source"/> is <see langword="null"/>. |
| | 172 | | /// </exception> |
| | 173 | | /// <exception cref="ArgumentNullException"> |
| | 174 | | /// <paramref name="strainerProcessor"/> is <see langword="null"/>. |
| | 175 | | /// </exception> |
| | 176 | | public static IQueryable<T> ApplySorting<T>( |
| | 177 | | this IQueryable<T> source, |
| | 178 | | IStrainerModel strainerModel, |
| | 179 | | IStrainerProcessor strainerProcessor) |
| | 180 | | { |
| 2 | 181 | | Guard.Against.Null(source); |
| 2 | 182 | | Guard.Against.Null(strainerProcessor); |
| | 183 | |
|
| 2 | 184 | | if (strainerModel is null) |
| | 185 | | { |
| 1 | 186 | | return source; |
| | 187 | | } |
| | 188 | |
|
| 1 | 189 | | return strainerProcessor.ApplySorting(strainerModel, source); |
| | 190 | | } |
| | 191 | | } |