| | | 1 | | using Microsoft.AspNetCore.Mvc.ModelBinding; |
| | | 2 | | using Plainquire.Filter.Abstractions; |
| | | 3 | | using System; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Reflection; |
| | | 6 | | |
| | | 7 | | namespace Plainquire.Filter.Mvc.ModelBinders; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Model binding provider for <see cref="EntityFilterSetModelBinder"/> |
| | | 11 | | /// </summary> |
| | | 12 | | public class EntityFilterSetModelBinderProvider : IModelBinderProvider |
| | | 13 | | { |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public IModelBinder? GetBinder(ModelBinderProviderContext context) |
| | | 16 | | { |
| | 6 | 17 | | if (context == null) |
| | 0 | 18 | | throw new ArgumentNullException(nameof(context)); |
| | | 19 | | |
| | 6 | 20 | | var entityFilterSetAttribute = context.Metadata.ModelType.GetCustomAttribute<EntityFilterSetAttribute>(); |
| | 6 | 21 | | if (entityFilterSetAttribute == null) |
| | 4 | 22 | | return null; |
| | | 23 | | |
| | 2 | 24 | | var entityFilterBinders = context.Metadata.ModelType |
| | 2 | 25 | | .GetProperties() |
| | 2 | 26 | | .Select(property => GetModelBinder(property, context)) |
| | 2 | 27 | | .ToDictionary(x => x.Type, x => (x.Metadata, x.Binder)); |
| | | 28 | | |
| | 2 | 29 | | return new EntityFilterSetModelBinder(entityFilterBinders); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | private static (Type Type, ModelMetadata Metadata, IModelBinder Binder) GetModelBinder(PropertyInfo property, ModelB |
| | | 33 | | { |
| | 2 | 34 | | var modelMetadata = context.MetadataProvider.GetMetadataForType(property.PropertyType); |
| | 2 | 35 | | var modelBinder = context.CreateBinder(modelMetadata); |
| | 2 | 36 | | return (property.PropertyType, modelMetadata, modelBinder); |
| | | 37 | | } |
| | | 38 | | } |