| | 1 | | using Fluorite.Strainer.Models; |
| | 2 | | using Fluorite.Strainer.Models.Filtering; |
| | 3 | | using Fluorite.Strainer.Models.Filtering.Operators; |
| | 4 | | using Fluorite.Strainer.Models.Metadata; |
| | 5 | | using Fluorite.Strainer.Models.Sorting; |
| | 6 | | using Fluorite.Strainer.Services.Filtering; |
| | 7 | | using Fluorite.Strainer.Services.Metadata; |
| | 8 | | using Fluorite.Strainer.Services.Sorting; |
| | 9 | | using System.Linq.Expressions; |
| | 10 | | using System.Reflection; |
| | 11 | |
|
| | 12 | | namespace Fluorite.Strainer.Services.Modules; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Default implementation of Strainer Module builder service. |
| | 16 | | /// </summary> |
| | 17 | | /// <typeparam name="T"> |
| | 18 | | /// The type of model for which this module builder is for. |
| | 19 | | /// </typeparam> |
| | 20 | | public class StrainerModuleBuilder<T> : IStrainerModuleBuilder<T> |
| | 21 | | { |
| | 22 | | /// <summary> |
| | 23 | | /// Initializes a new instance of the <see cref="StrainerModuleBuilder{T}"/> |
| | 24 | | /// class. |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="strainerModule"> |
| | 27 | | /// The Strainer moduel to operate on. |
| | 28 | | /// </param> |
| | 29 | | /// <param name="strainerOptions"> |
| | 30 | | /// The Stariner options. |
| | 31 | | /// </param> |
| | 32 | | /// <param name="propertyInfoProvider"> |
| | 33 | | /// The <see cref="PropertyInfo"/> provider. |
| | 34 | | /// </param> |
| | 35 | | /// <exception cref="ArgumentNullException"> |
| | 36 | | /// <paramref name="strainerModule"/> is <see langword="null"/>. |
| | 37 | | /// </exception> |
| | 38 | | /// <exception cref="ArgumentNullException"> |
| | 39 | | /// <paramref name="strainerOptions"/> is <see langword="null"/>. |
| | 40 | | /// </exception> |
| | 41 | | /// <exception cref="ArgumentNullException"> |
| | 42 | | /// <paramref name="propertyInfoProvider"/> is <see langword="null"/>. |
| | 43 | | /// </exception> |
| 11 | 44 | | public StrainerModuleBuilder( |
| 11 | 45 | | IPropertyInfoProvider propertyInfoProvider, |
| 11 | 46 | | IStrainerModule strainerModule, |
| 11 | 47 | | StrainerOptions strainerOptions) |
| | 48 | | { |
| 11 | 49 | | PropertyInfoProvider = Guard.Against.Null(propertyInfoProvider); |
| 11 | 50 | | Module = Guard.Against.Null(strainerModule); |
| 11 | 51 | | Options = Guard.Against.Null(strainerOptions); |
| 11 | 52 | | } |
| | 53 | |
|
| | 54 | | /// <inheritdoc/> |
| 5 | 55 | | public StrainerOptions Options { get; } |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Gets the Strainer module on which this builder operates on. |
| | 59 | | /// </summary> |
| 14 | 60 | | protected IStrainerModule Module { get; } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Gets the property info provider. |
| | 64 | | /// </summary> |
| 3 | 65 | | protected IPropertyInfoProvider PropertyInfoProvider { get; } |
| | 66 | |
|
| | 67 | | /// <inheritdoc/> |
| | 68 | | /// <exception cref="ArgumentException"> |
| | 69 | | /// <paramref name="buildingDelegate"/> is <see langword="null"/>. |
| | 70 | | /// </exception> |
| | 71 | | public IStrainerModuleBuilder<T> AddCustomFilterMethod(Func<ICustomFilterMethodBuilder<T>, ICustomFilterMethod<T>> b |
| | 72 | | { |
| 1 | 73 | | Guard.Against.Null(buildingDelegate); |
| | 74 | |
|
| 1 | 75 | | var builder = new CustomFilterMethodBuilder<T>(); |
| 1 | 76 | | var customMethod = buildingDelegate.Invoke(builder); |
| | 77 | |
|
| 1 | 78 | | if (!Module.CustomFilterMethods.ContainsKey(typeof(T))) |
| | 79 | | { |
| 1 | 80 | | Module.CustomFilterMethods[typeof(T)] = new Dictionary<string, ICustomFilterMethod>(); |
| | 81 | | } |
| | 82 | |
|
| 1 | 83 | | Module.CustomFilterMethods[typeof(T)][customMethod.Name] = customMethod; |
| | 84 | |
|
| 1 | 85 | | return this; |
| | 86 | | } |
| | 87 | |
|
| | 88 | | /// <inheritdoc/> |
| | 89 | | /// <exception cref="ArgumentException"> |
| | 90 | | /// <paramref name="buildingDelegate"/> is <see langword="null"/>. |
| | 91 | | /// </exception> |
| | 92 | | public IStrainerModuleBuilder<T> AddCustomSortMethod( |
| | 93 | | Func<ICustomSortMethodBuilder<T>, ICustomSortMethod<T>> buildingDelegate) |
| | 94 | | { |
| 1 | 95 | | Guard.Against.Null(buildingDelegate); |
| | 96 | |
|
| 1 | 97 | | var builder = new CustomSortMethodBuilder<T>(); |
| 1 | 98 | | var customMethod = buildingDelegate.Invoke(builder); |
| | 99 | |
|
| 1 | 100 | | if (!Module.CustomSortMethods.ContainsKey(typeof(T))) |
| | 101 | | { |
| 1 | 102 | | Module.CustomSortMethods[typeof(T)] = new Dictionary<string, ICustomSortMethod>(); |
| | 103 | | } |
| | 104 | |
|
| 1 | 105 | | Module.CustomSortMethods[typeof(T)][customMethod.Name] = customMethod; |
| | 106 | |
|
| 1 | 107 | | return this; |
| | 108 | | } |
| | 109 | |
|
| | 110 | | /// <inheritdoc/> |
| | 111 | | /// <exception cref="ArgumentException"> |
| | 112 | | /// <paramref name="buildingDelegate"/> is <see langword="null"/>. |
| | 113 | | /// </exception> |
| | 114 | | /// <exception cref="InvalidOperationException"> |
| | 115 | | /// <paramref name="buildingDelegate"/> is used by already defined filter operator. |
| | 116 | | /// </exception> |
| | 117 | | public IStrainerModuleBuilder<T> AddFilterOperator(Func<IFilterOperatorBuilder, IFilterOperator> buildingDelegate) |
| | 118 | | { |
| 2 | 119 | | Guard.Against.Null(buildingDelegate); |
| | 120 | |
|
| 2 | 121 | | var builder = new FilterOperatorBuilder(); |
| 2 | 122 | | var filterOperator = buildingDelegate.Invoke(builder); |
| | 123 | |
|
| 2 | 124 | | if (Module.FilterOperators.Keys.Contains(filterOperator.Symbol)) |
| | 125 | | { |
| 1 | 126 | | throw new InvalidOperationException( |
| 1 | 127 | | $"There is an already existing operator with a symbol {filterOperator.Symbol}. " + |
| 1 | 128 | | $"Please, choose a different symbol."); |
| | 129 | | } |
| | 130 | |
|
| 1 | 131 | | Module.FilterOperators.Add(filterOperator.Symbol, filterOperator); |
| | 132 | |
|
| 1 | 133 | | return this; |
| | 134 | | } |
| | 135 | |
|
| | 136 | | /// <inheritdoc/> |
| | 137 | | /// <exception cref="ArgumentException"> |
| | 138 | | /// <paramref name="defaultSortingPropertyExpression"/> is <see langword="null"/>. |
| | 139 | | /// </exception> |
| | 140 | | /// <exception cref="NotSupportedException"> |
| | 141 | | /// Fluent API is not supported. |
| | 142 | | /// </exception> |
| | 143 | | public IObjectMetadataBuilder<T> AddObject( |
| | 144 | | Expression<Func<T, object>> defaultSortingPropertyExpression) |
| | 145 | | { |
| 3 | 146 | | Guard.Against.Null(defaultSortingPropertyExpression); |
| | 147 | |
|
| 3 | 148 | | if (!Options.MetadataSourceType.HasFlag(MetadataSourceType.FluentApi)) |
| | 149 | | { |
| 1 | 150 | | throw new NotSupportedException( |
| 1 | 151 | | $"Current {nameof(MetadataSourceType)} setting does not " + |
| 1 | 152 | | $"support {nameof(MetadataSourceType.FluentApi)}. " + |
| 1 | 153 | | $"Include {nameof(MetadataSourceType.FluentApi)} option to " + |
| 1 | 154 | | $"be able to use it."); |
| | 155 | | } |
| | 156 | |
|
| 2 | 157 | | return new ObjectMetadataBuilder<T>( |
| 2 | 158 | | PropertyInfoProvider, |
| 2 | 159 | | Module.ObjectMetadata, |
| 2 | 160 | | defaultSortingPropertyExpression); |
| | 161 | | } |
| | 162 | |
|
| | 163 | | /// <inheritdoc/> |
| | 164 | | /// <exception cref="ArgumentException"> |
| | 165 | | /// <paramref name="propertyExpression"/> is <see langword="null"/>. |
| | 166 | | /// </exception> |
| | 167 | | /// <exception cref="NotSupportedException"> |
| | 168 | | /// Fluent API is not supported. |
| | 169 | | /// </exception> |
| | 170 | | public IPropertyMetadataBuilder<T> AddProperty( |
| | 171 | | Expression<Func<T, object>> propertyExpression) |
| | 172 | | { |
| 2 | 173 | | Guard.Against.Null(propertyExpression); |
| | 174 | |
|
| 2 | 175 | | if (!Options.MetadataSourceType.HasFlag(MetadataSourceType.FluentApi)) |
| | 176 | | { |
| 1 | 177 | | throw new NotSupportedException( |
| 1 | 178 | | $"Current {nameof(MetadataSourceType)} setting does not " + |
| 1 | 179 | | $"support {nameof(MetadataSourceType.FluentApi)}. " + |
| 1 | 180 | | $"Include {nameof(MetadataSourceType.FluentApi)} option to " + |
| 1 | 181 | | $"be able to use it."); |
| | 182 | | } |
| | 183 | |
|
| 1 | 184 | | var (propertyInfo, fullName) = PropertyInfoProvider.GetPropertyInfoAndFullName(propertyExpression); |
| | 185 | |
|
| 1 | 186 | | return new PropertyMetadataBuilder<T>( |
| 1 | 187 | | Module.PropertyMetadata, |
| 1 | 188 | | Module.DefaultMetadata, |
| 1 | 189 | | propertyInfo, |
| 1 | 190 | | fullName); |
| | 191 | | } |
| | 192 | |
|
| | 193 | | /// <inheritdoc/> |
| | 194 | | /// <exception cref="ArgumentNullException"> |
| | 195 | | /// <paramref name="symbol"/> is <see langword="null"/>. |
| | 196 | | /// </exception> |
| | 197 | | /// <exception cref="ArgumentException"> |
| | 198 | | /// <paramref name="symbol"/> is empty or whitespace. |
| | 199 | | /// </exception> |
| | 200 | | public IStrainerModuleBuilder<T> RemoveBuiltInFilterOperator(string symbol) |
| | 201 | | { |
| 1 | 202 | | Guard.Against.NullOrWhiteSpace(symbol); |
| | 203 | |
|
| 1 | 204 | | Module.ExcludedBuiltInFilterOperators.Add(symbol); |
| | 205 | |
|
| 1 | 206 | | return this; |
| | 207 | | } |
| | 208 | | } |