| | 1 | | using Fluorite.Strainer.Models; |
| | 2 | | using Fluorite.Strainer.Services.Pipelines; |
| | 3 | |
|
| | 4 | | namespace Fluorite.Strainer.Services; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// Default implementation of Strainer main service taking care of filtering, |
| | 8 | | /// sorting and pagination. |
| | 9 | | /// </summary> |
| | 10 | | public class StrainerProcessor : IStrainerProcessor |
| | 11 | | { |
| | 12 | | private readonly IStrainerPipelineBuilderFactory _strainerPipelineBuilderFactory; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Initializes a new instance of the <see cref="StrainerProcessor"/> class |
| | 16 | | /// with specified context. |
| | 17 | | /// </summary> |
| | 18 | | /// <param name="strainerPipelineBuilderFactory"></param> |
| | 19 | | /// <exception cref="ArgumentNullException"> |
| | 20 | | /// <paramref name="strainerPipelineBuilderFactory"/> is <see langword="null"/>. |
| | 21 | | /// </exception> |
| 5 | 22 | | public StrainerProcessor( |
| 5 | 23 | | IStrainerPipelineBuilderFactory strainerPipelineBuilderFactory) |
| | 24 | | { |
| 5 | 25 | | _strainerPipelineBuilderFactory = Guard.Against.Null(strainerPipelineBuilderFactory); |
| 5 | 26 | | } |
| | 27 | |
|
| | 28 | | /// <inheritdoc/> |
| | 29 | | /// <exception cref="ArgumentNullException"> |
| | 30 | | /// <paramref name="model"/> is <see langword="null"/>. |
| | 31 | | /// </exception> |
| | 32 | | /// <exception cref="ArgumentNullException"> |
| | 33 | | /// <paramref name="source"/> is <see langword="null"/>. |
| | 34 | | /// </exception> |
| | 35 | | public IQueryable<TEntity> Apply<TEntity>( |
| | 36 | | IStrainerModel model, |
| | 37 | | IQueryable<TEntity> source, |
| | 38 | | bool applyFiltering = true, |
| | 39 | | bool applySorting = true, |
| | 40 | | bool applyPagination = true) |
| | 41 | | { |
| 1 | 42 | | Guard.Against.Null(model); |
| 1 | 43 | | Guard.Against.Null(source); |
| | 44 | |
|
| 1 | 45 | | var builder = _strainerPipelineBuilderFactory.CreateBuilder(); |
| | 46 | |
|
| 1 | 47 | | if (applyFiltering) |
| | 48 | | { |
| 1 | 49 | | builder.Filter(); |
| | 50 | | } |
| | 51 | |
|
| 1 | 52 | | if (applySorting) |
| | 53 | | { |
| 1 | 54 | | builder.Sort(); |
| | 55 | | } |
| | 56 | |
|
| 1 | 57 | | if (applyPagination) |
| | 58 | | { |
| 1 | 59 | | builder.Paginate(); |
| | 60 | | } |
| | 61 | |
|
| 1 | 62 | | var pipeline = builder.Build(); |
| | 63 | |
|
| 1 | 64 | | return pipeline.Run(model, source); |
| | 65 | | } |
| | 66 | |
|
| | 67 | | /// <inheritdoc/> |
| | 68 | | /// <exception cref="ArgumentNullException"> |
| | 69 | | /// <paramref name="model"/> is <see langword="null"/>. |
| | 70 | | /// </exception> |
| | 71 | | /// <exception cref="ArgumentNullException"> |
| | 72 | | /// <paramref name="source"/> is <see langword="null"/>. |
| | 73 | | /// </exception> |
| | 74 | | public IQueryable<TEntity> ApplyFiltering<TEntity>( |
| | 75 | | IStrainerModel model, |
| | 76 | | IQueryable<TEntity> source) |
| | 77 | | { |
| 1 | 78 | | Guard.Against.Null(model); |
| 1 | 79 | | Guard.Against.Null(source); |
| | 80 | |
|
| 1 | 81 | | var pipeline = _strainerPipelineBuilderFactory |
| 1 | 82 | | .CreateBuilder() |
| 1 | 83 | | .Filter() |
| 1 | 84 | | .Build(); |
| | 85 | |
|
| 1 | 86 | | return pipeline.Run(model, source); |
| | 87 | | } |
| | 88 | |
|
| | 89 | | /// <inheritdoc/> |
| | 90 | | /// <exception cref="ArgumentNullException"> |
| | 91 | | /// <paramref name="model"/> is <see langword="null"/>. |
| | 92 | | /// </exception> |
| | 93 | | /// <exception cref="ArgumentNullException"> |
| | 94 | | /// <paramref name="source"/> is <see langword="null"/>. |
| | 95 | | /// </exception> |
| | 96 | | public IQueryable<TEntity> ApplyPagination<TEntity>( |
| | 97 | | IStrainerModel model, |
| | 98 | | IQueryable<TEntity> source) |
| | 99 | | { |
| 1 | 100 | | Guard.Against.Null(model); |
| 1 | 101 | | Guard.Against.Null(source); |
| | 102 | |
|
| 1 | 103 | | var pipeline = _strainerPipelineBuilderFactory |
| 1 | 104 | | .CreateBuilder() |
| 1 | 105 | | .Paginate() |
| 1 | 106 | | .Build(); |
| | 107 | |
|
| 1 | 108 | | return pipeline.Run(model, source); |
| | 109 | | } |
| | 110 | |
|
| | 111 | | /// <inheritdoc/> |
| | 112 | | /// <exception cref="ArgumentNullException"> |
| | 113 | | /// <paramref name="model"/> is <see langword="null"/>. |
| | 114 | | /// </exception> |
| | 115 | | /// <exception cref="ArgumentNullException"> |
| | 116 | | /// <paramref name="source"/> is <see langword="null"/>. |
| | 117 | | /// </exception> |
| | 118 | | public IQueryable<TEntity> ApplySorting<TEntity>( |
| | 119 | | IStrainerModel model, |
| | 120 | | IQueryable<TEntity> source) |
| | 121 | | { |
| 1 | 122 | | Guard.Against.Null(model); |
| 1 | 123 | | Guard.Against.Null(source); |
| | 124 | |
|
| 1 | 125 | | var pipeline = _strainerPipelineBuilderFactory |
| 1 | 126 | | .CreateBuilder() |
| 1 | 127 | | .Sort() |
| 1 | 128 | | .Build(); |
| | 129 | |
|
| 1 | 130 | | return pipeline.Run(model, source); |
| | 131 | | } |
| | 132 | | } |