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
This commit is contained in:
Hasan Manzak 2021-05-15 20:57:18 +03:00 committed by GitHub
parent 285468522c
commit a5b9e5757e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 22 deletions

View File

@ -1,6 +1,5 @@
{ {
"Logging": { "Logging": {
"IncludeScopes": false,
"LogLevel": { "LogLevel": {
"Default": "Debug", "Default": "Debug",
"System": "Information", "System": "Information",

View File

@ -7,7 +7,6 @@
"DefaultPageSize": 10 "DefaultPageSize": 10
}, },
"Logging": { "Logging": {
"IncludeScopes": false,
"Debug": { "Debug": {
"LogLevel": { "LogLevel": {
"Default": "Warning" "Default": "Warning"

View File

@ -68,17 +68,16 @@ namespace Sieve.Services
where TSortTerm : ISortTerm, new() where TSortTerm : ISortTerm, new()
{ {
private const string NullFilterValue = "null"; private const string NullFilterValue = "null";
private readonly IOptions<SieveOptions> _options;
private readonly ISieveCustomSortMethods _customSortMethods; private readonly ISieveCustomSortMethods _customSortMethods;
private readonly ISieveCustomFilterMethods _customFilterMethods; private readonly ISieveCustomFilterMethods _customFilterMethods;
private readonly SievePropertyMapper _mapper = new SievePropertyMapper(); private SievePropertyMapper _mapper;
private SievePropertyMapper Mapper => _mapper ??= MapProperties(new SievePropertyMapper());
public SieveProcessor(IOptions<SieveOptions> options, public SieveProcessor(IOptions<SieveOptions> options,
ISieveCustomSortMethods customSortMethods, ISieveCustomSortMethods customSortMethods,
ISieveCustomFilterMethods customFilterMethods) ISieveCustomFilterMethods customFilterMethods)
{ {
_mapper = MapProperties(_mapper); Options = options;
_options = options;
_customSortMethods = customSortMethods; _customSortMethods = customSortMethods;
_customFilterMethods = customFilterMethods; _customFilterMethods = customFilterMethods;
} }
@ -86,25 +85,24 @@ namespace Sieve.Services
public SieveProcessor(IOptions<SieveOptions> options, public SieveProcessor(IOptions<SieveOptions> options,
ISieveCustomSortMethods customSortMethods) ISieveCustomSortMethods customSortMethods)
{ {
_mapper = MapProperties(_mapper); Options = options;
_options = options;
_customSortMethods = customSortMethods; _customSortMethods = customSortMethods;
} }
public SieveProcessor(IOptions<SieveOptions> options, public SieveProcessor(IOptions<SieveOptions> options,
ISieveCustomFilterMethods customFilterMethods) ISieveCustomFilterMethods customFilterMethods)
{ {
_mapper = MapProperties(_mapper); Options = options;
_options = options;
_customFilterMethods = customFilterMethods; _customFilterMethods = customFilterMethods;
} }
public SieveProcessor(IOptions<SieveOptions> options) public SieveProcessor(IOptions<SieveOptions> options)
{ {
_mapper = MapProperties(_mapper); Options = options;
_options = options;
} }
protected IOptions<SieveOptions> Options { get; }
/// <summary> /// <summary>
/// Apply filtering, sorting, and pagination parameters found in `model` to `source` /// Apply filtering, sorting, and pagination parameters found in `model` to `source`
/// </summary> /// </summary>
@ -148,7 +146,7 @@ namespace Sieve.Services
} }
catch (Exception ex) catch (Exception ex)
{ {
if (!_options.Value.ThrowExceptions) if (!Options.Value.ThrowExceptions)
{ {
return result; return result;
} }
@ -376,8 +374,8 @@ namespace Sieve.Services
private IQueryable<TEntity> ApplyPagination<TEntity>(TSieveModel model, IQueryable<TEntity> result) private IQueryable<TEntity> ApplyPagination<TEntity>(TSieveModel model, IQueryable<TEntity> result)
{ {
var page = model?.Page ?? 1; var page = model?.Page ?? 1;
var pageSize = model?.PageSize ?? _options.Value.DefaultPageSize; var pageSize = model?.PageSize ?? Options.Value.DefaultPageSize;
var maxPageSize = _options.Value.MaxPageSize > 0 ? _options.Value.MaxPageSize : pageSize; var maxPageSize = Options.Value.MaxPageSize > 0 ? Options.Value.MaxPageSize : pageSize;
if (pageSize <= 0) if (pageSize <= 0)
{ {
@ -398,15 +396,15 @@ namespace Sieve.Services
private (string, PropertyInfo) GetSieveProperty<TEntity>(bool canSortRequired, bool canFilterRequired, private (string, PropertyInfo) GetSieveProperty<TEntity>(bool canSortRequired, bool canFilterRequired,
string name) string name)
{ {
var property = _mapper.FindProperty<TEntity>(canSortRequired, canFilterRequired, name, var property = Mapper.FindProperty<TEntity>(canSortRequired, canFilterRequired, name,
_options.Value.CaseSensitive); Options.Value.CaseSensitive);
if (property.Item1 != null) if (property.Item1 != null)
{ {
return property; return property;
} }
var prop = FindPropertyBySieveAttribute<TEntity>(canSortRequired, canFilterRequired, name, var prop = FindPropertyBySieveAttribute<TEntity>(canSortRequired, canFilterRequired, name,
_options.Value.CaseSensitive); Options.Value.CaseSensitive);
return (prop?.Name, prop); return (prop?.Name, prop);
} }
@ -426,7 +424,7 @@ namespace Sieve.Services
{ {
var customMethod = parent?.GetType() var customMethod = parent?.GetType()
.GetMethodExt(name, .GetMethodExt(name,
_options.Value.CaseSensitive Options.Value.CaseSensitive
? BindingFlags.Default ? BindingFlags.Default
: BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance, : BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance,
typeof(IQueryable<TEntity>)); typeof(IQueryable<TEntity>));
@ -437,7 +435,7 @@ namespace Sieve.Services
// Find generic methods `public IQueryable<T> Filter<T>(IQueryable<T> source, ...)` // Find generic methods `public IQueryable<T> Filter<T>(IQueryable<T> source, ...)`
var genericCustomMethod = parent?.GetType() var genericCustomMethod = parent?.GetType()
.GetMethodExt(name, .GetMethodExt(name,
_options.Value.CaseSensitive Options.Value.CaseSensitive
? BindingFlags.Default ? BindingFlags.Default
: BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance, : BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance,
typeof(IQueryable<>)); typeof(IQueryable<>));
@ -481,11 +479,11 @@ namespace Sieve.Services
var incompatibleCustomMethods = var incompatibleCustomMethods =
parent? parent?
.GetType() .GetType()
.GetMethods(_options.Value.CaseSensitive .GetMethods(Options.Value.CaseSensitive
? BindingFlags.Default ? BindingFlags.Default
: BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) : BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance)
.Where(method => string.Equals(method.Name, name, .Where(method => string.Equals(method.Name, name,
_options.Value.CaseSensitive Options.Value.CaseSensitive
? StringComparison.InvariantCulture ? StringComparison.InvariantCulture
: StringComparison.InvariantCultureIgnoreCase)) : StringComparison.InvariantCultureIgnoreCase))
.ToList() .ToList()