| | 1 | | using Fluorite.Extensions; |
| | 2 | | using Fluorite.Strainer.Models.Metadata; |
| | 3 | |
|
| | 4 | | namespace Fluorite.Strainer.Services.Metadata; |
| | 5 | |
|
| | 6 | | public class MetadataFacade : IMetadataFacade |
| | 7 | | { |
| | 8 | | private readonly IEnumerable<IMetadataProvider> _metadataProviders; |
| | 9 | |
|
| 17 | 10 | | public MetadataFacade(IEnumerable<IMetadataProvider> metadataProviders) |
| | 11 | | { |
| 17 | 12 | | _metadataProviders = Guard.Against.Null(metadataProviders); |
| 17 | 13 | | } |
| | 14 | |
|
| | 15 | | public IReadOnlyDictionary<Type, IReadOnlyDictionary<string, IPropertyMetadata>> GetAllMetadata() |
| | 16 | | { |
| 8 | 17 | | return GetFirstNotNullResultFromProviders(p => p.GetAllPropertyMetadata()) |
| 4 | 18 | | ?? new Dictionary<Type, IReadOnlyDictionary<string, IPropertyMetadata>>().ToReadOnly(); |
| | 19 | | } |
| | 20 | |
|
| | 21 | | public IPropertyMetadata? GetDefaultMetadata<TEntity>() |
| | 22 | | { |
| 8 | 23 | | return GetDefaultMetadata(typeof(TEntity)); |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public IPropertyMetadata? GetDefaultMetadata(Type modelType) |
| | 27 | | { |
| 8 | 28 | | Guard.Against.Null(modelType); |
| | 29 | |
|
| 15 | 30 | | return GetFirstNotNullResultFromProviders(p => p.GetDefaultMetadata(modelType)); |
| | 31 | | } |
| | 32 | |
|
| | 33 | | public IPropertyMetadata? GetMetadata<TEntity>( |
| | 34 | | bool isSortableRequired, |
| | 35 | | bool isFilterableRequired, |
| | 36 | | string name) |
| | 37 | | { |
| 2 | 38 | | return GetMetadata(typeof(TEntity), isSortableRequired, isFilterableRequired, name); |
| | 39 | | } |
| | 40 | |
|
| | 41 | | public IPropertyMetadata? GetMetadata( |
| | 42 | | Type modelType, |
| | 43 | | bool isSortableRequired, |
| | 44 | | bool isFilterableRequired, |
| | 45 | | string name) |
| | 46 | | { |
| 2 | 47 | | Guard.Against.Null(modelType); |
| 2 | 48 | | Guard.Against.NullOrWhiteSpace(name); |
| | 49 | |
|
| 4 | 50 | | return GetFirstNotNullResultFromProviders(p => p.GetPropertyMetadata(modelType, isSortableRequired, isFilterable |
| | 51 | | } |
| | 52 | |
|
| | 53 | | public IEnumerable<IPropertyMetadata> GetMetadatas<TEntity>() |
| | 54 | | { |
| 2 | 55 | | return GetMetadatas(typeof(TEntity)); |
| | 56 | | } |
| | 57 | |
|
| | 58 | | public IEnumerable<IPropertyMetadata> GetMetadatas(Type modelType) |
| | 59 | | { |
| 2 | 60 | | Guard.Against.Null(modelType); |
| | 61 | |
|
| 4 | 62 | | return GetFirstNotNullResultFromProviders(p => p.GetPropertyMetadatas(modelType)) |
| 2 | 63 | | ?? Enumerable.Empty<IPropertyMetadata>(); |
| | 64 | | } |
| | 65 | |
|
| | 66 | | private TResult? GetFirstNotNullResultFromProviders<TResult>(Func<IMetadataProvider, TResult?> resultFunc) |
| | 67 | | where TResult : class |
| | 68 | | { |
| 50 | 69 | | foreach (var provider in _metadataProviders) |
| | 70 | | { |
| 15 | 71 | | var result = resultFunc.Invoke(provider); |
| 15 | 72 | | if (result != null) |
| | 73 | | { |
| 12 | 74 | | return result; |
| | 75 | | } |
| | 76 | | } |
| | 77 | |
|
| 4 | 78 | | return null; |
| 12 | 79 | | } |
| | 80 | | } |