Fix for Issue #19

This commit is contained in:
David Bond 2018-05-15 00:34:37 +01:00
parent afbd41e090
commit 204a1b55e2
39 changed files with 379 additions and 504 deletions

40
.editorconfig Normal file
View File

@ -0,0 +1,40 @@
# To learn more about .editorconfig see https://aka.ms/editorconfigdocs
root = true
# All files
[*]
indent_style = space
trim_trailing_whitespace = true
# Code files
[*.{cs,csx,vb,vbx}]
indent_size = 4
end_of_line = crlf
# Xml files
[*.xml]
indent_size = 2
# Dotnet code style
[*.{cs,vb}]
# Organize usings
dotnet_sort_system_directives_first = true
# Avoid this. unless absolutely necessary
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion
# Use language keywords instead of BCL types
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion
# Naming conventions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
# Classes, structs, methods, enums, events, properties, namespaces, delegates must be PascalCase
dotnet_naming_rule.general_naming.severity = suggestion
dotnet_naming_rule.general_naming.symbols = general
dotnet_naming_rule.general_naming.style = pascal_case_style
dotnet_naming_symbols.general.applicable_kinds = class,struct,enum,property,method,event,namespace,delegate
dotnet_naming_symbols.general.applicable_accessibilities = *

View File

@ -7,7 +7,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sieve", "Sieve\Sieve.csproj
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SieveTests", "SieveTests\SieveTests.csproj", "{8043D264-42A0-4275-97A1-46400C02E37E}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SieveTests", "SieveTests\SieveTests.csproj", "{8043D264-42A0-4275-97A1-46400C02E37E}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SieveUnitTests", "SieveUnitTests\SieveUnitTests.csproj", "{21C3082D-F40E-457F-BE2E-AA099E19E199}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SieveUnitTests", "SieveUnitTests\SieveUnitTests.csproj", "{21C3082D-F40E-457F-BE2E-AA099E19E199}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{904F25A9-5CBD-42AE-8422-ADAB98F4B4B7}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -1,7 +1,5 @@
using Sieve.Models; using Sieve.Models;
using System; using System;
using System.Collections.Generic;
using System.Text;
namespace Sieve.Attributes namespace Sieve.Attributes
{ {

View File

@ -1,6 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Text;
namespace Sieve.Exceptions namespace Sieve.Exceptions
{ {
@ -13,5 +11,13 @@ namespace Sieve.Exceptions
public SieveException(string message, Exception innerException) : base(message, innerException) public SieveException(string message, Exception innerException) : base(message, innerException)
{ {
} }
public SieveException()
{
}
protected SieveException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context)
{
}
} }
} }

View File

@ -1,6 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Text;
namespace Sieve.Exceptions namespace Sieve.Exceptions
{ {
@ -10,7 +8,6 @@ namespace Sieve.Exceptions
public Type ExpectedType { get; protected set; } public Type ExpectedType { get; protected set; }
public Type ActualType { get; protected set; } public Type ActualType { get; protected set; }
public SieveIncompatibleMethodException( public SieveIncompatibleMethodException(
string methodName, string methodName,
Type expectedType, Type expectedType,
@ -35,5 +32,21 @@ namespace Sieve.Exceptions
ExpectedType = expectedType; ExpectedType = expectedType;
ActualType = actualType; ActualType = actualType;
} }
public SieveIncompatibleMethodException(string message) : base(message)
{
}
public SieveIncompatibleMethodException(string message, Exception innerException) : base(message, innerException)
{
}
public SieveIncompatibleMethodException()
{
}
protected SieveIncompatibleMethodException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context)
{
}
} }
} }

View File

@ -1,6 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Text;
namespace Sieve.Exceptions namespace Sieve.Exceptions
{ {
@ -8,7 +6,7 @@ namespace Sieve.Exceptions
{ {
public string MethodName { get; protected set; } public string MethodName { get; protected set; }
public SieveMethodNotFoundException(string methodName, string message) : base (message) public SieveMethodNotFoundException(string methodName, string message) : base(message)
{ {
MethodName = methodName; MethodName = methodName;
} }
@ -17,5 +15,21 @@ namespace Sieve.Exceptions
{ {
MethodName = methodName; MethodName = methodName;
} }
public SieveMethodNotFoundException(string message) : base(message)
{
}
public SieveMethodNotFoundException(string message, Exception innerException) : base(message, innerException)
{
}
public SieveMethodNotFoundException()
{
}
protected SieveMethodNotFoundException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context)
{
}
} }
} }

View File

@ -1,8 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Text;
namespace Sieve.Extensions namespace Sieve.Extensions
{ {

View File

@ -1,8 +1,4 @@
using System; namespace Sieve.Models
using System.Collections.Generic;
using System.Text;
namespace Sieve.Models
{ {
public enum FilterOperator public enum FilterOperator
{ {

View File

@ -1,14 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace Sieve.Models namespace Sieve.Models
{ {
public class FilterTerm : IFilterTerm public class FilterTerm : IFilterTerm
{ {
private string _filter; private static readonly string[] Operators = new string[] {
private string[] operators = new string[] {
"==*", "==*",
"@=*", "@=*",
"_=*", "_=*",
@ -24,48 +21,23 @@ namespace Sieve.Models
public FilterTerm(string filter) public FilterTerm(string filter)
{ {
_filter = 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("*");
} }
public string Name public string[] Names { get; }
{
get
{
var tokens = _filter.Split(operators, StringSplitOptions.RemoveEmptyEntries);
return tokens.Length > 0 ? tokens[0].Trim() : "";
} public FilterOperator OperatorParsed { get; }
}
public string Operator public string Value { get; }
{
get
{
foreach (var op in operators)
{
if (_filter.IndexOf(op) != -1)
{
return op;
}
}
// Custom operators not supported public string Operator { get; }
// var tokens = _filter.Split(' ');
// return tokens.Length > 2 ? tokens[1] : "";
return "";
}
}
public string Value { private FilterOperator GetOperatorParsed(string Operator)
get
{
var tokens = _filter.Split(operators, StringSplitOptions.RemoveEmptyEntries);
return tokens.Length > 1 ? tokens[1].Trim() : null;
}
}
public FilterOperator OperatorParsed {
get
{ {
switch (Operator.Trim().ToLower()) switch (Operator.Trim().ToLower())
{ {
@ -92,14 +64,7 @@ namespace Sieve.Models
return FilterOperator.Equals; return FilterOperator.Equals;
} }
} }
}
public bool OperatorIsCaseInsensitive public bool OperatorIsCaseInsensitive { get; }
{
get
{
return Operator.Contains("*");
}
}
} }
} }

View File

@ -1,13 +1,8 @@
using System; namespace Sieve.Models
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace Sieve.Models
{ {
public interface IFilterTerm public interface IFilterTerm
{ {
string Name { get; } string[] Names { get; }
string Operator { get; } string Operator { get; }
bool OperatorIsCaseInsensitive { get; } bool OperatorIsCaseInsensitive { get; }
FilterOperator OperatorParsed { get; } FilterOperator OperatorParsed { get; }

View File

@ -1,7 +1,4 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace Sieve.Models namespace Sieve.Models
{ {

View File

@ -1,9 +1,4 @@
using System; namespace Sieve.Models
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace Sieve.Models
{ {
public interface ISievePropertyMetadata public interface ISievePropertyMetadata
{ {

View File

@ -1,9 +1,4 @@
using System; namespace Sieve.Models
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace Sieve.Models
{ {
public interface ISortTerm public interface ISortTerm
{ {

View File

@ -1,23 +1,20 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Text;
namespace Sieve.Models namespace Sieve.Models
{ {
public class SieveModel: ISieveModel<IFilterTerm, ISortTerm> public class SieveModel : ISieveModel<IFilterTerm, ISortTerm>
{ {
public string Filters { get; set; } public string Filters { get; set; }
public string Sorts { get; set; } public string Sorts { get; set; }
[Range(1, Double.MaxValue)] [Range(1, int.MaxValue)]
public int? Page { get; set; } public int? Page { get; set; }
[Range(1, Double.MaxValue)] [Range(1, int.MaxValue)]
public int? PageSize { get; set; } public int? PageSize { get; set; }
public List<IFilterTerm> FiltersParsed public List<IFilterTerm> FiltersParsed
{ {
get get
@ -30,11 +27,8 @@ namespace Sieve.Models
if (filter.StartsWith("(")) if (filter.StartsWith("("))
{ {
var filterOpAndVal = filter.Substring(filter.LastIndexOf(")") + 1); var filterOpAndVal = filter.Substring(filter.LastIndexOf(")") + 1);
var subfilters = filter.Replace(filterOpAndVal, "").Replace("(", "").Replace(")",""); var subfilters = filter.Replace(filterOpAndVal, "").Replace("(", "").Replace(")", "");
foreach (var subfilter in subfilters.Split('|')) value.Add(new FilterTerm(subfilters + filterOpAndVal));
{
value.Add(new FilterTerm(subfilter + filterOpAndVal));
}
} }
else else
{ {

View File

@ -1,9 +1,4 @@
using Microsoft.Extensions.Options; namespace Sieve.Models
using System;
using System.Collections.Generic;
using System.Text;
namespace Sieve.Models
{ {
public class SieveOptions public class SieveOptions
{ {

View File

@ -1,9 +1,4 @@
using System; namespace Sieve.Models
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace Sieve.Models
{ {
public class SievePropertyMetadata : ISievePropertyMetadata public class SievePropertyMetadata : ISievePropertyMetadata
{ {

View File

@ -1,47 +1,16 @@
using System; namespace Sieve.Models
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace Sieve.Models
{ {
public class SortTerm : ISortTerm public class SortTerm : ISortTerm
{ {
private string _sort; private readonly string _sort;
public SortTerm(string sort) public SortTerm(string sort)
{ {
_sort = sort; _sort = sort;
} }
public string Name public string Name => (_sort.StartsWith("-")) ? _sort.Substring(1) : _sort;
{
get
{
if (_sort.StartsWith("-"))
{
return _sort.Substring(1);
}
else
{
return _sort;
}
}
}
public bool Descending public bool Descending => _sort.StartsWith("-");
{
get
{
if (_sort.StartsWith("-"))
{
return true;
}
else
{
return false;
}
}
}
} }
} }

View File

@ -1,8 +1,4 @@
using System; namespace Sieve.Services
using System.Collections.Generic;
using System.Text;
namespace Sieve.Services
{ {
public interface ISieveCustomFilterMethods public interface ISieveCustomFilterMethods
{ {

View File

@ -1,8 +1,4 @@
using System; namespace Sieve.Services
using System.Collections.Generic;
using System.Text;
namespace Sieve.Services
{ {
public interface ISieveCustomSortMethods public interface ISieveCustomSortMethods
{ {

View File

@ -1,5 +1,4 @@
using System.Collections.Generic; using System.Linq;
using System.Linq;
using Sieve.Models; using Sieve.Models;
namespace Sieve.Services namespace Sieve.Services

View File

@ -1,27 +1,22 @@
using Microsoft.Extensions.Options; using System;
using Sieve.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using Sieve.Attributes;
using Sieve.Extensions;
using System.ComponentModel; using System.ComponentModel;
using System.Collections; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection;
using Microsoft.Extensions.Options;
using Sieve.Attributes;
using Sieve.Exceptions; using Sieve.Exceptions;
using Sieve.Extensions;
using Sieve.Models;
namespace Sieve.Services namespace Sieve.Services
{ {
public class SieveProcessor : ISieveProcessor public class SieveProcessor : ISieveProcessor
{ {
private IOptions<SieveOptions> _options; private readonly IOptions<SieveOptions> _options;
private ISieveCustomSortMethods _customSortMethods; private readonly ISieveCustomSortMethods _customSortMethods;
private ISieveCustomFilterMethods _customFilterMethods; private readonly ISieveCustomFilterMethods _customFilterMethods;
private SievePropertyMapper mapper = new SievePropertyMapper(); private readonly SievePropertyMapper mapper = new SievePropertyMapper();
public SieveProcessor(IOptions<SieveOptions> options, public SieveProcessor(IOptions<SieveOptions> options,
ISieveCustomSortMethods customSortMethods, ISieveCustomSortMethods customSortMethods,
@ -77,21 +72,29 @@ namespace Sieve.Services
var result = source; var result = source;
if (model == null) if (model == null)
{
return result; return result;
}
try try
{ {
// Filter // Filter
if (applyFiltering) if (applyFiltering)
{
result = ApplyFiltering(model, result, dataForCustomMethods); result = ApplyFiltering(model, result, dataForCustomMethods);
}
// Sort // Sort
if (applySorting) if (applySorting)
{
result = ApplySorting(model, result, dataForCustomMethods); result = ApplySorting(model, result, dataForCustomMethods);
}
// Paginate // Paginate
if (applyPagination) if (applyPagination)
{
result = ApplyPagination(model, result); result = ApplyPagination(model, result);
}
return result; return result;
} }
@ -100,7 +103,10 @@ namespace Sieve.Services
if (_options.Value.ThrowExceptions) if (_options.Value.ThrowExceptions)
{ {
if (ex is SieveException) if (ex is SieveException)
{
throw; throw;
}
throw new SieveException(ex.Message, ex); throw new SieveException(ex.Message, ex);
} }
else else
@ -116,23 +122,27 @@ namespace Sieve.Services
object[] dataForCustomMethods = null) object[] dataForCustomMethods = null)
{ {
if (model?.FiltersParsed == null) if (model?.FiltersParsed == null)
{
return result; return result;
}
Expression outerExpression = null;
var parameterExpression = Expression.Parameter(typeof(TEntity), "e");
foreach (var filterTerm in model.FiltersParsed) foreach (var filterTerm in model.FiltersParsed)
{ {
var property = GetSieveProperty<TEntity>(false, true, filterTerm.Name); Expression innerExpression = null;
foreach (var filterTermName in filterTerm.Names)
{
var property = GetSieveProperty<TEntity>(false, true, filterTermName);
if (property != null) if (property != null)
{ {
var converter = TypeDescriptor.GetConverter(property.PropertyType); var converter = TypeDescriptor.GetConverter(property.PropertyType);
var parameter = Expression.Parameter(typeof(TEntity), "e");
dynamic filterValue = Expression.Constant( dynamic filterValue = Expression.Constant(
converter.CanConvertFrom(typeof(string)) ? converter.CanConvertFrom(typeof(string))
converter.ConvertFrom(filterTerm.Value) : ? converter.ConvertFrom(filterTerm.Value)
Convert.ChangeType(filterTerm.Value, property.PropertyType)); : Convert.ChangeType(filterTerm.Value, property.PropertyType));
dynamic propertyValue = Expression.PropertyOrField(parameter, property.Name); dynamic propertyValue = Expression.PropertyOrField(parameterExpression, property.Name);
if (filterTerm.OperatorIsCaseInsensitive) if (filterTerm.OperatorIsCaseInsensitive)
{ {
@ -145,60 +155,66 @@ namespace Sieve.Services
.First(m => m.Name == "ToUpper" && m.GetParameters().Length == 0)); .First(m => m.Name == "ToUpper" && m.GetParameters().Length == 0));
} }
Expression comparison; if (innerExpression == null)
switch (filterTerm.OperatorParsed)
{ {
case FilterOperator.Equals: innerExpression = GetExpression(filterTerm, filterValue, propertyValue);
comparison = Expression.Equal(propertyValue, filterValue);
break;
case FilterOperator.NotEquals:
comparison = Expression.NotEqual(propertyValue, filterValue);
break;
case FilterOperator.GreaterThan:
comparison = Expression.GreaterThan(propertyValue, filterValue);
break;
case FilterOperator.LessThan:
comparison = Expression.LessThan(propertyValue, filterValue);
break;
case FilterOperator.GreaterThanOrEqualTo:
comparison = Expression.GreaterThanOrEqual(propertyValue, filterValue);
break;
case FilterOperator.LessThanOrEqualTo:
comparison = Expression.LessThanOrEqual(propertyValue, filterValue);
break;
case FilterOperator.Contains:
comparison = Expression.Call(propertyValue,
typeof(string).GetMethods()
.First(m => m.Name == "Contains" && m.GetParameters().Length == 1),
filterValue);
break;
case FilterOperator.StartsWith:
comparison = Expression.Call(propertyValue,
typeof(string).GetMethods()
.First(m => m.Name == "StartsWith" && m.GetParameters().Length == 1),
filterValue); break;
default:
comparison = Expression.Equal(propertyValue, filterValue);
break;
}
result = result.Where(Expression.Lambda<Func<TEntity, bool>>(
comparison,
parameter));
} }
else else
{ {
result = ApplyCustomMethod(result, filterTerm.Name, _customFilterMethods, innerExpression = Expression.Or(innerExpression, GetExpression(filterTerm, filterValue, propertyValue));
new object[] { }
}
else
{
var parameters = new object[] {
result, result,
filterTerm.Operator, filterTerm.Operator,
filterTerm.Value filterTerm.Value
}, dataForCustomMethods); };
result = ApplyCustomMethod(result, filterTermName, _customFilterMethods, parameters, dataForCustomMethods);
} }
} }
if (outerExpression == null)
{
outerExpression = innerExpression;
continue;
}
outerExpression = Expression.And(outerExpression, innerExpression);
}
return outerExpression == null
? result
: result.Where(Expression.Lambda<Func<TEntity, bool>>(outerExpression, parameterExpression));
}
return result; private static Expression GetExpression(IFilterTerm filterTerm, dynamic filterValue, dynamic propertyValue)
{
switch (filterTerm.OperatorParsed)
{
case FilterOperator.Equals:
return Expression.Equal(propertyValue, filterValue);
case FilterOperator.NotEquals:
return Expression.NotEqual(propertyValue, filterValue);
case FilterOperator.GreaterThan:
return Expression.GreaterThan(propertyValue, filterValue);
case FilterOperator.LessThan:
return Expression.LessThan(propertyValue, filterValue);
case FilterOperator.GreaterThanOrEqualTo:
return Expression.GreaterThanOrEqual(propertyValue, filterValue);
case FilterOperator.LessThanOrEqualTo:
return Expression.LessThanOrEqual(propertyValue, filterValue);
case FilterOperator.Contains:
return Expression.Call(propertyValue,
typeof(string).GetMethods()
.First(m => m.Name == "Contains" && m.GetParameters().Length == 1),
filterValue);
case FilterOperator.StartsWith:
return Expression.Call(propertyValue,
typeof(string).GetMethods()
.First(m => m.Name == "StartsWith" && m.GetParameters().Length == 1),
filterValue);
default:
return Expression.Equal(propertyValue, filterValue);
}
} }
private IQueryable<TEntity> ApplySorting<TEntity>( private IQueryable<TEntity> ApplySorting<TEntity>(
@ -207,7 +223,9 @@ namespace Sieve.Services
object[] dataForCustomMethods = null) object[] dataForCustomMethods = null)
{ {
if (model?.SortsParsed == null) if (model?.SortsParsed == null)
{
return result; return result;
}
var useThenBy = false; var useThenBy = false;
foreach (var sortTerm in model.SortsParsed) foreach (var sortTerm in model.SortsParsed)
@ -245,12 +263,13 @@ namespace Sieve.Services
result = result.Skip((page - 1) * pageSize); result = result.Skip((page - 1) * pageSize);
if (pageSize > 0) if (pageSize > 0)
{
result = result.Take(Math.Min(pageSize, maxPageSize)); result = result.Take(Math.Min(pageSize, maxPageSize));
}
return result; return result;
} }
protected virtual SievePropertyMapper MapProperties(SievePropertyMapper mapper) protected virtual SievePropertyMapper MapProperties(SievePropertyMapper mapper)
{ {
return mapper; return mapper;
@ -269,19 +288,13 @@ namespace Sieve.Services
bool canSortRequired, bool canSortRequired,
bool canFilterRequired, bool canFilterRequired,
string name, string name,
bool isCaseSensitive) bool isCaseSensitive) => Array.Find(typeof(TEntity).GetProperties(), p =>
{ {
return typeof(TEntity).GetProperties().FirstOrDefault(p => return p.GetCustomAttribute(typeof(SieveAttribute)) is SieveAttribute sieveAttribute
{ && (canSortRequired ? sieveAttribute.CanSort : true)
if (p.GetCustomAttribute(typeof(SieveAttribute)) is SieveAttribute sieveAttribute) && (canFilterRequired ? sieveAttribute.CanFilter : true)
if ((canSortRequired ? sieveAttribute.CanSort : true) && && ((sieveAttribute.Name ?? p.Name).Equals(name, isCaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase));
(canFilterRequired ? sieveAttribute.CanFilter : true) &&
((sieveAttribute.Name ?? p.Name).Equals(name,
isCaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase)))
return true;
return false;
}); });
}
private IQueryable<TEntity> ApplyCustomMethod<TEntity>(IQueryable<TEntity> result, string name, object parent, object[] parameters, object[] optionalParameters = null) private IQueryable<TEntity> ApplyCustomMethod<TEntity>(IQueryable<TEntity> result, string name, object parent, object[] parameters, object[] optionalParameters = null)
{ {
@ -308,7 +321,7 @@ namespace Sieve.Services
throw; throw;
} }
} }
catch (ArgumentException) // name matched with custom method for a differnt type catch (ArgumentException) // name matched with custom method for a different type
{ {
var expected = typeof(IQueryable<TEntity>); var expected = typeof(IQueryable<TEntity>);
var actual = customMethod.ReturnType; var actual = customMethod.ReturnType;
@ -318,8 +331,7 @@ namespace Sieve.Services
} }
else else
{ {
throw new SieveMethodNotFoundException(name, throw new SieveMethodNotFoundException(name, $"{name} not found.");
$"{name} not found.");
} }
return result; return result;

View File

@ -4,27 +4,28 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using System.Text;
namespace Sieve.Services namespace Sieve.Services
{ {
public class SievePropertyMapper public class SievePropertyMapper
{ {
private Dictionary<Type, Dictionary<PropertyInfo, ISievePropertyMetadata>> _map private readonly Dictionary<Type, Dictionary<PropertyInfo, ISievePropertyMetadata>> _map
= new Dictionary<Type, Dictionary<PropertyInfo, ISievePropertyMetadata>>(); = new Dictionary<Type, Dictionary<PropertyInfo, ISievePropertyMetadata>>();
public PropertyFluentApi<TEntity> Property<TEntity>(Expression<Func<TEntity, object>> expression) public PropertyFluentApi<TEntity> Property<TEntity>(Expression<Func<TEntity, object>> expression)
{ {
if(!_map.ContainsKey(typeof(TEntity))) if(!_map.ContainsKey(typeof(TEntity)))
{
_map.Add(typeof(TEntity), new Dictionary<PropertyInfo, ISievePropertyMetadata>()); _map.Add(typeof(TEntity), new Dictionary<PropertyInfo, ISievePropertyMetadata>());
}
return new PropertyFluentApi<TEntity>(this, expression); return new PropertyFluentApi<TEntity>(this, expression);
} }
public class PropertyFluentApi<TEntity> public class PropertyFluentApi<TEntity>
{ {
private SievePropertyMapper _sievePropertyMapper; private readonly SievePropertyMapper _sievePropertyMapper;
private PropertyInfo _property; private readonly PropertyInfo _property;
public PropertyFluentApi(SievePropertyMapper sievePropertyMapper, Expression<Func<TEntity, object>> expression) public PropertyFluentApi(SievePropertyMapper sievePropertyMapper, Expression<Func<TEntity, object>> expression)
{ {
@ -92,16 +93,14 @@ namespace Sieve.Services
{ {
return _map[typeof(TEntity)] return _map[typeof(TEntity)]
.FirstOrDefault(kv => .FirstOrDefault(kv =>
kv.Value.Name.Equals(name, isCaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase) && kv.Value.Name.Equals(name, isCaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase)
(canSortRequired ? kv.Value.CanSort : true) && && (canSortRequired ? kv.Value.CanSort : true)
(canFilterRequired ? kv.Value.CanFilter : true)).Key; && (canFilterRequired ? kv.Value.CanFilter : true)).Key;
} }
catch (Exception ex) when (ex is KeyNotFoundException || ex is ArgumentNullException) catch (Exception ex) when (ex is KeyNotFoundException || ex is ArgumentNullException)
{ {
return null; return null;
} }
} }
} }
} }

View File

@ -1,7 +1,4 @@
using System; using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Sieve.Models; using Sieve.Models;
@ -13,8 +10,8 @@ namespace SieveTests.Controllers
[Route("api/[controller]/[action]")] [Route("api/[controller]/[action]")]
public class PostsController : Controller public class PostsController : Controller
{ {
private ISieveProcessor _sieveProcessor; private readonly ISieveProcessor _sieveProcessor;
private ApplicationDbContext _dbContext; private readonly ApplicationDbContext _dbContext;
public PostsController(ISieveProcessor sieveProcessor, public PostsController(ISieveProcessor sieveProcessor,
ApplicationDbContext dbContext) ApplicationDbContext dbContext)

View File

@ -1,9 +1,4 @@
using JetBrains.Annotations; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SieveTests.Entities namespace SieveTests.Entities
{ {

View File

@ -1,7 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Sieve.Attributes; using Sieve.Attributes;
namespace SieveTests.Entities namespace SieveTests.Entities

View File

@ -1,7 +1,6 @@
using Microsoft.EntityFrameworkCore.Metadata; using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;
namespace SieveTests.Migrations namespace SieveTests.Migrations
{ {
@ -20,10 +19,7 @@ namespace SieveTests.Migrations
LikeCount = table.Column<int>(nullable: false), LikeCount = table.Column<int>(nullable: false),
Title = table.Column<string>(nullable: true) Title = table.Column<string>(nullable: true)
}, },
constraints: table => constraints: table => table.PrimaryKey("PK_Posts", x => x.Id));
{
table.PrimaryKey("PK_Posts", x => x.Id);
});
} }
protected override void Down(MigrationBuilder migrationBuilder) protected override void Down(MigrationBuilder migrationBuilder)

View File

@ -1,16 +1,9 @@
using System; using Microsoft.AspNetCore;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace SieveTests namespace SieveTests
{ {
public class Program public static class Program
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {

View File

@ -2,10 +2,6 @@
using Sieve.Models; using Sieve.Models;
using Sieve.Services; using Sieve.Services;
using SieveTests.Entities; using SieveTests.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SieveTests.Services namespace SieveTests.Services
{ {

View File

@ -1,20 +1,12 @@
using Sieve.Services; using Sieve.Services;
using SieveTests.Entities; using SieveTests.Entities;
using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
namespace SieveTests.Services namespace SieveTests.Services
{ {
public class SieveCustomFilterMethods : ISieveCustomFilterMethods public class SieveCustomFilterMethods : ISieveCustomFilterMethods
{ {
public IQueryable<Post> IsNew(IQueryable<Post> source, string op, string value) public IQueryable<Post> IsNew(IQueryable<Post> source)
{ => source.Where(p => p.LikeCount < 100 && p.CommentCount < 5);
var result = source.Where(p => p.LikeCount < 100 &&
p.CommentCount < 5);
return result;
}
} }
} }

View File

@ -1,23 +1,15 @@
using Sieve.Services; using System.Linq;
using Sieve.Services;
using SieveTests.Entities; using SieveTests.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SieveTests.Services namespace SieveTests.Services
{ {
public class SieveCustomSortMethods : ISieveCustomSortMethods public class SieveCustomSortMethods : ISieveCustomSortMethods
{ {
public IQueryable<Post> Popularity(IQueryable<Post> source, bool useThenBy, bool desc) public IQueryable<Post> Popularity(IQueryable<Post> source, bool useThenBy) => useThenBy
{ ? ((IOrderedQueryable<Post>)source).ThenBy(p => p.LikeCount)
var result = useThenBy ? : source.OrderBy(p => p.LikeCount)
((IOrderedQueryable<Post>)source).ThenBy(p => p.LikeCount) :
source.OrderBy(p => p.LikeCount)
.ThenBy(p => p.CommentCount) .ThenBy(p => p.CommentCount)
.ThenBy(p => p.DateCreated); .ThenBy(p => p.DateCreated);
return result;
}
} }
} }

View File

@ -1,14 +1,8 @@
using System; using Microsoft.AspNetCore.Builder;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Sieve.Models; using Sieve.Models;
using Sieve.Services; using Sieve.Services;
using SieveTests.Entities; using SieveTests.Entities;

View File

@ -1,10 +1,4 @@
using System; namespace SieveUnitTests.Entities
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Sieve.Attributes;
namespace SieveUnitTests.Entities
{ {
public class Comment public class Comment
{ {

View File

@ -1,7 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Sieve.Attributes; using Sieve.Attributes;
namespace SieveUnitTests.Entities namespace SieveUnitTests.Entities

View File

@ -1,20 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sieve.Exceptions;
using Sieve.Models; using Sieve.Models;
using Sieve.Services; using Sieve.Services;
using SieveUnitTests.Entities; using SieveUnitTests.Entities;
using SieveUnitTests.Services; using SieveUnitTests.Services;
using System;
using System.Linq;
using System.Collections.Generic;
using Sieve.Exceptions;
namespace SieveUnitTests namespace SieveUnitTests
{ {
[TestClass] [TestClass]
public class General public class General
{ {
private SieveProcessor _processor; private readonly SieveProcessor _processor;
private IQueryable<Post> _posts; private readonly IQueryable<Post> _posts;
public General() public General()
{ {
@ -43,7 +43,7 @@ namespace SieveUnitTests
}, },
new Post() { new Post() {
Id = 3, Id = 3,
Title = "3", Title = "D",
LikeCount = 3, LikeCount = 3,
IsDraft = true IsDraft = true
}, },
@ -90,7 +90,6 @@ namespace SieveUnitTests
Assert.IsTrue(result.Count() == 2); Assert.IsTrue(result.Count() == 2);
} }
[TestMethod] [TestMethod]
public void CanSortBools() public void CanSortBools()
{ {
@ -112,14 +111,12 @@ namespace SieveUnitTests
Filters = "LikeCount==50", Filters = "LikeCount==50",
}; };
Console.WriteLine(model.FiltersParsed.First().Value); Console.WriteLine(model.FiltersParsed[0].Value);
Console.WriteLine(model.FiltersParsed.First().Operator); Console.WriteLine(model.FiltersParsed[0].Operator);
Console.WriteLine(model.FiltersParsed.First().OperatorParsed); Console.WriteLine(model.FiltersParsed[0].OperatorParsed);
var result = _processor.Apply(model, _posts); var result = _processor.Apply(model, _posts);
Assert.AreEqual(result.First().Id, 1); Assert.AreEqual(result.First().Id, 1);
Assert.IsTrue(result.Count() == 1); Assert.IsTrue(result.Count() == 1);
} }
@ -169,15 +166,12 @@ namespace SieveUnitTests
}; };
var result = _processor.Apply(model, _posts); var result = _processor.Apply(model, _posts);
var entry = result.FirstOrDefault();
var resultCount = result.Count();
Assert.AreEqual(result.First().Id, 3); Assert.IsNotNull(entry);
Assert.IsTrue(result.Count() == 1); Assert.AreEqual(1, resultCount);
Assert.AreEqual(3, entry.Id);
} }
} }
} }
//
//Sorts = "LikeCount",
//Page = 1,
//PageSize = 10
//

View File

@ -1,9 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sieve.Models; using Sieve.Models;
using Sieve.Services;
using SieveUnitTests.Entities; using SieveUnitTests.Entities;
using SieveUnitTests.Services; using SieveUnitTests.Services;
using System;
using System.Linq; using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using Sieve.Exceptions; using Sieve.Exceptions;
@ -13,8 +11,8 @@ namespace SieveUnitTests
[TestClass] [TestClass]
public class Mapper public class Mapper
{ {
private ApplicationSieveProcessor _processor; private readonly ApplicationSieveProcessor _processor;
private IQueryable<Post> _posts; private readonly IQueryable<Post> _posts;
public Mapper() public Mapper()
{ {

View File

@ -2,10 +2,6 @@
using Sieve.Models; using Sieve.Models;
using Sieve.Services; using Sieve.Services;
using SieveUnitTests.Entities; using SieveUnitTests.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SieveUnitTests.Services namespace SieveUnitTests.Services
{ {

View File

@ -1,24 +1,15 @@
using Sieve.Services; using System.Linq;
using Sieve.Services;
using SieveUnitTests.Entities; using SieveUnitTests.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SieveUnitTests.Services namespace SieveUnitTests.Services
{ {
public class SieveCustomFilterMethods : ISieveCustomFilterMethods public class SieveCustomFilterMethods : ISieveCustomFilterMethods
{ {
public IQueryable<Post> IsNew(IQueryable<Post> source, string op, string value) public IQueryable<Post> IsNew(IQueryable<Post> source)
{ => source.Where(p => p.LikeCount < 100);
var result = source.Where(p => p.LikeCount < 100);
return result; public IQueryable<Comment> TestComment(IQueryable<Comment> source)
} => source;
public IQueryable<Comment> TestComment(IQueryable<Comment> source, string op, string value)
{
return source;
}
} }
} }

View File

@ -1,23 +1,15 @@
using Sieve.Services; using System.Linq;
using Sieve.Services;
using SieveUnitTests.Entities; using SieveUnitTests.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SieveUnitTests.Services namespace SieveUnitTests.Services
{ {
public class SieveCustomSortMethods : ISieveCustomSortMethods public class SieveCustomSortMethods : ISieveCustomSortMethods
{ {
public IQueryable<Post> Popularity(IQueryable<Post> source, bool useThenBy, bool desc) public IQueryable<Post> Popularity(IQueryable<Post> source, bool useThenBy) => useThenBy
{ ? ((IOrderedQueryable<Post>)source).ThenBy(p => p.LikeCount)
var result = useThenBy ? : source.OrderBy(p => p.LikeCount)
((IOrderedQueryable<Post>)source).ThenBy(p => p.LikeCount) :
source.OrderBy(p => p.LikeCount)
.ThenBy(p => p.CommentCount) .ThenBy(p => p.CommentCount)
.ThenBy(p => p.DateCreated); .ThenBy(p => p.DateCreated);
return result;
}
} }
} }

View File

@ -1,26 +1,15 @@
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Sieve.Models; using Sieve.Models;
using System;
using System.Collections.Generic;
using System.Text;
namespace SieveUnitTests namespace SieveUnitTests
{ {
public class SieveOptionsAccessor : IOptions<SieveOptions> public class SieveOptionsAccessor : IOptions<SieveOptions>
{ {
private SieveOptions _value; public SieveOptions Value { get; }
public SieveOptions Value
{
get
{
return _value;
}
}
public SieveOptionsAccessor() public SieveOptionsAccessor()
{ {
_value = new SieveOptions() Value = new SieveOptions()
{ {
ThrowExceptions = true ThrowExceptions = true
}; };