< Summary

Information
Class: Fluorite.Strainer.Services.Metadata.PropertyMetadataBuilder<T>
Assembly: Fluorite.Strainer
File(s): /builds/fluorite/strainer/src/Strainer/Services/Metadata/PropertyMetadataBuilder.cs
Line coverage
100%
Covered lines: 56
Uncovered lines: 0
Coverable lines: 56
Total lines: 130
Line coverage: 100%
Branch coverage
100%
Covered branches: 16
Total branches: 16
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_DisplayName()100%11100%
get_FullName()100%11100%
get_IsDefaultSorting()100%11100%
get_DefaultSortingWay()100%11100%
get_IsFilterableValue()100%11100%
get_IsSortableValue()100%11100%
get_PropertyInfo()100%11100%
Build()100%11100%
IsFilterable()100%11100%
IsSortable()100%11100%
HasDisplayName(...)100%44100%
Save(...)100%66100%
ValidateDisplayNameCollision(...)100%66100%

File(s)

/builds/fluorite/strainer/src/Strainer/Services/Metadata/PropertyMetadataBuilder.cs

#LineLine coverage
 1using Fluorite.Strainer.Exceptions;
 2using Fluorite.Strainer.Models.Metadata;
 3using Fluorite.Strainer.Models.Sorting;
 4using Fluorite.Strainer.Services.Sorting;
 5using System.Reflection;
 6
 7namespace Fluorite.Strainer.Services.Metadata;
 8
 9public class PropertyMetadataBuilder<TEntity> : IPropertyMetadataBuilder<TEntity>
 10{
 11    private readonly IDictionary<Type, IDictionary<string, IPropertyMetadata>> _propertyMetadata;
 12    private readonly IDictionary<Type, IPropertyMetadata> _defaultMetadata;
 13
 1314    public PropertyMetadataBuilder(
 1315        IDictionary<Type, IDictionary<string, IPropertyMetadata>> propertyMetadata,
 1316        IDictionary<Type, IPropertyMetadata> defaultMetadata,
 1317        PropertyInfo propertyInfo,
 1318        string fullName)
 19    {
 1320        _propertyMetadata = Guard.Against.Null(propertyMetadata);
 1321        _defaultMetadata = Guard.Against.Null(defaultMetadata);
 1322        PropertyInfo = Guard.Against.Null(propertyInfo);
 1323        FullName = Guard.Against.NullOrWhiteSpace(fullName);
 24
 1325        Save(Build());
 1326    }
 27
 4328    protected string? DisplayName { get; set; }
 29
 4230    protected string FullName { get; }
 31
 3332    protected bool IsDefaultSorting { get; set; }
 33
 3334    protected SortingWay? DefaultSortingWay { get; set; }
 35
 3236    protected bool IsFilterableValue { get; set; }
 37
 3238    protected bool IsSortableValue { get; set; }
 39
 3140    protected PropertyInfo PropertyInfo { get; }
 41
 42    public virtual IPropertyMetadata Build()
 43    {
 2744        return new PropertyMetadata(FullName, PropertyInfo)
 2745        {
 2746            DisplayName = DisplayName,
 2747            IsDefaultSorting = IsDefaultSorting,
 2748            DefaultSortingWay = DefaultSortingWay,
 2749            IsFilterable = IsFilterableValue,
 2750            IsSortable = IsSortableValue,
 2751        };
 52    }
 53
 54    public virtual IPropertyMetadataBuilder<TEntity> IsFilterable()
 55    {
 156        IsFilterableValue = true;
 157        Save(Build());
 58
 159        return this;
 60    }
 61
 62    public virtual ISortPropertyMetadataBuilder<TEntity> IsSortable()
 63    {
 164        IsSortableValue = true;
 165        Save(Build());
 66
 167        return new SortPropertyMetadataBuilder<TEntity>(_propertyMetadata, _defaultMetadata, PropertyInfo, FullName, Bui
 68    }
 69
 70    public virtual IPropertyMetadataBuilder<TEntity> HasDisplayName(string displayName)
 71    {
 672        Guard.Against.NullOrWhiteSpace(displayName);
 73
 674        var metadata = _propertyMetadata[typeof(TEntity)];
 75
 676        ValidateDisplayNameCollision(metadata, displayName, FullName);
 77
 78        // Display name is now used,
 79        // try to remove the same entry under old name, to avoid duplication.
 580        if (metadata.ContainsKey(FullName))
 81        {
 382            metadata.Remove(FullName);
 83        }
 84
 85        // Check if display name has been already set,
 86        // remove the same entry under old display name to avoid duplication.
 587        if (DisplayName is not null)
 88        {
 289            metadata.Remove(DisplayName);
 90        }
 91
 592        DisplayName = displayName;
 93
 594        Save(Build());
 95
 596        return this;
 97    }
 98
 99    protected void Save(IPropertyMetadata propertyMetadata)
 100    {
 26101        Guard.Against.Null(propertyMetadata);
 102
 26103        if (!_propertyMetadata.ContainsKey(typeof(TEntity)))
 104        {
 9105            _propertyMetadata[typeof(TEntity)] = new Dictionary<string, IPropertyMetadata>();
 106        }
 107
 26108        if (propertyMetadata.IsDefaultSorting)
 109        {
 2110            _defaultMetadata[typeof(TEntity)] = propertyMetadata;
 111        }
 112
 26113        var metadataKey = propertyMetadata.DisplayName ?? propertyMetadata.Name;
 114
 26115        _propertyMetadata[typeof(TEntity)][metadataKey] = propertyMetadata;
 26116    }
 117
 118    private void ValidateDisplayNameCollision(IDictionary<string, IPropertyMetadata> metadata, string displayName, strin
 119    {
 6120        if (metadata.TryGetValue(displayName, out var existingMetadata))
 121        {
 3122            if (existingMetadata.PropertyInfo != PropertyInfo)
 123            {
 1124                throw new StrainerException(
 1125                    $"Cannot overwrite different property {existingMetadata.DisplayName ?? existingMetadata.Name} " +
 1126                    $"on type {typeof(TEntity).Name} with metadata using display name {displayName} for property {fullNa
 127            }
 128        }
 5129    }
 130}