thangchung/clean-architecture-dotnet

Extension.ApplySorting parameters are incorrect

whighsmith opened this issue · 1 comments

    public static void ApplySorting(this IRootSpecification gridSpec,
        string sort,
        string orderByDescendingMethodName,
        string groupByMethodName)
    {

should be

    public static void ApplySorting(this IRootSpecification gridSpec,
        string sort,
        string orderByMethodName,
        string orderByDescendingMethodName)
    {

and very last "else" block should be...

            else
            {
                specificationType.GetMethod(
                        orderByMethodName,
                        BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                    .Invoke(gridSpec, new object[] { propertyReturningExpression });
            }

awesome sample....thank you.

The following implementation of ListSpecificationBase supports OrderBy / ThenBy sorting

public abstract class ListSpecificationBase<T> : IListSpecification<T>
{
    public virtual List<Expression<Func<T, bool>>> Criterias { get; } = new();
    public List<Expression<Func<T, object>>> Includes { get; } = new();
    public List<string> IncludeStrings { get; } = new();
    public Expression<Func<T, object>> OrderBy { get; private set; }
    public Expression<Func<T, object>> ThenBy { get; private set; }
    public Expression<Func<T, object>> OrderByDescending { get; private set; }
    public Expression<Func<T, object>> ThenByDescending { get; private set; }
    public Expression<Func<T, object>> GroupBy { get; private set; }

    public int Take { get; private set; }
    public int Skip { get; private set; }
    public bool IsPagingEnabled { get; set; }

    protected void ApplyIncludeList(IEnumerable<Expression<Func<T, object>>> includes)
    {
        foreach (var include in includes)
        {
            AddInclude(include);
        }
    }

    protected void AddInclude(Expression<Func<T, object>> includeExpression)
    {
        Includes.Add(includeExpression);
    }

    protected void ApplyIncludeList(IEnumerable<string> includes)
    {
        foreach (var include in includes)
        {
            AddInclude(include);
        }
    }

    protected void AddInclude(string includeString)
    {
        IncludeStrings.Add(includeString);
    }

    protected IListSpecification<T> ApplyFilterList(IEnumerable<FilterModel> filters)
    {
        foreach (var (fieldName, comparision, fieldValue) in filters)
        {
            ApplyFilter(PredicateBuilder.Build<T>(fieldName, comparision, fieldValue));
        }

        return this;
    }

    protected IListSpecification<T> ApplyFilter(Expression<Func<T, bool>> expr)
    {
        Criterias.Add(expr);

        return this;
    }

    protected void ApplyPaging(int skip, int take)
    {
        Skip = skip;
        Take = take;
        IsPagingEnabled = true;
    }

    protected void ApplyOrderBy(Expression<Func<T, object>> orderByExpression) =>
        OrderBy = orderByExpression;

    protected void ApplyThenOrderBy(Expression<Func<T, object>> thenByExpression) =>
        ThenBy = thenByExpression;

    protected void ApplyOrderByDescending(Expression<Func<T, object>> orderByDescendingExpression) =>
        OrderByDescending = orderByDescendingExpression;

    protected void ApplyThenOrderByDescending(Expression<Func<T, object>> thenByDescendingExpression) =>
        ThenByDescending = thenByDescendingExpression;

    protected void ApplyGroupBy(Expression<Func<T, object>> groupByExpression) =>
        GroupBy = groupByExpression;

    protected void ApplySortingList(IList<string> sorts)
    {
        if (sorts.Count > 0)
        {
            //  Get first sort item and remove it from sorts list
            var sort1 = sorts[0];
            sorts.RemoveAt(0);

            //  Apply OrderBy to first item
            ApplySorting(sort1);

            //  Apply ThenBy to subsequent items
            foreach (var sort in sorts)
            {
                ApplyThenBySorting(sort);
            }
        }
    }

    protected void ApplySorting(string sort)
    {
        this.ApplySorting(sort, nameof(ApplyOrderBy), nameof(ApplyOrderByDescending));
    }

    protected void ApplyThenBySorting(string sort)
    {
        this.ApplySorting(sort, nameof(ApplyThenOrderBy), nameof(ApplyThenOrderByDescending));
    }
}