seperate sort builder from filter builder.
makes logic more seperated.
This commit is contained in:
parent
0540554877
commit
71b207ef78
@ -82,7 +82,7 @@ namespace PoweredSoft.DynamicLinq.Test
|
|||||||
|
|
||||||
// the query.
|
// the query.
|
||||||
var query = posts.AsQueryable();
|
var query = posts.AsQueryable();
|
||||||
var queryBuilder = new PoweredSoft.DynamicLinq.Fluent.QueryBuilder<Post>(query);
|
var queryBuilder = new PoweredSoft.DynamicLinq.Fluent.SortBuilder<Post>(query);
|
||||||
|
|
||||||
// add some sorting.
|
// add some sorting.
|
||||||
queryBuilder
|
queryBuilder
|
||||||
|
@ -32,7 +32,7 @@ namespace PoweredSoft.DynamicLinq
|
|||||||
|
|
||||||
public static IQueryable<T> Sort<T>(this IQueryable<T> query, string path, QuerySortDirection sortDirection, bool appendSort)
|
public static IQueryable<T> Sort<T>(this IQueryable<T> query, string path, QuerySortDirection sortDirection, bool appendSort)
|
||||||
{
|
{
|
||||||
var qb = new QueryBuilder<T>(query);
|
var qb = new SortBuilder<T>(query);
|
||||||
qb.Sort(path, sortDirection, appendSort);
|
qb.Sort(path, sortDirection, appendSort);
|
||||||
var ret = qb.Build();
|
var ret = qb.Build();
|
||||||
return ret;
|
return ret;
|
||||||
@ -40,7 +40,7 @@ namespace PoweredSoft.DynamicLinq
|
|||||||
|
|
||||||
public static IQueryable<T> OrderBy<T>(this IQueryable<T> query, string path)
|
public static IQueryable<T> OrderBy<T>(this IQueryable<T> query, string path)
|
||||||
{
|
{
|
||||||
var qb = new QueryBuilder<T>(query);
|
var qb = new SortBuilder<T>(query);
|
||||||
qb.OrderBy(path);
|
qb.OrderBy(path);
|
||||||
var ret = qb.Build();
|
var ret = qb.Build();
|
||||||
return ret;
|
return ret;
|
||||||
@ -48,7 +48,7 @@ namespace PoweredSoft.DynamicLinq
|
|||||||
|
|
||||||
public static IQueryable<T> OrderByDescending<T>(this IQueryable<T> query, string path)
|
public static IQueryable<T> OrderByDescending<T>(this IQueryable<T> query, string path)
|
||||||
{
|
{
|
||||||
var qb = new QueryBuilder<T>(query);
|
var qb = new SortBuilder<T>(query);
|
||||||
qb.OrderByDescending(path);
|
qb.OrderByDescending(path);
|
||||||
var ret = qb.Build();
|
var ret = qb.Build();
|
||||||
return ret;
|
return ret;
|
||||||
@ -56,7 +56,7 @@ namespace PoweredSoft.DynamicLinq
|
|||||||
|
|
||||||
public static IQueryable<T> ThenBy<T>(this IQueryable<T> query, string path)
|
public static IQueryable<T> ThenBy<T>(this IQueryable<T> query, string path)
|
||||||
{
|
{
|
||||||
var qb = new QueryBuilder<T>(query);
|
var qb = new SortBuilder<T>(query);
|
||||||
qb.ThenBy(path);
|
qb.ThenBy(path);
|
||||||
var ret = qb.Build();
|
var ret = qb.Build();
|
||||||
return ret;
|
return ret;
|
||||||
@ -64,7 +64,7 @@ namespace PoweredSoft.DynamicLinq
|
|||||||
|
|
||||||
public static IQueryable<T> ThenByDescending<T>(this IQueryable<T> query, string path)
|
public static IQueryable<T> ThenByDescending<T>(this IQueryable<T> query, string path)
|
||||||
{
|
{
|
||||||
var qb = new QueryBuilder<T>(query);
|
var qb = new SortBuilder<T>(query);
|
||||||
qb.ThenByDescending(path);
|
qb.ThenByDescending(path);
|
||||||
var ret = qb.Build();
|
var ret = qb.Build();
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -12,8 +12,6 @@ namespace PoweredSoft.DynamicLinq.Fluent
|
|||||||
{
|
{
|
||||||
public IQueryable<T> Query { get; set; }
|
public IQueryable<T> Query { get; set; }
|
||||||
|
|
||||||
public Type QueryableType { get; set; }
|
|
||||||
|
|
||||||
public QueryBuilder(IQueryable<T> query)
|
public QueryBuilder(IQueryable<T> query)
|
||||||
{
|
{
|
||||||
Query = query;
|
Query = query;
|
||||||
@ -24,27 +22,6 @@ namespace PoweredSoft.DynamicLinq.Fluent
|
|||||||
// the query.
|
// the query.
|
||||||
var query = Query;
|
var query = Query;
|
||||||
|
|
||||||
// build the filters.
|
|
||||||
query = BuildFilters(query);
|
|
||||||
|
|
||||||
// build the sorts
|
|
||||||
query = BuildSorts(query);
|
|
||||||
|
|
||||||
return query;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual IQueryable<T> BuildSorts(IQueryable<T> query)
|
|
||||||
{
|
|
||||||
Sorts.ForEach(sort =>
|
|
||||||
{
|
|
||||||
query = QueryableHelpers.CreateSortExpression(query, sort.Path, sort.sortDirection, sort.AppendSort);
|
|
||||||
});
|
|
||||||
|
|
||||||
return query;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual IQueryable<T> BuildFilters(IQueryable<T> query)
|
|
||||||
{
|
|
||||||
if (Filters == null || Filters?.Count() == 0)
|
if (Filters == null || Filters?.Count() == 0)
|
||||||
return query;
|
return query;
|
||||||
|
|
||||||
|
@ -13,8 +13,6 @@ namespace PoweredSoft.DynamicLinq.Fluent
|
|||||||
|
|
||||||
public List<QueryBuilderFilter> Filters { get; protected set; } = new List<QueryBuilderFilter>();
|
public List<QueryBuilderFilter> Filters { get; protected set; } = new List<QueryBuilderFilter>();
|
||||||
|
|
||||||
public List<QueryBuilderSort> Sorts { get; protected set; } = new List<QueryBuilderSort>();
|
|
||||||
|
|
||||||
public virtual QueryBuilderBase NullChecking(bool check = true)
|
public virtual QueryBuilderBase NullChecking(bool check = true)
|
||||||
{
|
{
|
||||||
IsNullCheckingEnabled = check;
|
IsNullCheckingEnabled = check;
|
||||||
@ -39,17 +37,6 @@ namespace PoweredSoft.DynamicLinq.Fluent
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual QueryBuilderBase Sort(string path, QuerySortDirection sortDirection, bool appendSort)
|
|
||||||
{
|
|
||||||
Sorts.Add(new QueryBuilderSort
|
|
||||||
{
|
|
||||||
Path = path,
|
|
||||||
sortDirection = sortDirection,
|
|
||||||
AppendSort = appendSort
|
|
||||||
});
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract QueryBuilderBase GetSubQueryBuilder();
|
protected abstract QueryBuilderBase GetSubQueryBuilder();
|
||||||
|
|
||||||
public virtual QueryBuilderBase SubQuery(Action<QueryBuilderBase> subQuery, bool and = true)
|
public virtual QueryBuilderBase SubQuery(Action<QueryBuilderBase> subQuery, bool and = true)
|
||||||
@ -70,51 +57,5 @@ namespace PoweredSoft.DynamicLinq.Fluent
|
|||||||
//return self.
|
//return self.
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual QueryBuilderBase OrderBy(string path)
|
|
||||||
{
|
|
||||||
Sorts.Clear();
|
|
||||||
Sorts.Add(new QueryBuilderSort
|
|
||||||
{
|
|
||||||
Path = path,
|
|
||||||
sortDirection = QuerySortDirection.Ascending,
|
|
||||||
AppendSort = false
|
|
||||||
});
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual QueryBuilderBase OrderByDescending(string path)
|
|
||||||
{
|
|
||||||
Sorts.Clear();
|
|
||||||
Sorts.Add(new QueryBuilderSort
|
|
||||||
{
|
|
||||||
Path = path,
|
|
||||||
sortDirection = QuerySortDirection.Descending,
|
|
||||||
AppendSort = false
|
|
||||||
});
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual QueryBuilderBase ThenBy(string path)
|
|
||||||
{
|
|
||||||
Sorts.Add(new QueryBuilderSort
|
|
||||||
{
|
|
||||||
Path = path,
|
|
||||||
sortDirection = QuerySortDirection.Ascending,
|
|
||||||
AppendSort = true
|
|
||||||
});
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual QueryBuilderBase ThenByDescending(string path)
|
|
||||||
{
|
|
||||||
Sorts.Add(new QueryBuilderSort
|
|
||||||
{
|
|
||||||
Path = path,
|
|
||||||
sortDirection = QuerySortDirection.Descending,
|
|
||||||
AppendSort = true
|
|
||||||
});
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
92
PoweredSoft.DynamicLinq/Fluent/Sort/SortBuilder.cs
Normal file
92
PoweredSoft.DynamicLinq/Fluent/Sort/SortBuilder.cs
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
using PoweredSoft.DynamicLinq.Helpers;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace PoweredSoft.DynamicLinq.Fluent
|
||||||
|
{
|
||||||
|
public class SortBuilderBase
|
||||||
|
{
|
||||||
|
public List<QueryBuilderSort> Sorts { get; protected set; } = new List<QueryBuilderSort>();
|
||||||
|
|
||||||
|
public virtual SortBuilderBase Sort(string path, QuerySortDirection sortDirection, bool appendSort)
|
||||||
|
{
|
||||||
|
Sorts.Add(new QueryBuilderSort
|
||||||
|
{
|
||||||
|
Path = path,
|
||||||
|
sortDirection = sortDirection,
|
||||||
|
AppendSort = appendSort
|
||||||
|
});
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual SortBuilderBase OrderBy(string path)
|
||||||
|
{
|
||||||
|
Sorts.Clear();
|
||||||
|
Sorts.Add(new QueryBuilderSort
|
||||||
|
{
|
||||||
|
Path = path,
|
||||||
|
sortDirection = QuerySortDirection.Ascending,
|
||||||
|
AppendSort = false
|
||||||
|
});
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual SortBuilderBase OrderByDescending(string path)
|
||||||
|
{
|
||||||
|
Sorts.Clear();
|
||||||
|
Sorts.Add(new QueryBuilderSort
|
||||||
|
{
|
||||||
|
Path = path,
|
||||||
|
sortDirection = QuerySortDirection.Descending,
|
||||||
|
AppendSort = false
|
||||||
|
});
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual SortBuilderBase ThenBy(string path)
|
||||||
|
{
|
||||||
|
Sorts.Add(new QueryBuilderSort
|
||||||
|
{
|
||||||
|
Path = path,
|
||||||
|
sortDirection = QuerySortDirection.Ascending,
|
||||||
|
AppendSort = true
|
||||||
|
});
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual SortBuilderBase ThenByDescending(string path)
|
||||||
|
{
|
||||||
|
Sorts.Add(new QueryBuilderSort
|
||||||
|
{
|
||||||
|
Path = path,
|
||||||
|
sortDirection = QuerySortDirection.Descending,
|
||||||
|
AppendSort = true
|
||||||
|
});
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class SortBuilder<T> : SortBuilderBase
|
||||||
|
{
|
||||||
|
public IQueryable<T> Query { get; }
|
||||||
|
|
||||||
|
public SortBuilder(IQueryable<T> query)
|
||||||
|
{
|
||||||
|
Query = query;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual IQueryable<T> Build()
|
||||||
|
{
|
||||||
|
var query = Query;
|
||||||
|
|
||||||
|
Sorts.ForEach(sort =>
|
||||||
|
{
|
||||||
|
query = QueryableHelpers.CreateSortExpression(query, sort.Path, sort.sortDirection, sort.AppendSort);
|
||||||
|
});
|
||||||
|
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user