Giter VIP home page Giter VIP logo

Comments (1)

whighsmith avatar whighsmith commented on September 4, 2024

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));
    }
}

from clean-architecture-dotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.