Defer to interface

This commit is contained in:
Biarity 2018-02-07 21:51:29 +10:00
parent fe7234f571
commit 2a170df0c2
2 changed files with 28 additions and 13 deletions

View File

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

View File

@ -49,7 +49,7 @@ namespace Sieve.Services
_options = options; _options = options;
} }
public IQueryable<TEntity> ApplyAll<TEntity>(ISieveModel model, IQueryable<TEntity> source) public IQueryable<TEntity> ApplyAll<TEntity>(ISieveModel model, IQueryable<TEntity> source, object[] dataForCustomMethods = null)
{ {
var result = source; var result = source;
@ -57,10 +57,10 @@ namespace Sieve.Services
return result; return result;
// Sort // Sort
result = ApplySorting(model, result); result = ApplySorting(model, result, dataForCustomMethods);
// Filter // Filter
result = ApplyFiltering(model, result); result = ApplyFiltering(model, result, dataForCustomMethods);
// Paginate // Paginate
result = ApplyPagination(model, result); result = ApplyPagination(model, result);
@ -68,7 +68,7 @@ namespace Sieve.Services
return result; return result;
} }
public IQueryable<TEntity> ApplySorting<TEntity>(ISieveModel model, IQueryable<TEntity> result) public IQueryable<TEntity> ApplySorting<TEntity>(ISieveModel model, IQueryable<TEntity> result, object[] dataForCustomMethods = null)
{ {
if (model?.SortsParsed == null) if (model?.SortsParsed == null)
return result; return result;
@ -90,7 +90,7 @@ namespace Sieve.Services
result, result,
useThenBy, useThenBy,
sortTerm.Descending sortTerm.Descending
}); }, dataForCustomMethods);
} }
useThenBy = true; useThenBy = true;
} }
@ -98,7 +98,7 @@ namespace Sieve.Services
return result; return result;
} }
public IQueryable<TEntity> ApplyFiltering<TEntity>(ISieveModel model, IQueryable<TEntity> result) public IQueryable<TEntity> ApplyFiltering<TEntity>(ISieveModel model, IQueryable<TEntity> result, object[] dataForCustomMethods = null)
{ {
if (model?.FiltersParsed == null) if (model?.FiltersParsed == null)
return result; return result;
@ -168,7 +168,7 @@ namespace Sieve.Services
result, result,
filterTerm.Operator, filterTerm.Operator,
filterTerm.Value filterTerm.Value
}); }, dataForCustomMethods);
} }
} }
@ -202,15 +202,30 @@ namespace Sieve.Services
}); });
} }
private IQueryable<TEntity> ApplyCustomMethod<TEntity>(IQueryable<TEntity> result, string name, object parent, object[] parameters) private IQueryable<TEntity> ApplyCustomMethod<TEntity>(IQueryable<TEntity> result, string name, object parent, object[] parameters, object[] optionalParameters = null)
{ {
var customMethod = parent?.GetType() var customMethod = parent?.GetType()
.GetMethod(name); .GetMethod(name);
if (customMethod != null) if (customMethod != null)
{ {
result = customMethod.Invoke(parent, parameters) try
as IQueryable<TEntity>; {
result = customMethod.Invoke(parent, parameters)
as IQueryable<TEntity>;
}
catch (TargetParameterCountException)
{
if (optionalParameters != null)
{
result = customMethod.Invoke(parent, parameters.Concat(optionalParameters).ToArray())
as IQueryable<TEntity>;
}
else
{
throw;
}
}
} }
return result; return result;