From 2a170df0c22f3b840745a7813bee840acb9cd2bb Mon Sep 17 00:00:00 2001 From: Biarity Date: Wed, 7 Feb 2018 21:51:29 +1000 Subject: [PATCH] Defer to interface --- Sieve/Services/ISieveProcessor.cs | 6 +++--- Sieve/Services/SieveProcessor.cs | 35 ++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/Sieve/Services/ISieveProcessor.cs b/Sieve/Services/ISieveProcessor.cs index 93885a3..6b0fa20 100644 --- a/Sieve/Services/ISieveProcessor.cs +++ b/Sieve/Services/ISieveProcessor.cs @@ -6,9 +6,9 @@ namespace Sieve.Services { public interface ISieveProcessor { - IQueryable ApplyAll(ISieveModel model, IQueryable source); - IQueryable ApplySorting(ISieveModel model, IQueryable result); - IQueryable ApplyFiltering(ISieveModel model, IQueryable result); + IQueryable ApplyAll(ISieveModel model, IQueryable source, object[] dataForCustomMethods = null); + IQueryable ApplySorting(ISieveModel model, IQueryable result, object[] dataForCustomMethods = null); + IQueryable ApplyFiltering(ISieveModel model, IQueryable result, object[] dataForCustomMethods = null); IQueryable ApplyPagination(ISieveModel model, IQueryable result); } } \ No newline at end of file diff --git a/Sieve/Services/SieveProcessor.cs b/Sieve/Services/SieveProcessor.cs index 6fac2c1..8d797e7 100644 --- a/Sieve/Services/SieveProcessor.cs +++ b/Sieve/Services/SieveProcessor.cs @@ -49,7 +49,7 @@ namespace Sieve.Services _options = options; } - public IQueryable ApplyAll(ISieveModel model, IQueryable source) + public IQueryable ApplyAll(ISieveModel model, IQueryable source, object[] dataForCustomMethods = null) { var result = source; @@ -57,10 +57,10 @@ namespace Sieve.Services return result; // Sort - result = ApplySorting(model, result); + result = ApplySorting(model, result, dataForCustomMethods); // Filter - result = ApplyFiltering(model, result); + result = ApplyFiltering(model, result, dataForCustomMethods); // Paginate result = ApplyPagination(model, result); @@ -68,7 +68,7 @@ namespace Sieve.Services return result; } - public IQueryable ApplySorting(ISieveModel model, IQueryable result) + public IQueryable ApplySorting(ISieveModel model, IQueryable result, object[] dataForCustomMethods = null) { if (model?.SortsParsed == null) return result; @@ -90,7 +90,7 @@ namespace Sieve.Services result, useThenBy, sortTerm.Descending - }); + }, dataForCustomMethods); } useThenBy = true; } @@ -98,7 +98,7 @@ namespace Sieve.Services return result; } - public IQueryable ApplyFiltering(ISieveModel model, IQueryable result) + public IQueryable ApplyFiltering(ISieveModel model, IQueryable result, object[] dataForCustomMethods = null) { if (model?.FiltersParsed == null) return result; @@ -168,7 +168,7 @@ namespace Sieve.Services result, filterTerm.Operator, filterTerm.Value - }); + }, dataForCustomMethods); } } @@ -202,15 +202,30 @@ namespace Sieve.Services }); } - private IQueryable ApplyCustomMethod(IQueryable result, string name, object parent, object[] parameters) + private IQueryable ApplyCustomMethod(IQueryable result, string name, object parent, object[] parameters, object[] optionalParameters = null) { var customMethod = parent?.GetType() .GetMethod(name); if (customMethod != null) { - result = customMethod.Invoke(parent, parameters) - as IQueryable; + try + { + result = customMethod.Invoke(parent, parameters) + as IQueryable; + } + catch (TargetParameterCountException) + { + if (optionalParameters != null) + { + result = customMethod.Invoke(parent, parameters.Concat(optionalParameters).ToArray()) + as IQueryable; + } + else + { + throw; + } + } } return result;