extracted alot of things in the base class. :)
This commit is contained in:
parent
bb309f9ffb
commit
8abed33160
@ -9,90 +9,17 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace PoweredSoft.DynamicLinq.Fluent
|
||||
{
|
||||
public class QueryBuilder<T>
|
||||
public class QueryBuilder<T> : QueryBuilderBase
|
||||
{
|
||||
public IQueryable<T> Query { get; set; }
|
||||
|
||||
public Type QueryableType { get; set; }
|
||||
|
||||
public bool IsNullCheckingEnabled { get; protected set; } = false;
|
||||
|
||||
public List<QueryBuilderFilter> Filters { get; protected set; } = new List<QueryBuilderFilter>();
|
||||
|
||||
public List<QueryBuilderSort> Sorts { get; protected set; } = new List<QueryBuilderSort>();
|
||||
|
||||
|
||||
public QueryBuilder(IQueryable<T> query)
|
||||
{
|
||||
Query = query;
|
||||
}
|
||||
|
||||
public QueryBuilder<T> NullChecking(bool check = true)
|
||||
{
|
||||
IsNullCheckingEnabled = check;
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual QueryBuilder<T> Compare(string path, ConditionOperators conditionOperators, object value,
|
||||
QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField,
|
||||
bool and = true, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any)
|
||||
{
|
||||
Filters.Add(new QueryBuilderFilter
|
||||
{
|
||||
And = and,
|
||||
ConditionOperator = conditionOperators,
|
||||
Path = path,
|
||||
Value = value,
|
||||
ConvertStrategy = convertStrategy,
|
||||
CollectionHandling = collectionHandling
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual QueryBuilder<T> Sort(string path, SortOrder sortOrder, bool appendSort)
|
||||
{
|
||||
Sorts.Add(new QueryBuilderSort
|
||||
{
|
||||
Path = path,
|
||||
SortOrder = sortOrder,
|
||||
AppendSort = appendSort
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual QueryBuilder<T> SubQuery(Action<QueryBuilder<T>> subQuery, bool and = true)
|
||||
{
|
||||
// create query builder for same type.
|
||||
var qb = new QueryBuilder<T>(Query);
|
||||
qb.NullChecking(IsNullCheckingEnabled);
|
||||
|
||||
// callback.
|
||||
subQuery(qb);
|
||||
|
||||
// create a query part.
|
||||
var part = new QueryBuilderFilter();
|
||||
part.And = and;
|
||||
part.Filters = qb.Filters;
|
||||
Filters.Add(part);
|
||||
|
||||
//return self.
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilder<T> And(string path, ConditionOperators conditionOperator, object value,
|
||||
QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any)
|
||||
=> Compare(path, conditionOperator, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, and: true);
|
||||
|
||||
public QueryBuilder<T> Or(string path, ConditionOperators conditionOperator, object value,
|
||||
QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any)
|
||||
=> Compare(path, conditionOperator, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, and: false);
|
||||
|
||||
public QueryBuilder<T> And(Action<QueryBuilder<T>> subQuery)
|
||||
=> SubQuery(subQuery, true);
|
||||
|
||||
public QueryBuilder<T> Or(Action<QueryBuilder<T>> subQuery)
|
||||
=> SubQuery(subQuery, false);
|
||||
|
||||
public virtual IQueryable<T> Build()
|
||||
{
|
||||
// the query.
|
||||
@ -225,5 +152,10 @@ namespace PoweredSoft.DynamicLinq.Fluent
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected override QueryBuilderBase GetSubQueryBuilder()
|
||||
{
|
||||
return new QueryBuilder<T>(Query);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
88
PoweredSoft.DynamicLinq/Fluent/QueryBuilderBase.cs
Normal file
88
PoweredSoft.DynamicLinq/Fluent/QueryBuilderBase.cs
Normal file
@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.CodeDom;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PoweredSoft.DynamicLinq.Fluent
|
||||
{
|
||||
public abstract class QueryBuilderBase
|
||||
{
|
||||
public bool IsNullCheckingEnabled { get; protected set; } = false;
|
||||
|
||||
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)
|
||||
{
|
||||
IsNullCheckingEnabled = check;
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual QueryBuilderBase Compare(string path, ConditionOperators conditionOperators, object value,
|
||||
QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField,
|
||||
bool and = true, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any)
|
||||
{
|
||||
Filters.Add(new QueryBuilderFilter
|
||||
{
|
||||
And = and,
|
||||
ConditionOperator = conditionOperators,
|
||||
Path = path,
|
||||
Value = value,
|
||||
ConvertStrategy = convertStrategy,
|
||||
CollectionHandling = collectionHandling
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual QueryBuilderBase Sort(string path, SortOrder sortOrder, bool appendSort)
|
||||
{
|
||||
Sorts.Add(new QueryBuilderSort
|
||||
{
|
||||
Path = path,
|
||||
SortOrder = sortOrder,
|
||||
AppendSort = appendSort
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
protected abstract QueryBuilderBase GetSubQueryBuilder();
|
||||
|
||||
public virtual QueryBuilderBase SubQuery(Action<QueryBuilderBase> subQuery, bool and = true)
|
||||
{
|
||||
// create query builder for same type.
|
||||
var qb = GetSubQueryBuilder();
|
||||
qb.NullChecking(IsNullCheckingEnabled);
|
||||
|
||||
// callback.
|
||||
subQuery(qb);
|
||||
|
||||
// create a query part.
|
||||
var part = new QueryBuilderFilter();
|
||||
part.And = and;
|
||||
part.Filters = qb.Filters;
|
||||
Filters.Add(part);
|
||||
|
||||
//return self.
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryBuilderBase And(string path, ConditionOperators conditionOperator, object value,
|
||||
QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any)
|
||||
=> Compare(path, conditionOperator, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, and: true);
|
||||
|
||||
public QueryBuilderBase Or(string path, ConditionOperators conditionOperator, object value,
|
||||
QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any)
|
||||
=> Compare(path, conditionOperator, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, and: false);
|
||||
|
||||
public QueryBuilderBase And(Action<QueryBuilderBase> subQuery)
|
||||
=> SubQuery(subQuery, true);
|
||||
|
||||
public QueryBuilderBase Or(Action<QueryBuilderBase> subQuery)
|
||||
=> SubQuery(subQuery, false);
|
||||
}
|
||||
}
|
@ -42,6 +42,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="Constants.cs" />
|
||||
<Compile Include="Extensions\QueryableExtensions.cs" />
|
||||
<Compile Include="Fluent\QueryBuilderBase.cs" />
|
||||
<Compile Include="Fluent\QueryBuilderSort.cs" />
|
||||
<Compile Include="Helpers\QueryableHelpers.cs" />
|
||||
<Compile Include="Helpers\TypeHelpers.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user