Beter model generics for extensibility

This commit is contained in:
Biarity 2018-02-10 11:21:43 +10:00
parent bfbbbf8b44
commit b52362e2bc
8 changed files with 57 additions and 20 deletions

View File

@ -5,7 +5,7 @@ using System.Text;
namespace Sieve.Models
{
public class FilterTerm
public class FilterTerm : IFilterTerm
{
private string _filter;
private string[] operators = new string[] {
@ -56,7 +56,6 @@ namespace Sieve.Models
}
}
public string Value {
get
{
@ -95,5 +94,12 @@ namespace Sieve.Models
}
}
public bool OperatorIsCaseInsensitive
{
get
{
return Operator.Contains("*");
}
}
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace Sieve.Models
{
public interface IFilterTerm
{
string Name { get; }
string Operator { get; }
bool OperatorIsCaseInsensitive { get; }
FilterOperator OperatorParsed { get; }
string Value { get; }
}
}

View File

@ -5,7 +5,9 @@ using System.Text;
namespace Sieve.Models
{
public interface ISieveModel
public interface ISieveModel<TFilterTerm, TSortTerm>
where TFilterTerm : IFilterTerm
where TSortTerm : ISortTerm
{
string Filters { get; set; }
@ -15,8 +17,8 @@ namespace Sieve.Models
int? PageSize { get; set; }
List<FilterTerm> FiltersParsed { get; }
List<TFilterTerm> FiltersParsed { get; }
List<SortTerm> SortsParsed { get; }
List<TSortTerm> SortsParsed { get; }
}
}

13
Sieve/Models/ISortTerm.cs Normal file
View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace Sieve.Models
{
public interface ISortTerm
{
bool Descending { get; }
string Name { get; }
}
}

View File

@ -5,7 +5,7 @@ using System.Text;
namespace Sieve.Models
{
public class SieveModel : ISieveModel
public class SieveModel: ISieveModel<IFilterTerm, ISortTerm>
{
public string Filters { get; set; }
@ -18,13 +18,13 @@ namespace Sieve.Models
public int? PageSize { get; set; }
public List<FilterTerm> FiltersParsed
public List<IFilterTerm> FiltersParsed
{
get
{
if (Filters != null)
{
var value = new List<FilterTerm>();
var value = new List<IFilterTerm>();
foreach (var filter in Filters.Split(','))
{
value.Add(new FilterTerm(filter));
@ -38,13 +38,13 @@ namespace Sieve.Models
}
}
public List<SortTerm> SortsParsed
public List<ISortTerm> SortsParsed
{
get
{
if (Sorts != null)
{
var value = new List<SortTerm>();
var value = new List<ISortTerm>();
foreach (var sort in Sorts.Split(','))
{
value.Add(new SortTerm(sort));

View File

@ -5,7 +5,7 @@ using System.Text;
namespace Sieve.Models
{
public class SortTerm
public class SortTerm : ISortTerm
{
private string _sort;

View File

@ -6,9 +6,9 @@ namespace Sieve.Services
{
public interface ISieveProcessor
{
IQueryable<TEntity> ApplyAll<TEntity>(ISieveModel model, IQueryable<TEntity> source, object[] dataForCustomMethods = null);
IQueryable<TEntity> ApplySorting<TEntity>(ISieveModel model, IQueryable<TEntity> result, object[] dataForCustomMethods = null);
IQueryable<TEntity> ApplyFiltering<TEntity>(ISieveModel model, IQueryable<TEntity> result, object[] dataForCustomMethods = null);
IQueryable<TEntity> ApplyPagination<TEntity>(ISieveModel model, IQueryable<TEntity> result);
IQueryable<TEntity> ApplyAll<TEntity>(ISieveModel<IFilterTerm, ISortTerm> model, IQueryable<TEntity> source, object[] dataForCustomMethods = null);
IQueryable<TEntity> ApplySorting<TEntity>(ISieveModel<IFilterTerm, ISortTerm> model, IQueryable<TEntity> result, object[] dataForCustomMethods = null);
IQueryable<TEntity> ApplyFiltering<TEntity>(ISieveModel<IFilterTerm, ISortTerm> model, IQueryable<TEntity> result, object[] dataForCustomMethods = null);
IQueryable<TEntity> ApplyPagination<TEntity>(ISieveModel<IFilterTerm, ISortTerm> model, IQueryable<TEntity> result);
}
}

View File

@ -57,7 +57,7 @@ namespace Sieve.Services
/// <param name="source">Data source</param>
/// <param name="dataForCustomMethods">Additional data that will be passed down to custom methods</param>
/// <returns>Returns a transformed version of `source`</returns>
public IQueryable<TEntity> ApplyAll<TEntity>(ISieveModel model, IQueryable<TEntity> source, object[] dataForCustomMethods = null)
public IQueryable<TEntity> ApplyAll<TEntity>(ISieveModel<IFilterTerm, ISortTerm> model, IQueryable<TEntity> source, object[] dataForCustomMethods = null)
{
var result = source;
@ -84,7 +84,7 @@ namespace Sieve.Services
/// <param name="source">Data source</param>
/// <param name="dataForCustomMethods">Additional data that will be passed down to custom methods</param>
/// <returns>Returns a transformed version of `source`</returns>
public IQueryable<TEntity> ApplyFiltering<TEntity>(ISieveModel model, IQueryable<TEntity> result, object[] dataForCustomMethods = null)
public IQueryable<TEntity> ApplyFiltering<TEntity>(ISieveModel<IFilterTerm, ISortTerm> model, IQueryable<TEntity> result, object[] dataForCustomMethods = null)
{
if (model?.FiltersParsed == null)
return result;
@ -106,7 +106,7 @@ namespace Sieve.Services
dynamic propertyValue = Expression.PropertyOrField(parameter, property.Name);
if (filterTerm.Operator.Contains("*"))
if (filterTerm.OperatorIsCaseInsensitive)
{
propertyValue = Expression.Call(propertyValue,
typeof(string).GetMethods()
@ -181,7 +181,7 @@ namespace Sieve.Services
/// <param name="source">Data source</param>
/// <param name="dataForCustomMethods">Additional data that will be passed down to custom methods</param>
/// <returns>Returns a transformed version of `source`</returns>
public IQueryable<TEntity> ApplySorting<TEntity>(ISieveModel model, IQueryable<TEntity> result, object[] dataForCustomMethods = null)
public IQueryable<TEntity> ApplySorting<TEntity>(ISieveModel<IFilterTerm, ISortTerm> model, IQueryable<TEntity> result, object[] dataForCustomMethods = null)
{
if (model?.SortsParsed == null)
return result;
@ -219,7 +219,7 @@ namespace Sieve.Services
/// <param name="source">Data source</param>
/// <param name="dataForCustomMethods">Additional data that will be passed down to custom methods</param>
/// <returns>Returns a transformed version of `source`</returns>
public IQueryable<TEntity> ApplyPagination<TEntity>(ISieveModel model, IQueryable<TEntity> result)
public IQueryable<TEntity> ApplyPagination<TEntity>(ISieveModel<IFilterTerm, ISortTerm> model, IQueryable<TEntity> result)
{
var page = model?.Page ?? 1;
var pageSize = model?.PageSize ?? _options.Value.DefaultPageSize;