From 3308f809b108194892b6ce6de4ef0327f988e95c Mon Sep 17 00:00:00 2001 From: Biarity Date: Mon, 2 Jul 2018 09:37:40 +1000 Subject: [PATCH] Better SieveModel, FilterTerm, and SortTerm generics --- Sieve/Models/FilterTerm.cs | 32 +++++++++++++++----------- Sieve/Models/IFilterTerm.cs | 3 ++- Sieve/Models/ISieveModel.cs | 10 ++++++-- Sieve/Models/ISortTerm.cs | 3 ++- Sieve/Models/SieveModel.cs | 38 +++++++++++++++++++++++-------- Sieve/Models/SortTerm.cs | 13 +++++++---- Sieve/Services/ISieveProcessor.cs | 13 ++++++++--- Sieve/Services/SieveProcessor.cs | 27 +++++++++++++++++++--- 8 files changed, 102 insertions(+), 37 deletions(-) diff --git a/Sieve/Models/FilterTerm.cs b/Sieve/Models/FilterTerm.cs index 826833e..b69f33f 100644 --- a/Sieve/Models/FilterTerm.cs +++ b/Sieve/Models/FilterTerm.cs @@ -5,6 +5,8 @@ namespace Sieve.Models { public class FilterTerm : IFilterTerm { + public FilterTerm() { } + private static readonly string[] Operators = new string[] { "==*", "@=*", @@ -19,23 +21,27 @@ namespace Sieve.Models "_=" }; - public FilterTerm(string filter) + public string Filter { - var filterSplits = filter.Split(Operators, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim()).ToArray(); - Names = filterSplits[0].Split('|').Select(t => t.Trim()).ToArray(); - Value = filterSplits.Length > 1 ? filterSplits[1] : null; - Operator = Array.Find(Operators, o => filter.Contains(o)) ?? "=="; - OperatorParsed = GetOperatorParsed(Operator); - OperatorIsCaseInsensitive = Operator.Contains("*"); + set + { + var filterSplits = value.Split(Operators, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim()).ToArray(); + Names = filterSplits[0].Split('|').Select(t => t.Trim()).ToArray(); + Value = filterSplits.Length > 1 ? filterSplits[1] : null; + Operator = Array.Find(Operators, o => value.Contains(o)) ?? "=="; + OperatorParsed = GetOperatorParsed(Operator); + OperatorIsCaseInsensitive = Operator.Contains("*"); + } + } - public string[] Names { get; } + public string[] Names { get; private set; } - public FilterOperator OperatorParsed { get; } + public FilterOperator OperatorParsed { get; private set; } - public string Value { get; } + public string Value { get; private set; } - public string Operator { get; } + public string Operator { get; private set; } private FilterOperator GetOperatorParsed(string Operator) { @@ -65,6 +71,6 @@ namespace Sieve.Models } } - public bool OperatorIsCaseInsensitive { get; } + public bool OperatorIsCaseInsensitive { get; private set; } } -} \ No newline at end of file +} diff --git a/Sieve/Models/IFilterTerm.cs b/Sieve/Models/IFilterTerm.cs index a7c2baf..a844f35 100644 --- a/Sieve/Models/IFilterTerm.cs +++ b/Sieve/Models/IFilterTerm.cs @@ -2,10 +2,11 @@ { public interface IFilterTerm { + string Filter { set; } string[] Names { get; } string Operator { get; } bool OperatorIsCaseInsensitive { get; } FilterOperator OperatorParsed { get; } string Value { get; } } -} \ No newline at end of file +} diff --git a/Sieve/Models/ISieveModel.cs b/Sieve/Models/ISieveModel.cs index 77e682b..f60476d 100644 --- a/Sieve/Models/ISieveModel.cs +++ b/Sieve/Models/ISieveModel.cs @@ -1,8 +1,14 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace Sieve.Models { - public interface ISieveModel + public interface ISieveModel : ISieveModel + { + + } + + public interface ISieveModel where TFilterTerm : IFilterTerm where TSortTerm : ISortTerm { diff --git a/Sieve/Models/ISortTerm.cs b/Sieve/Models/ISortTerm.cs index c75d30a..24f352c 100644 --- a/Sieve/Models/ISortTerm.cs +++ b/Sieve/Models/ISortTerm.cs @@ -2,7 +2,8 @@ { public interface ISortTerm { + string Sort { set; } bool Descending { get; } string Name { get; } } -} \ No newline at end of file +} diff --git a/Sieve/Models/SieveModel.cs b/Sieve/Models/SieveModel.cs index 9483d45..1094ecc 100644 --- a/Sieve/Models/SieveModel.cs +++ b/Sieve/Models/SieveModel.cs @@ -1,11 +1,16 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Runtime.Serialization; namespace Sieve.Models { + public class SieveModel : SieveModel { } + [DataContract] - public class SieveModel : ISieveModel + public class SieveModel : ISieveModel + where TFilterTerm : IFilterTerm, new() + where TSortTerm : ISortTerm, new() { [DataMember] public string Filters { get; set; } @@ -18,25 +23,33 @@ namespace Sieve.Models [DataMember, Range(1, int.MaxValue)] public int? PageSize { get; set; } - - public List FiltersParsed + + public List FiltersParsed { get { if (Filters != null) { - var value = new List(); + var value = new List(); foreach (var filter in Filters.Split(',')) { if (filter.StartsWith("(")) { var filterOpAndVal = filter.Substring(filter.LastIndexOf(")") + 1); var subfilters = filter.Replace(filterOpAndVal, "").Replace("(", "").Replace(")", ""); - value.Add(new FilterTerm(subfilters + filterOpAndVal)); + var filterTerm = new TFilterTerm + { + Filter = subfilters + filterOpAndVal + }; + value.Add(filterTerm); } else { - value.Add(new FilterTerm(filter)); + var filterTerm = new TFilterTerm + { + Filter = filter + }; + value.Add(filterTerm); } } return value; @@ -48,16 +61,20 @@ namespace Sieve.Models } } - public List SortsParsed + public List SortsParsed { get { if (Sorts != null) { - var value = new List(); + var value = new List(); foreach (var sort in Sorts.Split(',')) { - value.Add(new SortTerm(sort)); + var sortTerm = new TSortTerm() + { + Sort = sort + }; + value.Add(sortTerm); } return value; } @@ -65,6 +82,7 @@ namespace Sieve.Models { return null; } + } } } diff --git a/Sieve/Models/SortTerm.cs b/Sieve/Models/SortTerm.cs index 3b6b10f..4d819b6 100644 --- a/Sieve/Models/SortTerm.cs +++ b/Sieve/Models/SortTerm.cs @@ -2,15 +2,20 @@ { public class SortTerm : ISortTerm { - private readonly string _sort; + public SortTerm() { } - public SortTerm(string sort) + private string _sort; + + public string Sort { - _sort = sort; + set + { + _sort = value; + } } public string Name => (_sort.StartsWith("-")) ? _sort.Substring(1) : _sort; public bool Descending => _sort.StartsWith("-"); } -} \ No newline at end of file +} diff --git a/Sieve/Services/ISieveProcessor.cs b/Sieve/Services/ISieveProcessor.cs index 3984cf1..6baffca 100644 --- a/Sieve/Services/ISieveProcessor.cs +++ b/Sieve/Services/ISieveProcessor.cs @@ -3,15 +3,22 @@ using Sieve.Models; namespace Sieve.Services { - public interface ISieveProcessor : ISieveProcessor, IFilterTerm, ISortTerm> + public interface ISieveProcessor : ISieveProcessor + { + + } + + public interface ISieveProcessor : ISieveProcessor, TFilterTerm, TSortTerm> + where TFilterTerm : IFilterTerm, new() + where TSortTerm : ISortTerm, new() { } public interface ISieveProcessor where TSieveModel : class, ISieveModel - where TFilterTerm : IFilterTerm - where TSortTerm : ISortTerm + where TFilterTerm : IFilterTerm, new() + where TSortTerm : ISortTerm, new() { IQueryable Apply( diff --git a/Sieve/Services/SieveProcessor.cs b/Sieve/Services/SieveProcessor.cs index 11cbcb5..4a8bcc4 100644 --- a/Sieve/Services/SieveProcessor.cs +++ b/Sieve/Services/SieveProcessor.cs @@ -11,7 +11,28 @@ using Sieve.Models; namespace Sieve.Services { - public class SieveProcessor : SieveProcessor, IFilterTerm, ISortTerm>, ISieveProcessor + public class SieveProcessor : SieveProcessor, ISieveProcessor + { + public SieveProcessor(IOptions options) : base(options) + { + } + + public SieveProcessor(IOptions options, ISieveCustomSortMethods customSortMethods) : base(options, customSortMethods) + { + } + + public SieveProcessor(IOptions options, ISieveCustomFilterMethods customFilterMethods) : base(options, customFilterMethods) + { + } + + public SieveProcessor(IOptions options, ISieveCustomSortMethods customSortMethods, ISieveCustomFilterMethods customFilterMethods) : base(options, customSortMethods, customFilterMethods) + { + } + } + + public class SieveProcessor : SieveProcessor, TFilterTerm, TSortTerm>, ISieveProcessor + where TFilterTerm : IFilterTerm, new() + where TSortTerm : ISortTerm, new() { public SieveProcessor(IOptions options) : base(options) { @@ -32,8 +53,8 @@ namespace Sieve.Services public class SieveProcessor : ISieveProcessor where TSieveModel : class, ISieveModel - where TFilterTerm : IFilterTerm - where TSortTerm : ISortTerm + where TFilterTerm : IFilterTerm, new() + where TSortTerm : ISortTerm, new() { private readonly IOptions _options; private readonly ISieveCustomSortMethods _customSortMethods;