Comments (1)
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)
- Unit test and integration tests
- TiDB and Yugabyte experiments
- Refactor to the truly minimal routing on each services
- Build CRUD apps HOT 1
- i18n HOT 1
- How to install tye: 0.8.0-alpha.21301.1+0fed0b38e730cd07caf0a90287090638c110b77d HOT 1
- Refit on Dapr client calling HOT 1
- Apply EF Core compiled model to boost the startup and query performance
- Error handling
- Cant build via docker in m1 HOT 2
- [Sample] Dashboard statistics HOT 2
- Setup Dapr OAuth 2.0 with identityserver
- Clean Architecture CLI
- the branch inmem-outbox cannot run with tye
- Add versioning for Rest Api + Swagger
- Add YARP on top of services
- Event Bus and Transaction Outbox on Dapr
- Package the Building Blocks into Github nugets
- Bump to .NET 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clean-architecture-dotnet.