| | 1 | | using Plainquire.Filter.Abstractions; |
| | 2 | | using Plainquire.Filter.ValueFilterExpressions; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Diagnostics.CodeAnalysis; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Linq.Expressions; |
| | 8 | | using System.Reflection; |
| | 9 | |
|
| | 10 | | namespace Plainquire.Filter.PropertyFilterExpressions; |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Converter to create lambda filter expressions for a given property and a <see cref="ValueFilter"/>. |
| | 14 | | /// </summary> |
| | 15 | | [SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Provided as library, can be used from outside")] |
| | 16 | | public static class PropertyFilterExpression |
| | 17 | | { |
| 2 | 18 | | private static readonly IValueFilterExpression _defaultValueFilterExpressionCreator = new DefaultFilterExpression(); |
| 2 | 19 | | private static readonly MethodInfo _createFilterMethod = typeof(PropertyFilterExpression).GetMethods(BindingFlags.St |
| | 20 | |
|
| 2 | 21 | | private static readonly IValueFilterExpression[] _valueFilterExpressionCreators = |
| 2 | 22 | | [ |
| 2 | 23 | | new StringFilterExpression(), |
| 2 | 24 | | new GuidFilterExpression(), |
| 2 | 25 | | new DateTimeFilterExpression(), |
| 2 | 26 | | new BooleanFilterExpression(), |
| 2 | 27 | | new NumericFilterExpression(), |
| 2 | 28 | | new EnumFilterExpression() |
| 2 | 29 | | ]; |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Determines whether a property of type <paramref name="propertyType"/> can be filtered. |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="propertyType">The type to filter.</param> |
| | 35 | | public static bool CanCreateFilterFor(Type propertyType) |
| 16846 | 36 | | => _valueFilterExpressionCreators.Any(x => x.CanCreateExpressionFor(propertyType)); |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Creates a lambda expression for the given property and <see cref="ValueFilter"/>. |
| | 40 | | /// </summary> |
| | 41 | | /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| | 42 | | /// <typeparam name="TProperty">The type of the property.</typeparam> |
| | 43 | | /// <param name="propertySelector">The property selector.</param> |
| | 44 | | /// <param name="valueFilters">The filters to use.</param> |
| | 45 | | /// <param name="configuration">The filter configuration to use.</param> |
| | 46 | | /// <param name="interceptor">An interceptor to manipulate the generated filters.</param> |
| | 47 | | public static Expression<Func<TEntity, bool>>? CreateFilter<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> |
| | 48 | | { |
| 18194 | 49 | | var valueFilterExpressionCreator = _valueFilterExpressionCreators.FirstOrDefault(x => x.CanCreateExpressionFor<T |
| 18194 | 50 | | var propertyExpression = valueFilterExpressionCreator.CreateExpression(propertySelector, valueFilters, configura |
| 16314 | 51 | | if (propertyExpression == null) |
| 205 | 52 | | return null; |
| | 53 | |
|
| 16109 | 54 | | var result = propertySelector.CreateLambda<TEntity, TProperty, bool>(propertyExpression); |
| 16109 | 55 | | return result; |
| | 56 | | } |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Creates a lambda expression for the given property and <see cref="ValueFilter"/>. |
| | 60 | | /// </summary> |
| | 61 | | /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| | 62 | | /// <param name="propertyType">The type of the property.</param> |
| | 63 | | /// <param name="propertySelector">The property selector.</param> |
| | 64 | | /// <param name="valueFilters">The filters to use.</param> |
| | 65 | | /// <param name="configuration">The filter configuration.</param> |
| | 66 | | /// <param name="interceptor">An interceptor to manipulate the generated filters.</param> |
| | 67 | | public static Expression<Func<TEntity, bool>>? CreateFilter<TEntity>(Type propertyType, LambdaExpression propertySel |
| | 68 | | { |
| | 69 | | try |
| | 70 | | { |
| 18194 | 71 | | var genericMethod = _createFilterMethod.MakeGenericMethod(typeof(TEntity), propertyType); |
| 18194 | 72 | | var propertyExpression = (Expression<Func<TEntity, bool>>?)genericMethod.Invoke(null, [propertySelector, val |
| 16314 | 73 | | return propertyExpression; |
| | 74 | | } |
| 1880 | 75 | | catch (TargetInvocationException ex) when (ex.InnerException != null) |
| | 76 | | { |
| 1880 | 77 | | throw ex.InnerException; |
| | 78 | | } |
| 16314 | 79 | | } |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Creates a lambda expression for the given property and <see cref="ValueFilter"/>. |
| | 83 | | /// </summary> |
| | 84 | | /// <param name="propertyInfo">The property to filter.</param> |
| | 85 | | /// <param name="filters">The filters to use.</param> |
| | 86 | | /// <param name="configuration">The filter configuration.</param> |
| | 87 | | /// <param name="interceptor">An interceptor to manipulate the generated filters.</param> |
| | 88 | | public static Expression<Func<TEntity, bool>>? CreateFilter<TEntity>(PropertyInfo propertyInfo, IEnumerable<ValueFil |
| | 89 | | { |
| 18188 | 90 | | var propertySelector = typeof(TEntity).CreatePropertySelector(propertyInfo.Name); |
| 18188 | 91 | | return CreateFilter<TEntity>(propertyInfo.PropertyType, propertySelector, filters, configuration, interceptor); |
| | 92 | | } |
| | 93 | |
|
| | 94 | | /// <summary> |
| | 95 | | /// Creates no-operation filter. |
| | 96 | | /// </summary> |
| | 97 | | public static Expression<Func<TEntity, bool>> EmptyFilter<TEntity>() |
| 0 | 98 | | => x => true; |
| | 99 | | } |