-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87c7758
commit 93ee140
Showing
10 changed files
with
190 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using NetCorePal.Extensions.Dto; | ||
using NetCorePal.Extensions.Primitives; | ||
|
||
namespace NetCorePal.Extensions.AspNetCore | ||
{ | ||
public interface IPagedQuery<TResponse> : IQuery<PagedData<TResponse>> | ||
{ | ||
public int PageIndex { get; } | ||
public int PageSize { get; } | ||
public bool CountTotal { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using MediatR; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NetCorePal.Extensions.Primitives | ||
{ | ||
|
||
|
||
/// <summary> | ||
/// 表示一个类为查询类,框架则会自动注册该类查询类 | ||
/// </summary> | ||
public interface IQuery; | ||
|
||
|
||
/// <summary> | ||
/// 表示一个类为查询类,并指定查询结果类型,该Query由QueryHandler处理并相应 | ||
/// </summary> | ||
/// <typeparam name="TResponse"></typeparam> | ||
public interface IQuery<out TResponse> : IRequest<TResponse>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using MediatR; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NetCorePal.Extensions.Primitives; | ||
|
||
public interface IQueryHandler<in TQuery, TResponse> : IRequestHandler<TQuery, TResponse> | ||
where TQuery : IQuery<TResponse> | ||
{ | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
test/NetCorePal.Context.AspNetCore.UnitTests/ServiceCollectionExtensionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NetCorePal.Extensions.DependencyInjection; | ||
using NetCorePal.Extensions.Primitives; | ||
|
||
namespace NetCorePal.Context.AspNetCore.UnitTests | ||
{ | ||
|
||
public class Query1 : IQuery | ||
{ | ||
} | ||
|
||
|
||
public class ServiceCollectionExtensionTests | ||
{ | ||
[Fact] | ||
public void AddAllQueriesTests() | ||
{ | ||
var services = new ServiceCollection(); | ||
services.AddAllQueries(typeof(ServiceCollectionExtensionTests).Assembly); | ||
Assert.Equal(1, services.Count); | ||
Check warning on line 25 in test/NetCorePal.Context.AspNetCore.UnitTests/ServiceCollectionExtensionTests.cs GitHub Actions / build
|
||
var provider = services.BuildServiceProvider(); | ||
var queryHandler = provider.GetRequiredService<Query1>(); | ||
Assert.NotNull(queryHandler); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
test/NetCorePal.Web/Application/Queries/GetOrderByNameQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using NetCorePal.Extensions.Dto; | ||
using NetCorePal.Extensions.Primitives; | ||
|
||
namespace NetCorePal.Web.Application.Queries | ||
{ | ||
|
||
|
||
public record GetOrderByNameDto(OrderId OrderId, string Name); | ||
|
||
public record GetOrderByNameQuery(string Name, int PageIndex = 1, int PageSize = 10, bool CountTotal = false) : IPagedQuery<GetOrderByNameDto>; | ||
|
||
|
||
public class GetOrderByNameQueryHandler(ApplicationDbContext dbContext) : IQueryHandler<GetOrderByNameQuery, PagedData<GetOrderByNameDto>> | ||
{ | ||
public async Task<PagedData<GetOrderByNameDto>> Handle(GetOrderByNameQuery request, CancellationToken cancellationToken) | ||
{ | ||
return await dbContext.Orders.Where(x => x.Name.Contains(request.Name)) | ||
.Select(p => new GetOrderByNameDto(p.Id, p.Name)) | ||
.ToPagedDataAsync(request, cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters