Skip to content

Commit

Permalink
Merge pull request #98 from netcorepal/refact-dto-data-objects
Browse files Browse the repository at this point in the history
break change: move PagedData、ResponseData to NetCorePal.Extensions.Dto
  • Loading branch information
witskeeper authored Nov 4, 2024
2 parents 400921b + 87c7758 commit 9769a7f
Show file tree
Hide file tree
Showing 23 changed files with 312 additions and 155 deletions.
46 changes: 30 additions & 16 deletions netcorepal-cloud-framework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -105,37 +105,41 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCorePal.Extensions.AspNe
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCorePal.Extensions.Repository.EntityFrameworkCore.Identity", "src\Repository.EntityFrameworkCore.Identity\NetCorePal.Extensions.Repository.EntityFrameworkCore.Identity.csproj", "{103B3995-2BBE-4B0B-99BB-2FB893C740C5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.Context.AspNetCore.UnitTests", "test\NetCorePal.Context.AspNetCore.UnitTests\NetCorePal.Context.AspNetCore.UnitTests.csproj", "{CD5644EF-3161-4782-ACA2-FB7C7B390B72}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCorePal.Context.AspNetCore.UnitTests", "test\NetCorePal.Context.AspNetCore.UnitTests\NetCorePal.Context.AspNetCore.UnitTests.csproj", "{CD5644EF-3161-4782-ACA2-FB7C7B390B72}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.Extensions.Snowflake.Consul", "src\Snowflake.Consul\NetCorePal.Extensions.Snowflake.Consul.csproj", "{4C082F87-DF2D-4900-9CFE-21D3E31D3FFE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCorePal.Extensions.Snowflake.Consul", "src\Snowflake.Consul\NetCorePal.Extensions.Snowflake.Consul.csproj", "{4C082F87-DF2D-4900-9CFE-21D3E31D3FFE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.Extensions.Snowflake.Consul.UnitTests", "test\NetCorePal.Extensions.Snowflake.Consul.UnitTests\NetCorePal.Extensions.Snowflake.Consul.UnitTests.csproj", "{B3FC2F02-DA8A-4F04-BD3F-CFBC4DEF6AB6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCorePal.Extensions.Snowflake.Consul.UnitTests", "test\NetCorePal.Extensions.Snowflake.Consul.UnitTests\NetCorePal.Extensions.Snowflake.Consul.UnitTests.csproj", "{B3FC2F02-DA8A-4F04-BD3F-CFBC4DEF6AB6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.Extensions.AspNetCore.UnitTests", "test\NetCorePal.Extensions.AspNetCore.UnitTests\NetCorePal.Extensions.AspNetCore.UnitTests.csproj", "{9F85B529-3052-4411-AA6B-5D41B3F20137}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCorePal.Extensions.AspNetCore.UnitTests", "test\NetCorePal.Extensions.AspNetCore.UnitTests\NetCorePal.Extensions.AspNetCore.UnitTests.csproj", "{9F85B529-3052-4411-AA6B-5D41B3F20137}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.Extensions.DistributedTransactions.CAP.UnitTests", "test\NetCorePal.Extensions.DistributedTransactions.CAP.UnitTests\NetCorePal.Extensions.DistributedTransactions.CAP.UnitTests.csproj", "{694F90EF-B149-4A2B-A4A1-F47279E291A3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCorePal.Extensions.DistributedTransactions.CAP.UnitTests", "test\NetCorePal.Extensions.DistributedTransactions.CAP.UnitTests\NetCorePal.Extensions.DistributedTransactions.CAP.UnitTests.csproj", "{694F90EF-B149-4A2B-A4A1-F47279E291A3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.Extensions.DistributedTransactions.UnitTests", "test\NetCorePal.Extensions.DistributedTransactions.UnitTests\NetCorePal.Extensions.DistributedTransactions.UnitTests.csproj", "{C4CBFDFF-8ECC-4E6B-B68B-9896770C7903}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCorePal.Extensions.DistributedTransactions.UnitTests", "test\NetCorePal.Extensions.DistributedTransactions.UnitTests\NetCorePal.Extensions.DistributedTransactions.UnitTests.csproj", "{C4CBFDFF-8ECC-4E6B-B68B-9896770C7903}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.Extensions.NewtonsoftJson", "src\NewtonsoftJson\NetCorePal.Extensions.NewtonsoftJson.csproj", "{C8612E52-2943-465B-A725-1337F45ED507}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCorePal.Extensions.NewtonsoftJson", "src\NewtonsoftJson\NetCorePal.Extensions.NewtonsoftJson.csproj", "{C8612E52-2943-465B-A725-1337F45ED507}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.Extensions.DistributedLocks.Abstractions", "src\DistributedLocks.Abstractions\NetCorePal.Extensions.DistributedLocks.Abstractions.csproj", "{ABD2CD15-0955-4F14-9735-73E656C4FEDC}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCorePal.Extensions.DistributedLocks.Abstractions", "src\DistributedLocks.Abstractions\NetCorePal.Extensions.DistributedLocks.Abstractions.csproj", "{ABD2CD15-0955-4F14-9735-73E656C4FEDC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.Extensions.DistributedLocks.Redis", "src\DistributedLocks.Redis\NetCorePal.Extensions.DistributedLocks.Redis.csproj", "{7BA698FA-5B2B-4C9D-A6F8-ECCBF8D49869}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCorePal.Extensions.DistributedLocks.Redis", "src\DistributedLocks.Redis\NetCorePal.Extensions.DistributedLocks.Redis.csproj", "{7BA698FA-5B2B-4C9D-A6F8-ECCBF8D49869}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.Extensions.MultiEnv.CAP.RabbitMQ", "src\MultiEnv.CAP.RabbitMQ\NetCorePal.Extensions.MultiEnv.CAP.RabbitMQ.csproj", "{0A5543FA-4C70-4A95-AD83-FEED18A00563}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCorePal.Extensions.MultiEnv.CAP.RabbitMQ", "src\MultiEnv.CAP.RabbitMQ\NetCorePal.Extensions.MultiEnv.CAP.RabbitMQ.csproj", "{0A5543FA-4C70-4A95-AD83-FEED18A00563}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.SkyApm.Diagnostics", "src\NetCorePal.SkyApm.Diagnostics\NetCorePal.SkyApm.Diagnostics.csproj", "{EF1C82DE-D48B-4FD3-BF02-7469E9E5A0E2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCorePal.SkyApm.Diagnostics", "src\NetCorePal.SkyApm.Diagnostics\NetCorePal.SkyApm.Diagnostics.csproj", "{EF1C82DE-D48B-4FD3-BF02-7469E9E5A0E2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.Extensions.Snowflake.Redis", "src\Snowflake.Redis\NetCorePal.Extensions.Snowflake.Redis.csproj", "{FA85B2AD-3891-4F9A-ACB5-545E1DC07519}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCorePal.Extensions.Snowflake.Redis", "src\Snowflake.Redis\NetCorePal.Extensions.Snowflake.Redis.csproj", "{FA85B2AD-3891-4F9A-ACB5-545E1DC07519}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.Extensions.Snowflake.Redis.UnitTests", "test\NetCorePal.Extensions.Snowflake.Redis.UnitTests\NetCorePal.Extensions.Snowflake.Redis.UnitTests.csproj", "{ED4401A4-0B4B-4FF5-AEF3-55D5700A13FB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCorePal.Extensions.Snowflake.Redis.UnitTests", "test\NetCorePal.Extensions.Snowflake.Redis.UnitTests\NetCorePal.Extensions.Snowflake.Redis.UnitTests.csproj", "{ED4401A4-0B4B-4FF5-AEF3-55D5700A13FB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.Extensions.Hangfire", "src\Hangfire\NetCorePal.Extensions.Hangfire.csproj", "{664491B2-2001-4D42-8029-4EF4B1CDBEE6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCorePal.Extensions.Hangfire", "src\Hangfire\NetCorePal.Extensions.Hangfire.csproj", "{664491B2-2001-4D42-8029-4EF4B1CDBEE6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.Extensions.DistributedTransactions.CAP.SqlServer", "src\DistributedTransactions.CAP.SqlServer\NetCorePal.Extensions.DistributedTransactions.CAP.SqlServer.csproj", "{C926EA21-8372-4494-87D2-05F6AF2CA44E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCorePal.Extensions.DistributedTransactions.CAP.SqlServer", "src\DistributedTransactions.CAP.SqlServer\NetCorePal.Extensions.DistributedTransactions.CAP.SqlServer.csproj", "{C926EA21-8372-4494-87D2-05F6AF2CA44E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.OpenTelemetry.Diagnostics", "src\NetCorePal.OpenTelemetry.Diagnostics\NetCorePal.OpenTelemetry.Diagnostics.csproj", "{59BB1378-ABEE-4A1E-BF14-7C807F83C8E3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCorePal.OpenTelemetry.Diagnostics", "src\NetCorePal.OpenTelemetry.Diagnostics\NetCorePal.OpenTelemetry.Diagnostics.csproj", "{59BB1378-ABEE-4A1E-BF14-7C807F83C8E3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.Extensions.Dto", "src\NetCorePal.Extensions.Dto\NetCorePal.Extensions.Dto.csproj", "{02772177-21D1-4CF8-8F13-FBE4F5DE0F22}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.Extensions.Dto.Tests", "test\NetCorePal.Extensions.Dto.Tests\NetCorePal.Extensions.Dto.Tests.csproj", "{57FCB1FF-0A79-4172-B208-4AD7FC04EF26}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -347,6 +351,14 @@ Global
{59BB1378-ABEE-4A1E-BF14-7C807F83C8E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{59BB1378-ABEE-4A1E-BF14-7C807F83C8E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{59BB1378-ABEE-4A1E-BF14-7C807F83C8E3}.Release|Any CPU.Build.0 = Release|Any CPU
{02772177-21D1-4CF8-8F13-FBE4F5DE0F22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{02772177-21D1-4CF8-8F13-FBE4F5DE0F22}.Debug|Any CPU.Build.0 = Debug|Any CPU
{02772177-21D1-4CF8-8F13-FBE4F5DE0F22}.Release|Any CPU.ActiveCfg = Release|Any CPU
{02772177-21D1-4CF8-8F13-FBE4F5DE0F22}.Release|Any CPU.Build.0 = Release|Any CPU
{57FCB1FF-0A79-4172-B208-4AD7FC04EF26}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{57FCB1FF-0A79-4172-B208-4AD7FC04EF26}.Debug|Any CPU.Build.0 = Debug|Any CPU
{57FCB1FF-0A79-4172-B208-4AD7FC04EF26}.Release|Any CPU.ActiveCfg = Release|Any CPU
{57FCB1FF-0A79-4172-B208-4AD7FC04EF26}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -408,6 +420,8 @@ Global
{664491B2-2001-4D42-8029-4EF4B1CDBEE6} = {605436C1-2560-47BA-99D4-7A80B628230F}
{C926EA21-8372-4494-87D2-05F6AF2CA44E} = {12EE5383-F550-4070-9362-B5C1000D3DF3}
{59BB1378-ABEE-4A1E-BF14-7C807F83C8E3} = {605436C1-2560-47BA-99D4-7A80B628230F}
{02772177-21D1-4CF8-8F13-FBE4F5DE0F22} = {605436C1-2560-47BA-99D4-7A80B628230F}
{57FCB1FF-0A79-4172-B208-4AD7FC04EF26} = {DB5A8331-2B57-4F2B-8F7D-02E167BCD8BB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D46201E5-1E3C-47A0-8584-4ACCC135CF9A}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net.Http.Json;
using Microsoft.Extensions.Logging;
using NetCorePal.Extensions.Dto;
using NetCorePal.Extensions.Primitives;

namespace NetCorePal.Extensions.AspNetCore.HttpClients;
Expand Down
3 changes: 1 addition & 2 deletions src/AspNetCore/KnownExceptionHandleMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NetCorePal.Extensions.Dto;
using NetCorePal.Extensions.Primitives;
using System.Globalization;

namespace NetCorePal.Extensions.AspNetCore
{
Expand Down
3 changes: 2 additions & 1 deletion src/AspNetCore/NetCorePal.Extensions.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
<PackageReference Include="FluentValidation.AspNetCore" />
<PackageReference Include="MediatR" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Domain.Abstractions\NetCorePal.Extensions.Domain.Abstractions.csproj" />
<ProjectReference Include="..\NetCorePal.Extensions.Dto\NetCorePal.Extensions.Dto.csproj" />
<ProjectReference Include="..\Primitives\NetCorePal.Extensions.Primitives.csproj" />
</ItemGroup>
</Project>
28 changes: 28 additions & 0 deletions src/AspNetCore/PageQueryableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore;
using NetCorePal.Extensions.Dto;

namespace NetCorePal.Extensions.AspNetCore;

public static class PageQueryableExtensions
{
public static async Task<PagedData<T>> ToPagedDataAsync<T>(
this IQueryable<T> query,
int? index,
int? size,
bool? countTotal,
CancellationToken cancellationToken)
{
var pageIndex = index ?? 1; // 默认取第1页
var pageSize = size ?? 10; // 默认每页10条

// isTotalNeeded为true时才查询总数。默认不需要总数
var totalCount = countTotal ?? false ? await query.CountAsync(cancellationToken) : 0;

var items = await query.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.ToListAsync(cancellationToken);

return new PagedData<T>(items, totalCount, pageIndex, pageSize);
}

}
46 changes: 0 additions & 46 deletions src/AspNetCore/Pagination/PageQueryableExtensions.cs

This file was deleted.

7 changes: 0 additions & 7 deletions src/AspNetCore/Pagination/PageRequest.cs

This file was deleted.

11 changes: 0 additions & 11 deletions src/AspNetCore/Pagination/PagedData.cs

This file was deleted.

36 changes: 0 additions & 36 deletions src/AspNetCore/ResponseData.cs

This file was deleted.

22 changes: 0 additions & 22 deletions src/AspNetCore/ResponseDataExtensions.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
40 changes: 40 additions & 0 deletions src/NetCorePal.Extensions.Dto/PageQueryableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace NetCorePal.Extensions.Dto;

public static class PageQueryableExtensions
{
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="query"></param>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="countTotal"></param>
/// <returns></returns>
public static PagedData<T> ToPagedData<T>(
this IQueryable<T> query,
int pageIndex = 1,
int pageSize = 10,
bool countTotal = false)
{
if (pageIndex <= 0)
{
throw new ArgumentOutOfRangeException(nameof(pageIndex), "页码必须大于 0");
}

if (pageSize <= 0)
{
throw new ArgumentOutOfRangeException(nameof(pageSize), "每页条数必须大于 0");
}


// isTotalNeeded为true时才查询总数。默认不需要总数
var totalCount = countTotal ? query.Count() : 0;

var items = query.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.ToList();

return new PagedData<T>(items, totalCount, pageIndex, pageSize);
}
}
16 changes: 16 additions & 0 deletions src/NetCorePal.Extensions.Dto/PageRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace NetCorePal.Extensions.Dto;

/// <summary>
/// 分页请求模型
/// </summary>
public class PageRequest
{
/// <summary>
/// 请求的页码,从1开始
/// </summary>
public int Index { get; set; }
/// <summary>
/// 请求的每页条数
/// </summary>
public int Size { get; set; }
}
39 changes: 39 additions & 0 deletions src/NetCorePal.Extensions.Dto/PagedData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace NetCorePal.Extensions.Dto;

/// <summary>
/// 分页数据模型
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
public class PagedData<T>
{
/// <summary>
/// 构造分页数据
/// </summary>
/// <param name="items">分页的数据</param>
/// <param name="total">总数据条数</param>
/// <param name="index">当前页码,从1开始</param>
/// <param name="size">每页条数</param>
public PagedData(IEnumerable<T> items, int total, int index, int size)
{
Items = items;
Total = total;
Index = index;
Size = size;
}
/// <summary>
/// 分页数据
/// </summary>
public IEnumerable<T> Items { get; private set; }
/// <summary>
/// 数据总数
/// </summary>
public int Total { get; private set; }
/// <summary>
/// 当前页码,从1开始
/// </summary>
public int Index { get; private set; }
/// <summary>
/// 每页数据条数
/// </summary>
public int Size { get; private set; }
}
Loading

0 comments on commit 9769a7f

Please sign in to comment.