From a5b9e5757e821d489cccd5186a80adc00a18179b Mon Sep 17 00:00:00 2001 From: Hasan Manzak Date: Sat, 15 May 2021 20:57:18 +0300 Subject: [PATCH] SieveProcessor.Options made protected property (#134) Mapper assignment in constructor is moved to a null-coalescing member pair (a field and a property) "IncludeScopes" switch is removed from appSettings.{env}.json files --- Sieve.Sample/appsettings.Development.json | 1 - Sieve.Sample/appsettings.json | 1 - Sieve/Services/SieveProcessor.cs | 38 +++++++++++------------ 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/Sieve.Sample/appsettings.Development.json b/Sieve.Sample/appsettings.Development.json index fa8ce71..0623a3f 100644 --- a/Sieve.Sample/appsettings.Development.json +++ b/Sieve.Sample/appsettings.Development.json @@ -1,6 +1,5 @@ { "Logging": { - "IncludeScopes": false, "LogLevel": { "Default": "Debug", "System": "Information", diff --git a/Sieve.Sample/appsettings.json b/Sieve.Sample/appsettings.json index c696841..d1314f5 100644 --- a/Sieve.Sample/appsettings.json +++ b/Sieve.Sample/appsettings.json @@ -7,7 +7,6 @@ "DefaultPageSize": 10 }, "Logging": { - "IncludeScopes": false, "Debug": { "LogLevel": { "Default": "Warning" diff --git a/Sieve/Services/SieveProcessor.cs b/Sieve/Services/SieveProcessor.cs index 6496c7a..3be2dc4 100644 --- a/Sieve/Services/SieveProcessor.cs +++ b/Sieve/Services/SieveProcessor.cs @@ -68,17 +68,16 @@ namespace Sieve.Services where TSortTerm : ISortTerm, new() { private const string NullFilterValue = "null"; - private readonly IOptions _options; private readonly ISieveCustomSortMethods _customSortMethods; private readonly ISieveCustomFilterMethods _customFilterMethods; - private readonly SievePropertyMapper _mapper = new SievePropertyMapper(); + private SievePropertyMapper _mapper; + private SievePropertyMapper Mapper => _mapper ??= MapProperties(new SievePropertyMapper()); public SieveProcessor(IOptions options, ISieveCustomSortMethods customSortMethods, ISieveCustomFilterMethods customFilterMethods) { - _mapper = MapProperties(_mapper); - _options = options; + Options = options; _customSortMethods = customSortMethods; _customFilterMethods = customFilterMethods; } @@ -86,25 +85,24 @@ namespace Sieve.Services public SieveProcessor(IOptions options, ISieveCustomSortMethods customSortMethods) { - _mapper = MapProperties(_mapper); - _options = options; + Options = options; _customSortMethods = customSortMethods; } public SieveProcessor(IOptions options, ISieveCustomFilterMethods customFilterMethods) { - _mapper = MapProperties(_mapper); - _options = options; + Options = options; _customFilterMethods = customFilterMethods; } public SieveProcessor(IOptions options) { - _mapper = MapProperties(_mapper); - _options = options; + Options = options; } + protected IOptions Options { get; } + /// /// Apply filtering, sorting, and pagination parameters found in `model` to `source` /// @@ -148,7 +146,7 @@ namespace Sieve.Services } catch (Exception ex) { - if (!_options.Value.ThrowExceptions) + if (!Options.Value.ThrowExceptions) { return result; } @@ -376,8 +374,8 @@ namespace Sieve.Services private IQueryable ApplyPagination(TSieveModel model, IQueryable result) { var page = model?.Page ?? 1; - var pageSize = model?.PageSize ?? _options.Value.DefaultPageSize; - var maxPageSize = _options.Value.MaxPageSize > 0 ? _options.Value.MaxPageSize : pageSize; + var pageSize = model?.PageSize ?? Options.Value.DefaultPageSize; + var maxPageSize = Options.Value.MaxPageSize > 0 ? Options.Value.MaxPageSize : pageSize; if (pageSize <= 0) { @@ -398,15 +396,15 @@ namespace Sieve.Services private (string, PropertyInfo) GetSieveProperty(bool canSortRequired, bool canFilterRequired, string name) { - var property = _mapper.FindProperty(canSortRequired, canFilterRequired, name, - _options.Value.CaseSensitive); + var property = Mapper.FindProperty(canSortRequired, canFilterRequired, name, + Options.Value.CaseSensitive); if (property.Item1 != null) { return property; } var prop = FindPropertyBySieveAttribute(canSortRequired, canFilterRequired, name, - _options.Value.CaseSensitive); + Options.Value.CaseSensitive); return (prop?.Name, prop); } @@ -426,7 +424,7 @@ namespace Sieve.Services { var customMethod = parent?.GetType() .GetMethodExt(name, - _options.Value.CaseSensitive + Options.Value.CaseSensitive ? BindingFlags.Default : BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance, typeof(IQueryable)); @@ -437,7 +435,7 @@ namespace Sieve.Services // Find generic methods `public IQueryable Filter(IQueryable source, ...)` var genericCustomMethod = parent?.GetType() .GetMethodExt(name, - _options.Value.CaseSensitive + Options.Value.CaseSensitive ? BindingFlags.Default : BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance, typeof(IQueryable<>)); @@ -481,11 +479,11 @@ namespace Sieve.Services var incompatibleCustomMethods = parent? .GetType() - .GetMethods(_options.Value.CaseSensitive + .GetMethods(Options.Value.CaseSensitive ? BindingFlags.Default : BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) .Where(method => string.Equals(method.Name, name, - _options.Value.CaseSensitive + Options.Value.CaseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase)) .ToList()