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