From 51e5e7a47ac6eab972a00a464f210974ab277bf2 Mon Sep 17 00:00:00 2001 From: Vladimir Seldemirov Date: Wed, 23 Oct 2024 09:21:55 +0400 Subject: [PATCH] Add BitzArt.CoreExtensions package reference to BitzArt.LinqExtensions (#14) * Add BitzArt.CoreExtensions package reference to BitzArt.LinqExtensions * Fix namespaces --- .../Extensions/BatchExtensions.cs | 2 +- .../Interfaces/IBatchQueryBuilder.cs | 2 +- .../Interfaces/IBatchingStrategy.cs | 2 +- .../Models/BatchQueryBuilder.cs | 2 +- .../Models/BatchingStrategy.cs | 2 +- .../Models/LastValueBatchingStrategy.cs | 2 +- .../Models/OffsetBatchingStrategy.cs | 2 +- .../BitzArt.LinqExtensions.csproj | 4 ++ .../Enums/FilterOperation.cs | 37 ------------------ .../Enums/OrderDirection.cs | 39 ------------------- .../Extensions/AddFilterExtension.cs | 29 +++++++------- .../Extensions/OrderExtensions.cs | 3 +- .../BatchTests.cs | 4 +- .../AddFilterTests.cs | 10 ++--- 14 files changed, 34 insertions(+), 106 deletions(-) delete mode 100644 src/BitzArt.LinqExtensions/Enums/FilterOperation.cs delete mode 100644 src/BitzArt.LinqExtensions/Enums/OrderDirection.cs diff --git a/src/BitzArt.LinqExtensions.Batching/Extensions/BatchExtensions.cs b/src/BitzArt.LinqExtensions.Batching/Extensions/BatchExtensions.cs index 458c966..a4a8647 100644 --- a/src/BitzArt.LinqExtensions.Batching/Extensions/BatchExtensions.cs +++ b/src/BitzArt.LinqExtensions.Batching/Extensions/BatchExtensions.cs @@ -1,4 +1,4 @@ -using BitzArt.LinqExtensions.Batching; +using BitzArt; using System.Linq.Expressions; namespace System.Linq; diff --git a/src/BitzArt.LinqExtensions.Batching/Interfaces/IBatchQueryBuilder.cs b/src/BitzArt.LinqExtensions.Batching/Interfaces/IBatchQueryBuilder.cs index f85d726..aa6548b 100644 --- a/src/BitzArt.LinqExtensions.Batching/Interfaces/IBatchQueryBuilder.cs +++ b/src/BitzArt.LinqExtensions.Batching/Interfaces/IBatchQueryBuilder.cs @@ -1,4 +1,4 @@ -namespace BitzArt.LinqExtensions.Batching; +namespace BitzArt; /// /// A builder of a batch query. diff --git a/src/BitzArt.LinqExtensions.Batching/Interfaces/IBatchingStrategy.cs b/src/BitzArt.LinqExtensions.Batching/Interfaces/IBatchingStrategy.cs index 1d2f211..2b133ce 100644 --- a/src/BitzArt.LinqExtensions.Batching/Interfaces/IBatchingStrategy.cs +++ b/src/BitzArt.LinqExtensions.Batching/Interfaces/IBatchingStrategy.cs @@ -1,4 +1,4 @@ -namespace BitzArt.LinqExtensions.Batching; +namespace BitzArt; /// /// A strategy to compose a batch query. diff --git a/src/BitzArt.LinqExtensions.Batching/Models/BatchQueryBuilder.cs b/src/BitzArt.LinqExtensions.Batching/Models/BatchQueryBuilder.cs index 2a8b0e1..06d426f 100644 --- a/src/BitzArt.LinqExtensions.Batching/Models/BatchQueryBuilder.cs +++ b/src/BitzArt.LinqExtensions.Batching/Models/BatchQueryBuilder.cs @@ -1,7 +1,7 @@ using System.Collections; using System.Linq.Expressions; -namespace BitzArt.LinqExtensions.Batching; +namespace BitzArt; internal class BatchQueryBuilder : IBatchQueryBuilder, IAsyncEnumerable { diff --git a/src/BitzArt.LinqExtensions.Batching/Models/BatchingStrategy.cs b/src/BitzArt.LinqExtensions.Batching/Models/BatchingStrategy.cs index 37e9b89..c768bf8 100644 --- a/src/BitzArt.LinqExtensions.Batching/Models/BatchingStrategy.cs +++ b/src/BitzArt.LinqExtensions.Batching/Models/BatchingStrategy.cs @@ -1,4 +1,4 @@ -namespace BitzArt.LinqExtensions.Batching; +namespace BitzArt; internal class BatchingStrategy : IBatchingStrategy { diff --git a/src/BitzArt.LinqExtensions.Batching/Models/LastValueBatchingStrategy.cs b/src/BitzArt.LinqExtensions.Batching/Models/LastValueBatchingStrategy.cs index f6e3f0b..f3d0049 100644 --- a/src/BitzArt.LinqExtensions.Batching/Models/LastValueBatchingStrategy.cs +++ b/src/BitzArt.LinqExtensions.Batching/Models/LastValueBatchingStrategy.cs @@ -1,6 +1,6 @@ using System.Linq.Expressions; -namespace BitzArt.LinqExtensions.Batching; +namespace BitzArt; internal class LastValueBatchingStrategy( IBatchQueryBuilder builder, diff --git a/src/BitzArt.LinqExtensions.Batching/Models/OffsetBatchingStrategy.cs b/src/BitzArt.LinqExtensions.Batching/Models/OffsetBatchingStrategy.cs index 97a96a4..af985e3 100644 --- a/src/BitzArt.LinqExtensions.Batching/Models/OffsetBatchingStrategy.cs +++ b/src/BitzArt.LinqExtensions.Batching/Models/OffsetBatchingStrategy.cs @@ -1,4 +1,4 @@ -namespace BitzArt.LinqExtensions.Batching; +namespace BitzArt; internal class OffsetBatchingStrategy(IBatchQueryBuilder builder, int offset) : BatchingStrategy(builder) diff --git a/src/BitzArt.LinqExtensions/BitzArt.LinqExtensions.csproj b/src/BitzArt.LinqExtensions/BitzArt.LinqExtensions.csproj index b07ba38..2d7c111 100644 --- a/src/BitzArt.LinqExtensions/BitzArt.LinqExtensions.csproj +++ b/src/BitzArt.LinqExtensions/BitzArt.LinqExtensions.csproj @@ -14,4 +14,8 @@ https://github.com/BitzArt/Miscellaneous + + + + diff --git a/src/BitzArt.LinqExtensions/Enums/FilterOperation.cs b/src/BitzArt.LinqExtensions/Enums/FilterOperation.cs deleted file mode 100644 index 20fdd21..0000000 --- a/src/BitzArt.LinqExtensions/Enums/FilterOperation.cs +++ /dev/null @@ -1,37 +0,0 @@ -namespace System.Linq; - -/// -/// Filter operation for comparing values. -/// -public enum FilterOperation : byte -{ - /// - /// Represents an equality condition. - /// - Equal = 0, - - /// - /// Represents an inequality condition. - /// - NotEqual = 1, - - /// - /// Represents a 'greater than' condition. - /// - GreaterThan = 2, - - /// - /// Represents a 'greater than or equal' condition. - /// - GreaterThanOrEqual = 3, - - /// - /// Represents a 'less than' condition. - /// - LessThan = 4, - - /// - /// Represents a 'less than or equal' condition. - /// - LessThanOrEqual = 5 -} diff --git a/src/BitzArt.LinqExtensions/Enums/OrderDirection.cs b/src/BitzArt.LinqExtensions/Enums/OrderDirection.cs deleted file mode 100644 index 38fd16d..0000000 --- a/src/BitzArt.LinqExtensions/Enums/OrderDirection.cs +++ /dev/null @@ -1,39 +0,0 @@ -namespace System.Linq; - -/// -/// Order direction (ascending/descending). -/// -public enum OrderDirection : byte -{ - /// - /// Ascending order direction. - /// - Ascending = 0, - - /// - /// Descending order direction. - /// - Descending = 1 -} - -/// -/// Extensions to . -/// -public static class OrderDirectionExtensions -{ - /// - /// Reverse the order direction. - /// - /// - /// - /// - public static OrderDirection Reverse(this OrderDirection orderDirection) - { - return orderDirection switch - { - OrderDirection.Ascending => OrderDirection.Descending, - OrderDirection.Descending => OrderDirection.Ascending, - _ => throw new ArgumentOutOfRangeException(nameof(orderDirection), orderDirection, null) - }; - } -} diff --git a/src/BitzArt.LinqExtensions/Extensions/AddFilterExtension.cs b/src/BitzArt.LinqExtensions/Extensions/AddFilterExtension.cs index 40fc621..2a99c7b 100644 --- a/src/BitzArt.LinqExtensions/Extensions/AddFilterExtension.cs +++ b/src/BitzArt.LinqExtensions/Extensions/AddFilterExtension.cs @@ -1,4 +1,5 @@ -using System.Linq.Expressions; +using BitzArt; +using System.Linq.Expressions; namespace System.Linq; @@ -10,18 +11,18 @@ public static class AddFilterExtension /// /// Adds a filter to the query if the filter value is not null. /// - public static IQueryable AddFilter(this IQueryable source, Expression> expression, TProperty? filter, FilterOperation filterOperation = FilterOperation.Equal) + public static IQueryable AddFilter(this IQueryable source, Expression> expression, TProperty? filter, ComparisonType comparisonType = ComparisonType.Equal) where TProperty : class { if (filter is null) return source; - return BuildExpression(source, filter, expression, filterOperation); + return BuildExpression(source, filter, expression, comparisonType); } /// /// Adds a filter to the query if the filter value is not null. /// - public static IQueryable AddFilter(this IQueryable source, Expression> expression, TProperty? filter, FilterOperation filterOperation = FilterOperation.Equal) + public static IQueryable AddFilter(this IQueryable source, Expression> expression, TProperty? filter, ComparisonType comparisonType = ComparisonType.Equal) where TProperty : struct { if (filter is null) return source; @@ -30,24 +31,24 @@ public static IQueryable AddFilter(this IQueryable< Expression> getValueExpression = x => x!.Value; var valueExpression = expression.Compose(getValueExpression); - return BuildExpression(source, filterValue, valueExpression, filterOperation); + return BuildExpression(source, filterValue, valueExpression, comparisonType); } - private static IQueryable BuildExpression(IQueryable source, TProperty filter, Expression> expression, FilterOperation filterOperation) + private static IQueryable BuildExpression(IQueryable source, TProperty filter, Expression> expression, ComparisonType comparisonType) { var argument = Expression.Parameter(typeof(TSource)); var left = Expression.Invoke(expression, argument); var right = Expression.Constant(filter); - var eq = filterOperation switch + var eq = comparisonType switch { - FilterOperation.Equal => Expression.Equal(left, right), - FilterOperation.NotEqual => Expression.NotEqual(left, right), - FilterOperation.GreaterThan => Expression.GreaterThan(left, right), - FilterOperation.GreaterThanOrEqual => Expression.GreaterThanOrEqual(left, right), - FilterOperation.LessThan => Expression.LessThan(left, right), - FilterOperation.LessThanOrEqual => Expression.LessThanOrEqual(left, right), - _ => throw new NotImplementedException($"Unsupported Filter Operation: '{filterOperation}'") + ComparisonType.Equal => Expression.Equal(left, right), + ComparisonType.NotEqual => Expression.NotEqual(left, right), + ComparisonType.GreaterThan => Expression.GreaterThan(left, right), + ComparisonType.GreaterThanOrEqual => Expression.GreaterThanOrEqual(left, right), + ComparisonType.LessThan => Expression.LessThan(left, right), + ComparisonType.LessThanOrEqual => Expression.LessThanOrEqual(left, right), + _ => throw new NotImplementedException($"Unsupported Comparison Type: '{comparisonType}'") }; var lambda = Expression diff --git a/src/BitzArt.LinqExtensions/Extensions/OrderExtensions.cs b/src/BitzArt.LinqExtensions/Extensions/OrderExtensions.cs index eb96755..ac34cb7 100644 --- a/src/BitzArt.LinqExtensions/Extensions/OrderExtensions.cs +++ b/src/BitzArt.LinqExtensions/Extensions/OrderExtensions.cs @@ -1,4 +1,5 @@ -using System.Linq.Expressions; +using BitzArt; +using System.Linq.Expressions; namespace System.Linq; diff --git a/tests/BitzArt.LinqExtensions.Batching.Tests/BatchTests.cs b/tests/BitzArt.LinqExtensions.Batching.Tests/BatchTests.cs index 807c173..a581714 100644 --- a/tests/BitzArt.LinqExtensions.Batching.Tests/BatchTests.cs +++ b/tests/BitzArt.LinqExtensions.Batching.Tests/BatchTests.cs @@ -1,6 +1,4 @@ -using BitzArt.LinqExtensions.Batching; - -namespace BitzArt.LinqExtensions.Tests; +namespace BitzArt.LinqExtensions.Batching.Tests; public class BatchTests { diff --git a/tests/BitzArt.LinqExtensions.Tests/AddFilterTests.cs b/tests/BitzArt.LinqExtensions.Tests/AddFilterTests.cs index 2cadf06..c74c30f 100644 --- a/tests/BitzArt.LinqExtensions.Tests/AddFilterTests.cs +++ b/tests/BitzArt.LinqExtensions.Tests/AddFilterTests.cs @@ -89,7 +89,7 @@ public void NotEqual_Filter_Filters(int notEqualTo) int? filter = notEqualTo; - var filtered = queryable.AddFilter(x => x.Id, filter, FilterOperation.NotEqual).ToList(); + var filtered = queryable.AddFilter(x => x.Id, filter, ComparisonType.NotEqual).ToList(); Assert.Equal(9, filtered.Count); @@ -111,7 +111,7 @@ public void GreaterThan_Filter_Filters() int? filter = 5; - var filtered = queryable.AddFilter(x => x.Id, filter, FilterOperation.GreaterThan).ToList(); + var filtered = queryable.AddFilter(x => x.Id, filter, ComparisonType.GreaterThan).ToList(); Assert.Equal(5, filtered.Count); Assert.True(filtered.First().Id == 6); @@ -127,7 +127,7 @@ public void GreaterThanOrEqual_Filter_Filters() int? filter = 5; - var filtered = queryable.AddFilter(x => x.Id, filter, FilterOperation.GreaterThanOrEqual).ToList(); + var filtered = queryable.AddFilter(x => x.Id, filter, ComparisonType.GreaterThanOrEqual).ToList(); Assert.Equal(6, filtered.Count); Assert.True(filtered.First().Id == 5); @@ -143,7 +143,7 @@ public void LessThan_Filter_Filters() int? filter = 5; - var filtered = queryable.AddFilter(x => x.Id, filter, FilterOperation.LessThan).ToList(); + var filtered = queryable.AddFilter(x => x.Id, filter, ComparisonType.LessThan).ToList(); Assert.Equal(4, filtered.Count); Assert.True(filtered.First().Id == 1); @@ -159,7 +159,7 @@ public void LessThanOrEqual_Filter_Filters() int? filter = 5; - var filtered = queryable.AddFilter(x => x.Id, filter, FilterOperation.LessThanOrEqual).ToList(); + var filtered = queryable.AddFilter(x => x.Id, filter, ComparisonType.LessThanOrEqual).ToList(); Assert.Equal(5, filtered.Count); Assert.True(filtered.First().Id == 1);