From 8abed331602407e1604f610b7107b2e0d93b84c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Leb=C3=A9e?= Date: Tue, 13 Feb 2018 22:47:03 -0600 Subject: [PATCH] extracted alot of things in the base class. :) --- .../Fluent/QueryBuilder.cs | 82 ++--------------- .../Fluent/QueryBuilderBase.cs | 88 +++++++++++++++++++ .../PoweredSoft.DynamicLinq.csproj | 1 + 3 files changed, 96 insertions(+), 75 deletions(-) create mode 100644 PoweredSoft.DynamicLinq/Fluent/QueryBuilderBase.cs diff --git a/PoweredSoft.DynamicLinq/Fluent/QueryBuilder.cs b/PoweredSoft.DynamicLinq/Fluent/QueryBuilder.cs index f39ac7d..4b3d487 100644 --- a/PoweredSoft.DynamicLinq/Fluent/QueryBuilder.cs +++ b/PoweredSoft.DynamicLinq/Fluent/QueryBuilder.cs @@ -9,90 +9,17 @@ using System.Threading.Tasks; namespace PoweredSoft.DynamicLinq.Fluent { - public class QueryBuilder + public class QueryBuilder : QueryBuilderBase { public IQueryable Query { get; set; } public Type QueryableType { get; set; } - - public bool IsNullCheckingEnabled { get; protected set; } = false; - - public List Filters { get; protected set; } = new List(); - - public List Sorts { get; protected set; } = new List(); - + public QueryBuilder(IQueryable query) { Query = query; } - public QueryBuilder NullChecking(bool check = true) - { - IsNullCheckingEnabled = check; - return this; - } - - public virtual QueryBuilder 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 Sort(string path, SortOrder sortOrder, bool appendSort) - { - Sorts.Add(new QueryBuilderSort - { - Path = path, - SortOrder = sortOrder, - AppendSort = appendSort - }); - return this; - } - - public virtual QueryBuilder SubQuery(Action> subQuery, bool and = true) - { - // create query builder for same type. - var qb = new QueryBuilder(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 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 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 And(Action> subQuery) - => SubQuery(subQuery, true); - - public QueryBuilder Or(Action> subQuery) - => SubQuery(subQuery, false); - public virtual IQueryable Build() { // the query. @@ -225,5 +152,10 @@ namespace PoweredSoft.DynamicLinq.Fluent return ret; } + + protected override QueryBuilderBase GetSubQueryBuilder() + { + return new QueryBuilder(Query); + } } } diff --git a/PoweredSoft.DynamicLinq/Fluent/QueryBuilderBase.cs b/PoweredSoft.DynamicLinq/Fluent/QueryBuilderBase.cs new file mode 100644 index 0000000..f0092f2 --- /dev/null +++ b/PoweredSoft.DynamicLinq/Fluent/QueryBuilderBase.cs @@ -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 Filters { get; protected set; } = new List(); + + public List Sorts { get; protected set; } = new List(); + + 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 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 subQuery) + => SubQuery(subQuery, true); + + public QueryBuilderBase Or(Action subQuery) + => SubQuery(subQuery, false); + } +} diff --git a/PoweredSoft.DynamicLinq/PoweredSoft.DynamicLinq.csproj b/PoweredSoft.DynamicLinq/PoweredSoft.DynamicLinq.csproj index d8ad431..a2894bd 100644 --- a/PoweredSoft.DynamicLinq/PoweredSoft.DynamicLinq.csproj +++ b/PoweredSoft.DynamicLinq/PoweredSoft.DynamicLinq.csproj @@ -42,6 +42,7 @@ +