Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified imgs/benchmarks.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions sandbox/AutoQuery.Benchmark/Benchmarks/QueryPerformance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public void AutoQuery_FilterSortSelectPage()
PageSize = 10
};

_autoQueryTestData.AsQueryable().ApplyQueryPaged(_queryProcessor, queryOptions).Datas.ToList();
_autoQueryTestData.AsQueryable().ApplyQueryPaged(_queryProcessor, queryOptions).ToList();
}

[Benchmark]
public void DynamicLinq_FilterSortSelectPageWith()
public void DynamicLinq_FilterSortSelectPage()
{
_dynamicLinqTestData.AsQueryable()
.Where("Name == @0", "Name5000")
Expand All @@ -71,7 +71,7 @@ public void DynamicLinq_FilterSortSelectPageWith()
}

[Benchmark]
public void AutoQuery_FilterSortPageWith()
public void AutoQuery_FilterSortPage()
{
var queryOptions = new TestQueryOptions
{
Expand All @@ -81,11 +81,11 @@ public void AutoQuery_FilterSortPageWith()
PageSize = 10
};

_autoQueryTestData.AsQueryable().ApplyQueryPaged(_queryProcessor, queryOptions).Datas.ToList();
_autoQueryTestData.AsQueryable().ApplyQueryPaged(_queryProcessor, queryOptions).ToList();
}

[Benchmark]
public void DynamicLinq_FilterSortPageWith()
public void DynamicLinq_FilterSortPage()
{
_dynamicLinqTestData.AsQueryable()
.Where("Name == @0", "Name5000")
Expand All @@ -96,7 +96,7 @@ public void DynamicLinq_FilterSortPageWith()
}

[Benchmark]
public void Sieve_FilterSortPageWith()
public void Sieve_FilterSortPage()
{
var sieveModel = new SieveModel
{
Expand Down
24 changes: 23 additions & 1 deletion src/AutoQuery/Extensions/QueryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,29 @@ public static IQueryable<TData> ApplyQuery<TData, TQueryOptions>(this IQueryable
/// <param name="queryProcessor">The query processor.</param>
/// <param name="queryOption">The query options.</param>
/// <returns>The query object with conditions and pagination options applied.</returns>
public static PagedResult<TData> ApplyQueryPaged<TData, TQueryOptions>(this IQueryable<TData> query, IQueryProcessor queryProcessor, TQueryOptions queryOption)
public static IQueryable<TData> ApplyQueryPaged<TData, TQueryOptions>(this IQueryable<TData> query, IQueryProcessor queryProcessor, TQueryOptions queryOption)
where TQueryOptions : IQueryPagedOptions
where TData : class
{
var filterExpression = queryProcessor.BuildFilterExpression<TData, TQueryOptions>(queryOption);
var selectorExpression = queryProcessor.BuildSelectorExpression<TData, TQueryOptions>(queryOption);
if (filterExpression != null)
query = query.Where(filterExpression);
if (selectorExpression != null)
query = query.Select(selectorExpression);
return query.ApplySort(queryOption).ApplyPaging(queryOption);
}

/// <summary>
/// Applies query conditions and pagination options.
/// </summary>
/// <typeparam name="TData">The type of the entity being queried.</typeparam>
/// <typeparam name="TQueryOptions">The type of the query options.</typeparam>
/// <param name="query">The query object.</param>
/// <param name="queryProcessor">The query processor.</param>
/// <param name="queryOption">The query options.</param>
/// <returns>The query object with conditions and pagination options applied.</returns>
public static PagedResult<TData> ApplyQueryPagedResult<TData, TQueryOptions>(this IQueryable<TData> query, IQueryProcessor queryProcessor, TQueryOptions queryOption)
where TQueryOptions : IQueryPagedOptions
where TData : class
{
Expand Down
81 changes: 77 additions & 4 deletions test/AutoQuery.Tests/Extensions/QueryExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void ApplyQuery_ShouldApplyFilterAndSelector(List<TestData> data, TestQue

[Theory]
[ClassData(typeof(ApplyQueryPagedTestData))]
public void ApplyQueryPaged_ShouldApplyFilterSelectorAndPaging(List<TestData> data, TestQueryPagedOptions queryOptions, Expression<Func<TestData, bool>> filterExpression, Expression<Func<TestData, TestData>> selectorExpression, int expectedCount, int totalCount, int totalPages)
public void ApplyQueryPaged_ShouldApplyFilterSelectorAndPaging(List<TestData> data, TestQueryPagedOptions queryOptions, Expression<Func<TestData, bool>> filterExpression, Expression<Func<TestData, TestData>> selectorExpression, int expectedCount, string expectedName)
{
// Arrange
var queryableData = data.AsQueryable();
Expand All @@ -50,10 +50,31 @@ public void ApplyQueryPaged_ShouldApplyFilterSelectorAndPaging(List<TestData> da
// Act
var result = queryableData.ApplyQueryPaged(_queryProcessorMock.Object, queryOptions);

// Assert
Assert.Equal(expectedCount, result.Count());
Assert.Equal(expectedName, result.First().Name);
}

[Theory]
[ClassData(typeof(ApplyQueryPagedResultTestData))]
public void ApplyQueryPagedResult_ShouldApplyFilterSelectorAndPaging(List<TestData> data, TestQueryPagedOptions queryOptions, Expression<Func<TestData, bool>> filterExpression, Expression<Func<TestData, TestData>> selectorExpression, int expectedCount, int totalCount, int totalPages, string expectedName)
{
// Arrange
var queryableData = data.AsQueryable();

_queryProcessorMock.Setup(x => x.BuildFilterExpression<TestData, TestQueryPagedOptions>(queryOptions))
.Returns(filterExpression);
_queryProcessorMock.Setup(x => x.BuildSelectorExpression<TestData, TestQueryPagedOptions>(queryOptions))
.Returns(selectorExpression);

// Act
var result = queryableData.ApplyQueryPagedResult(_queryProcessorMock.Object, queryOptions);

// Assert
Assert.Equal(expectedCount, result.Datas.Count());
Assert.Equal(totalCount, result.Count);
Assert.Equal(totalPages, result.TotalPages);
Assert.Equal(expectedName, result.Datas.First().Name);
}

[Theory]
Expand Down Expand Up @@ -168,8 +189,58 @@ public IEnumerator<object[]> GetEnumerator()
(Expression<Func<TestData, bool>>)(x => x.Id > 0),
(Expression<Func<TestData, TestData>>)(x => new TestData { Id = x.Id, Name = x.Name }),
1,
"Test1"
};
yield return new object[]
{
new List<TestData>
{
new TestData { Id = 3, Name = "Test3" },
new TestData { Id = 4, Name = "Test4" }
},
new TestQueryPagedOptions { Page = 1, PageSize = 2 },
(Expression<Func<TestData, bool>>)(x => x.Id > 2),
(Expression<Func<TestData, TestData>>)(x => new TestData { Id = x.Id, Name = x.Name }),
2,
2
"Test3"
};
yield return new object[]
{
new List<TestData>
{
new TestData { Id = 5, Name = "Test5" },
new TestData { Id = 6, Name = "Test6" },
new TestData { Id = 7, Name = "Test7" }
},
new TestQueryPagedOptions { Page = 2, PageSize = 2 },
(Expression<Func<TestData, bool>>)(x => x.Id > 4),
(Expression<Func<TestData, TestData>>)(x => new TestData { Id = x.Id, Name = x.Name }),
1,
"Test7"
};
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

public class ApplyQueryPagedResultTestData : IEnumerable<object[]>
{
public IEnumerator<object[]> GetEnumerator()
{
yield return new object[]
{
new List<TestData>
{
new TestData { Id = 1, Name = "Test1" },
new TestData { Id = 2, Name = "Test2" }
},
new TestQueryPagedOptions { Page = 1, PageSize = 1 },
(Expression<Func<TestData, bool>>)(x => x.Id > 0),
(Expression<Func<TestData, TestData>>)(x => new TestData { Id = x.Id, Name = x.Name }),
1,
2,
2,
"Test1"
};
yield return new object[]
{
Expand All @@ -183,7 +254,8 @@ public IEnumerator<object[]> GetEnumerator()
(Expression<Func<TestData, TestData>>)(x => new TestData { Id = x.Id, Name = x.Name }),
2,
2,
1
1,
"Test3"
};
yield return new object[]
{
Expand All @@ -198,7 +270,8 @@ public IEnumerator<object[]> GetEnumerator()
(Expression<Func<TestData, TestData>>)(x => new TestData { Id = x.Id, Name = x.Name }),
1,
3,
2
2,
"Test7"
};
}

Expand Down