From 58610106f2ec881a595f0a138990ddc86b601a82 Mon Sep 17 00:00:00 2001 From: 2017875139 <2017875139@qq.com> Date: Sun, 6 Mar 2022 17:47:03 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AE=B5=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=20=20Result=20=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=A0=B8=E5=BF=83=20FluentValidation=20Guids=20?= =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AE=B5=E6=8F=90=E4=BA=A4=20=20Result=20?= =?UTF-8?q?=E6=B8=85AutoMapper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .editorconfig | 4 ++ Easy.sln | 28 ++++++-- .../DDD/Application/Dtos/PagingRequest.cs | 38 +++++++++++ .../ServiceCollectionServiceExtensions.cs | 2 +- .../Easy/{DDD => }/Realization/ApiMapAuto.cs | 2 +- .../Realization/AppServiceActivator.cs | 2 +- .../AppServiceControllerFeatureProvider.cs | 2 +- .../Realization/ApplicationConvention.cs | 2 +- src/Easy.Core/Easy.Core.csproj | 10 +++ src/{Easy.Result => Easy.Core}/Easy/Check.cs | 2 +- .../Easy/Exceptions/BusinessException.cs} | 6 +- src/{Easy.Result => Easy.Core}/Easy/When.cs | 8 +-- src/Easy.DDD.Domain/Easy.DDD.Domain.csproj | 2 +- .../DDD/Domain/Repositories/Repository.cs | 2 - .../ServiceCollectionServiceExtensions.cs | 2 +- .../Realization/DependencyInterface.cs | 2 +- .../Realization/LazyServiceProvider.cs | 4 +- .../Easy.FluentValidation.csproj | 20 ++++++ .../Easy/Aop/FluentValidationRegister.cs | 4 +- .../ServiceCollectionServiceExtensions.cs | 17 +++++ src/Easy.Guids/Easy.Guids.csproj | 19 ++++++ .../Easy/Enums/SequentialGuidType.cs | 19 ++++++ src/Easy.Guids/Easy/Guids/IGuidGenerator.cs | 6 ++ .../Options/SequentialGuidGeneratorOptions.cs | 18 +++++ .../Realization/SequentialGuidGenerator.cs | 68 +++++++++++++++++++ src/Easy.Result/Common/AutoMapReadonly.cs | 24 ------- src/Easy.Result/Easy.Result.csproj | 6 +- src/Easy.Result/Easy/Aop/ApiResultProxy.cs | 56 +++++++++++++++ .../Easy/Aop/ErrApiResMiddleware.cs | 60 ---------------- .../Easy/{ => Consts}/ApiResultConsts.cs | 2 +- .../Easy/Extensions/ApiResultExtensions.cs | 45 ++---------- .../ApplicationBuilderExtensions.cs | 22 ------ .../ServiceCollectionServiceExtensions.cs | 17 +++++ src/Easy.Result/Easy/IEntities.cs | 65 ------------------ src/Easy.Result/Easy/IEntity.cs | 7 -- src/Easy.Result/Easy/IModel.cs | 8 --- src/Easy.Result/Easy/Result/ApiResult.cs | 19 ++++-- src/Easy.Result/Easy/Result/ApiResultPaged.cs | 11 +-- .../Easy/Result/ApiResultValues.cs | 7 +- src/Easy.Uow/Easy/Aop/UowProxy.cs | 9 +-- .../ServiceCollectionServiceExtensions.cs | 5 +- .../Easy/{Uow => Realization}/UnitOfWork.cs | 6 +- test/WebApplication/GraphDbContext.cs | 3 +- .../WebApplication}/PagingRequest.cs | 0 44 files changed, 375 insertions(+), 286 deletions(-) create mode 100644 .editorconfig create mode 100644 src/Easy.Api/Easy/DDD/Application/Dtos/PagingRequest.cs rename src/Easy.Api/Easy/{DDD => }/Realization/ApiMapAuto.cs (98%) rename src/Easy.Api/Easy/{DDD => }/Realization/AppServiceActivator.cs (95%) rename src/Easy.Api/Easy/{DDD => }/Realization/AppServiceControllerFeatureProvider.cs (93%) rename src/Easy.Api/Easy/{DDD => }/Realization/ApplicationConvention.cs (99%) create mode 100644 src/Easy.Core/Easy.Core.csproj rename src/{Easy.Result => Easy.Core}/Easy/Check.cs (98%) rename src/{Easy.Result/Easy/Exceptions/SvcException.cs => Easy.Core/Easy/Exceptions/BusinessException.cs} (61%) rename src/{Easy.Result => Easy.Core}/Easy/When.cs (58%) rename src/Easy.DI/Easy/{DI => }/Realization/DependencyInterface.cs (98%) rename src/Easy.DI/Easy/{DI => }/Realization/LazyServiceProvider.cs (97%) create mode 100644 src/Easy.FluentValidation/Easy.FluentValidation.csproj rename src/{Easy.Result => Easy.FluentValidation}/Easy/Aop/FluentValidationRegister.cs (94%) create mode 100644 src/Easy.FluentValidation/Easy/Extensions/ServiceCollectionServiceExtensions.cs create mode 100644 src/Easy.Guids/Easy.Guids.csproj create mode 100644 src/Easy.Guids/Easy/Enums/SequentialGuidType.cs create mode 100644 src/Easy.Guids/Easy/Guids/IGuidGenerator.cs create mode 100644 src/Easy.Guids/Easy/Options/SequentialGuidGeneratorOptions.cs create mode 100644 src/Easy.Guids/Easy/Realization/SequentialGuidGenerator.cs delete mode 100644 src/Easy.Result/Common/AutoMapReadonly.cs create mode 100644 src/Easy.Result/Easy/Aop/ApiResultProxy.cs delete mode 100644 src/Easy.Result/Easy/Aop/ErrApiResMiddleware.cs rename src/Easy.Result/Easy/{ => Consts}/ApiResultConsts.cs (91%) delete mode 100644 src/Easy.Result/Easy/Extensions/ApplicationBuilderExtensions.cs create mode 100644 src/Easy.Result/Easy/Extensions/ServiceCollectionServiceExtensions.cs delete mode 100644 src/Easy.Result/Easy/IEntities.cs delete mode 100644 src/Easy.Result/Easy/IEntity.cs delete mode 100644 src/Easy.Result/Easy/IModel.cs rename src/Easy.Uow/Easy/{Uow => Realization}/UnitOfWork.cs (90%) rename {src/Easy.Result/Easy => test/WebApplication}/PagingRequest.cs (100%) diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..74c4787 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +[*.cs] + +# CA1848: 使用 LoggerMessage 委托 +dotnet_diagnostic.CA1848.severity = none diff --git a/Easy.sln b/Easy.sln index b9a55d4..fee1c0e 100644 --- a/Easy.sln +++ b/Easy.sln @@ -15,12 +15,16 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Easy.Uow", "src\Easy.Uow\Ea EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Easy.Specifications", "src\Easy.Specifications\Easy.Specifications.csproj", "{9C37245E-66EE-4F3A-8713-7B59379422FE}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApplication", "test\WebApplication\WebApplication.csproj", "{4383A8FF-5E4F-4772-B86F-29264AFB511B}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Easy.DDD", "Easy.DDD", "{A263D7E3-0153-48D0-822D-E3E0B51A4890}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Easy", "Easy", "{E2F1B3BA-DD92-4729-BDD0-91BB936520C0}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Easy.Guids", "src\Easy.Guids\Easy.Guids.csproj", "{1B91E50D-7E98-4B94-9BCA-76BB0423FB97}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Easy.Core", "src\Easy.Core\Easy.Core.csproj", "{EF3CD7D3-DC58-4A36-BB6D-BB87EDAE7408}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Easy.FluentValidation", "src\Easy.FluentValidation\Easy.FluentValidation.csproj", "{8EC50AAD-EB45-402A-B474-A94EDBD342A3}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -51,10 +55,18 @@ Global {9C37245E-66EE-4F3A-8713-7B59379422FE}.Debug|Any CPU.Build.0 = Debug|Any CPU {9C37245E-66EE-4F3A-8713-7B59379422FE}.Release|Any CPU.ActiveCfg = Release|Any CPU {9C37245E-66EE-4F3A-8713-7B59379422FE}.Release|Any CPU.Build.0 = Release|Any CPU - {4383A8FF-5E4F-4772-B86F-29264AFB511B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {4383A8FF-5E4F-4772-B86F-29264AFB511B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {4383A8FF-5E4F-4772-B86F-29264AFB511B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {4383A8FF-5E4F-4772-B86F-29264AFB511B}.Release|Any CPU.Build.0 = Release|Any CPU + {1B91E50D-7E98-4B94-9BCA-76BB0423FB97}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1B91E50D-7E98-4B94-9BCA-76BB0423FB97}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1B91E50D-7E98-4B94-9BCA-76BB0423FB97}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1B91E50D-7E98-4B94-9BCA-76BB0423FB97}.Release|Any CPU.Build.0 = Release|Any CPU + {EF3CD7D3-DC58-4A36-BB6D-BB87EDAE7408}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EF3CD7D3-DC58-4A36-BB6D-BB87EDAE7408}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EF3CD7D3-DC58-4A36-BB6D-BB87EDAE7408}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EF3CD7D3-DC58-4A36-BB6D-BB87EDAE7408}.Release|Any CPU.Build.0 = Release|Any CPU + {8EC50AAD-EB45-402A-B474-A94EDBD342A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8EC50AAD-EB45-402A-B474-A94EDBD342A3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8EC50AAD-EB45-402A-B474-A94EDBD342A3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8EC50AAD-EB45-402A-B474-A94EDBD342A3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -62,9 +74,13 @@ Global GlobalSection(NestedProjects) = preSolution {689367DF-1216-40A5-91CE-412EE8FE6C8F} = {A263D7E3-0153-48D0-822D-E3E0B51A4890} {A269FF3D-00BC-499D-A496-46D97D51FE92} = {E2F1B3BA-DD92-4729-BDD0-91BB936520C0} + {82677F0B-E9CF-4491-8B04-9BF9B04B1534} = {E2F1B3BA-DD92-4729-BDD0-91BB936520C0} {8004C9B6-FF61-44F1-B4CC-F4CF1C8045BF} = {A263D7E3-0153-48D0-822D-E3E0B51A4890} {A1121475-1403-4133-8F5E-9C5AE020F347} = {E2F1B3BA-DD92-4729-BDD0-91BB936520C0} {9C37245E-66EE-4F3A-8713-7B59379422FE} = {E2F1B3BA-DD92-4729-BDD0-91BB936520C0} + {1B91E50D-7E98-4B94-9BCA-76BB0423FB97} = {E2F1B3BA-DD92-4729-BDD0-91BB936520C0} + {EF3CD7D3-DC58-4A36-BB6D-BB87EDAE7408} = {E2F1B3BA-DD92-4729-BDD0-91BB936520C0} + {8EC50AAD-EB45-402A-B474-A94EDBD342A3} = {E2F1B3BA-DD92-4729-BDD0-91BB936520C0} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {7260112B-0232-4725-AB84-EF4DCCABFD77} diff --git a/src/Easy.Api/Easy/DDD/Application/Dtos/PagingRequest.cs b/src/Easy.Api/Easy/DDD/Application/Dtos/PagingRequest.cs new file mode 100644 index 0000000..0c9725d --- /dev/null +++ b/src/Easy.Api/Easy/DDD/Application/Dtos/PagingRequest.cs @@ -0,0 +1,38 @@ +namespace Easy.Wapper; + +public record PagingRequest +{ + private int _pageIndex; + private int _pageSize; + + /// + /// 当前第几页 + /// + public int PageIndex + { + get => _pageIndex; + set + { + if (value <= 0) value = 1; + _pageIndex = value; + } + } + + /// + /// 页面数量 + /// + public int PageSize + { + get => _pageSize; + set + { + if (value <= 9) value = 10; + _pageSize = value; + } + } + + /// + /// 默认是降序 + /// + public bool OrderByDescending { get; set; } = true; +} diff --git a/src/Easy.Api/Easy/Extensions/ServiceCollectionServiceExtensions.cs b/src/Easy.Api/Easy/Extensions/ServiceCollectionServiceExtensions.cs index ae7d69e..7cc4d92 100644 --- a/src/Easy.Api/Easy/Extensions/ServiceCollectionServiceExtensions.cs +++ b/src/Easy.Api/Easy/Extensions/ServiceCollectionServiceExtensions.cs @@ -1,5 +1,5 @@ using Easy.DDD.Application; -using Easy.DDD.Realization; +using Easy.Realization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.Extensions.DependencyInjection; diff --git a/src/Easy.Api/Easy/DDD/Realization/ApiMapAuto.cs b/src/Easy.Api/Easy/Realization/ApiMapAuto.cs similarity index 98% rename from src/Easy.Api/Easy/DDD/Realization/ApiMapAuto.cs rename to src/Easy.Api/Easy/Realization/ApiMapAuto.cs index 360c3ce..5336572 100644 --- a/src/Easy.Api/Easy/DDD/Realization/ApiMapAuto.cs +++ b/src/Easy.Api/Easy/Realization/ApiMapAuto.cs @@ -1,4 +1,4 @@ -namespace Easy.DDD.Realization; +namespace Easy.Realization; public class ApiMapAuto { diff --git a/src/Easy.Api/Easy/DDD/Realization/AppServiceActivator.cs b/src/Easy.Api/Easy/Realization/AppServiceActivator.cs similarity index 95% rename from src/Easy.Api/Easy/DDD/Realization/AppServiceActivator.cs rename to src/Easy.Api/Easy/Realization/AppServiceActivator.cs index 2ffb984..744d93a 100644 --- a/src/Easy.Api/Easy/DDD/Realization/AppServiceActivator.cs +++ b/src/Easy.Api/Easy/Realization/AppServiceActivator.cs @@ -2,7 +2,7 @@ using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.Extensions.DependencyInjection; -namespace Easy.DDD.Realization; +namespace Easy.Realization; internal class AppServiceActivator : IControllerActivator { diff --git a/src/Easy.Api/Easy/DDD/Realization/AppServiceControllerFeatureProvider.cs b/src/Easy.Api/Easy/Realization/AppServiceControllerFeatureProvider.cs similarity index 93% rename from src/Easy.Api/Easy/DDD/Realization/AppServiceControllerFeatureProvider.cs rename to src/Easy.Api/Easy/Realization/AppServiceControllerFeatureProvider.cs index 5b26bbb..6da12f0 100644 --- a/src/Easy.Api/Easy/DDD/Realization/AppServiceControllerFeatureProvider.cs +++ b/src/Easy.Api/Easy/Realization/AppServiceControllerFeatureProvider.cs @@ -2,7 +2,7 @@ using Microsoft.AspNetCore.Mvc.Controllers; using System.Reflection; -namespace Easy.DDD.Realization; +namespace Easy.Realization; internal class AppServiceControllerFeatureProvider : ControllerFeatureProvider { diff --git a/src/Easy.Api/Easy/DDD/Realization/ApplicationConvention.cs b/src/Easy.Api/Easy/Realization/ApplicationConvention.cs similarity index 99% rename from src/Easy.Api/Easy/DDD/Realization/ApplicationConvention.cs rename to src/Easy.Api/Easy/Realization/ApplicationConvention.cs index a1e5320..fe5622d 100644 --- a/src/Easy.Api/Easy/DDD/Realization/ApplicationConvention.cs +++ b/src/Easy.Api/Easy/Realization/ApplicationConvention.cs @@ -5,7 +5,7 @@ using Microsoft.AspNetCore.Mvc.ApplicationModels; using Microsoft.AspNetCore.Mvc.ModelBinding; using System.Reflection; -namespace Easy.DDD.Realization; +namespace Easy.Realization; internal class ApplicationConvention : IApplicationModelConvention { diff --git a/src/Easy.Core/Easy.Core.csproj b/src/Easy.Core/Easy.Core.csproj new file mode 100644 index 0000000..845194f --- /dev/null +++ b/src/Easy.Core/Easy.Core.csproj @@ -0,0 +1,10 @@ + + + + net6.0 + enable + + + + + diff --git a/src/Easy.Result/Easy/Check.cs b/src/Easy.Core/Easy/Check.cs similarity index 98% rename from src/Easy.Result/Easy/Check.cs rename to src/Easy.Core/Easy/Check.cs index 5088ad0..3f8af22 100644 --- a/src/Easy.Result/Easy/Check.cs +++ b/src/Easy.Core/Easy/Check.cs @@ -1,6 +1,6 @@ using System.Diagnostics.CodeAnalysis; -namespace Easy.Result; +namespace Easy; public static class Check { diff --git a/src/Easy.Result/Easy/Exceptions/SvcException.cs b/src/Easy.Core/Easy/Exceptions/BusinessException.cs similarity index 61% rename from src/Easy.Result/Easy/Exceptions/SvcException.cs rename to src/Easy.Core/Easy/Exceptions/BusinessException.cs index 5850d5b..e90f068 100644 --- a/src/Easy.Result/Easy/Exceptions/SvcException.cs +++ b/src/Easy.Core/Easy/Exceptions/BusinessException.cs @@ -1,15 +1,15 @@ -namespace Easy.Result.Exceptions; +namespace Easy.Exceptions; /// /// 服务异常 /// -internal class SvcException : Exception +public class BusinessException : Exception { /// /// 服务异常 /// /// 消息 - public SvcException(string message) + public BusinessException(string message) : base(message) { diff --git a/src/Easy.Result/Easy/When.cs b/src/Easy.Core/Easy/When.cs similarity index 58% rename from src/Easy.Result/Easy/When.cs rename to src/Easy.Core/Easy/When.cs index 0f08639..6ad2799 100644 --- a/src/Easy.Result/Easy/When.cs +++ b/src/Easy.Core/Easy/When.cs @@ -1,6 +1,6 @@ -using Easy.Result.Exceptions; +using Easy.Exceptions; -namespace Easy.Result; +namespace Easy; /// /// when 语句 @@ -12,9 +12,9 @@ public class When /// /// 是否 /// 消息 - /// 服务异常会被中间件拦截 + /// 服务异常会被中间件拦截 public static void Is(bool isOk, string message) { - if (isOk) throw new SvcException(message); + if (isOk) throw new BusinessException(message); } } diff --git a/src/Easy.DDD.Domain/Easy.DDD.Domain.csproj b/src/Easy.DDD.Domain/Easy.DDD.Domain.csproj index 14ea61b..4e05e41 100644 --- a/src/Easy.DDD.Domain/Easy.DDD.Domain.csproj +++ b/src/Easy.DDD.Domain/Easy.DDD.Domain.csproj @@ -8,7 +8,7 @@ - + diff --git a/src/Easy.DDD.Domain/Easy/DDD/Domain/Repositories/Repository.cs b/src/Easy.DDD.Domain/Easy/DDD/Domain/Repositories/Repository.cs index de7f03a..1590122 100644 --- a/src/Easy.DDD.Domain/Easy/DDD/Domain/Repositories/Repository.cs +++ b/src/Easy.DDD.Domain/Easy/DDD/Domain/Repositories/Repository.cs @@ -1,7 +1,5 @@ using Easy.DDD.Domain.Entities; -using EFCore.BulkExtensions; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.ChangeTracking; using System.Linq.Expressions; namespace Easy.DDD.Domain.Repositories; diff --git a/src/Easy.DI/Easy/Extensions/ServiceCollectionServiceExtensions.cs b/src/Easy.DI/Easy/Extensions/ServiceCollectionServiceExtensions.cs index efb2afc..174f8c2 100644 --- a/src/Easy.DI/Easy/Extensions/ServiceCollectionServiceExtensions.cs +++ b/src/Easy.DI/Easy/Extensions/ServiceCollectionServiceExtensions.cs @@ -1,4 +1,4 @@ -using Easy.DI.Realization; +using Easy.Realization; using Microsoft.Extensions.DependencyInjection; namespace Easy.Extensions; diff --git a/src/Easy.DI/Easy/DI/Realization/DependencyInterface.cs b/src/Easy.DI/Easy/Realization/DependencyInterface.cs similarity index 98% rename from src/Easy.DI/Easy/DI/Realization/DependencyInterface.cs rename to src/Easy.DI/Easy/Realization/DependencyInterface.cs index 3c99c76..3bf5e53 100644 --- a/src/Easy.DI/Easy/DI/Realization/DependencyInterface.cs +++ b/src/Easy.DI/Easy/Realization/DependencyInterface.cs @@ -2,7 +2,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; -namespace Easy.DI.Realization; +namespace Easy.Realization; internal static class DependencyInterface { diff --git a/src/Easy.DI/Easy/DI/Realization/LazyServiceProvider.cs b/src/Easy.DI/Easy/Realization/LazyServiceProvider.cs similarity index 97% rename from src/Easy.DI/Easy/DI/Realization/LazyServiceProvider.cs rename to src/Easy.DI/Easy/Realization/LazyServiceProvider.cs index 8f3acee..57416e1 100644 --- a/src/Easy.DI/Easy/DI/Realization/LazyServiceProvider.cs +++ b/src/Easy.DI/Easy/Realization/LazyServiceProvider.cs @@ -1,4 +1,6 @@ -namespace Easy.DI.Realization; +using Easy.DI; + +namespace Easy.Realization; public class LazyServiceProvider : ILazyServiceProvider, ITransientDependency { diff --git a/src/Easy.FluentValidation/Easy.FluentValidation.csproj b/src/Easy.FluentValidation/Easy.FluentValidation.csproj new file mode 100644 index 0000000..cd07ea1 --- /dev/null +++ b/src/Easy.FluentValidation/Easy.FluentValidation.csproj @@ -0,0 +1,20 @@ + + + + net6.0 + enable + + + + + + + + + + + + + + + diff --git a/src/Easy.Result/Easy/Aop/FluentValidationRegister.cs b/src/Easy.FluentValidation/Easy/Aop/FluentValidationRegister.cs similarity index 94% rename from src/Easy.Result/Easy/Aop/FluentValidationRegister.cs rename to src/Easy.FluentValidation/Easy/Aop/FluentValidationRegister.cs index 3252163..cb04a75 100644 --- a/src/Easy.Result/Easy/Aop/FluentValidationRegister.cs +++ b/src/Easy.FluentValidation/Easy/Aop/FluentValidationRegister.cs @@ -2,9 +2,9 @@ using FluentValidation.Internal; using Microsoft.AspNetCore.Mvc.Filters; -namespace Easy.Result.FluentValidation; +namespace Easy.Aop; -public class FluentValidationFilter : IAsyncActionFilter +public class FluentValidationRegister : IAsyncActionFilter { public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { diff --git a/src/Easy.FluentValidation/Easy/Extensions/ServiceCollectionServiceExtensions.cs b/src/Easy.FluentValidation/Easy/Extensions/ServiceCollectionServiceExtensions.cs new file mode 100644 index 0000000..1905d8c --- /dev/null +++ b/src/Easy.FluentValidation/Easy/Extensions/ServiceCollectionServiceExtensions.cs @@ -0,0 +1,17 @@ +using Easy.Aop; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.DependencyInjection; + +namespace Easy.Extensions; +public static class ServiceCollectionServiceExtensions +{ + public static IServiceCollection AddUow(this IServiceCollection services) + { + services.Configure(options => + { + options.Filters.Add(); + }); + return services; + } + +} diff --git a/src/Easy.Guids/Easy.Guids.csproj b/src/Easy.Guids/Easy.Guids.csproj new file mode 100644 index 0000000..54753d9 --- /dev/null +++ b/src/Easy.Guids/Easy.Guids.csproj @@ -0,0 +1,19 @@ + + + + net6.0 + enable + + + + + + + + + + + + + + diff --git a/src/Easy.Guids/Easy/Enums/SequentialGuidType.cs b/src/Easy.Guids/Easy/Enums/SequentialGuidType.cs new file mode 100644 index 0000000..30afa20 --- /dev/null +++ b/src/Easy.Guids/Easy/Enums/SequentialGuidType.cs @@ -0,0 +1,19 @@ +namespace Easy.Enums; + +public enum SequentialGuidType +{ + /// + /// MySql and PostgreSql. + /// + SequentialAsString, + + /// + /// Oracle. + /// + SequentialAsBinary, + + /// + /// SqlServer. + /// + SequentialAtEnd +} diff --git a/src/Easy.Guids/Easy/Guids/IGuidGenerator.cs b/src/Easy.Guids/Easy/Guids/IGuidGenerator.cs new file mode 100644 index 0000000..5615f04 --- /dev/null +++ b/src/Easy.Guids/Easy/Guids/IGuidGenerator.cs @@ -0,0 +1,6 @@ +namespace Easy.Guids; + +public interface IGuidGenerator +{ + Guid Create(); +} diff --git a/src/Easy.Guids/Easy/Options/SequentialGuidGeneratorOptions.cs b/src/Easy.Guids/Easy/Options/SequentialGuidGeneratorOptions.cs new file mode 100644 index 0000000..306ea6f --- /dev/null +++ b/src/Easy.Guids/Easy/Options/SequentialGuidGeneratorOptions.cs @@ -0,0 +1,18 @@ +using Easy.Enums; + +namespace Easy.Options; + +public class SequentialGuidGeneratorOptions +{ + public SequentialGuidType? SequentialGuidType { get; set; } + + public SequentialGuidType GetSequentialGuidType() + { + if (SequentialGuidType == null) + { + throw new ArgumentNullException(nameof(SequentialGuidType), + "not Configure(options =>{ options.SequentialGuidType = ???; })"); + } + return SequentialGuidType.Value; + } +} diff --git a/src/Easy.Guids/Easy/Realization/SequentialGuidGenerator.cs b/src/Easy.Guids/Easy/Realization/SequentialGuidGenerator.cs new file mode 100644 index 0000000..595d7b8 --- /dev/null +++ b/src/Easy.Guids/Easy/Realization/SequentialGuidGenerator.cs @@ -0,0 +1,68 @@ +using Easy.DI; +using Easy.Enums; +using Easy.Guids; +using Easy.Options; +using Microsoft.Extensions.Options; +using System.Security.Cryptography; + +namespace Easy.Realization; + +public class SequentialGuidGenerator : IGuidGenerator, ITransientDependency +{ + public SequentialGuidGeneratorOptions Options { get; } + + private static readonly RandomNumberGenerator RandomNumberGenerator = RandomNumberGenerator.Create(); + + public SequentialGuidGenerator(IOptions options) + { + Options = options.Value; + } + + + public Guid Create() + { + SequentialGuidType guidType = Options.GetSequentialGuidType(); + + var randomBytes = new byte[10]; + RandomNumberGenerator.GetBytes(randomBytes); + + long timestamp = DateTime.UtcNow.Ticks / 10000L; + + byte[] timestampBytes = BitConverter.GetBytes(timestamp); + + + if (BitConverter.IsLittleEndian) + { + Array.Reverse(timestampBytes); + } + + byte[] guidBytes = new byte[16]; + + switch (guidType) + { + case SequentialGuidType.SequentialAsString: + + case SequentialGuidType.SequentialAsBinary: + + Buffer.BlockCopy(timestampBytes, 2, guidBytes, 0, 6); + Buffer.BlockCopy(randomBytes, 0, guidBytes, 6, 10); + + if (guidType == SequentialGuidType.SequentialAsString && BitConverter.IsLittleEndian) + { + Array.Reverse(guidBytes, 0, 4); + Array.Reverse(guidBytes, 4, 2); + } + + break; + + case SequentialGuidType.SequentialAtEnd: + + Buffer.BlockCopy(randomBytes, 0, guidBytes, 0, 10); + Buffer.BlockCopy(timestampBytes, 2, guidBytes, 10, 6); + + break; + } + + return new Guid(guidBytes); + } +} diff --git a/src/Easy.Result/Common/AutoMapReadonly.cs b/src/Easy.Result/Common/AutoMapReadonly.cs deleted file mode 100644 index 89b0f07..0000000 --- a/src/Easy.Result/Common/AutoMapReadonly.cs +++ /dev/null @@ -1,24 +0,0 @@ -using AutoMapper; -using Microsoft.Extensions.DependencyModel; -using System.Reflection; -using System.Runtime.Loader; - -namespace Easy.Result.Common; - -public class AutoMapReadonly -{ - private static IEnumerable Assemblies = DependencyContext.Default.CompileLibraries. - Where(lib => !lib.Serviceable && lib.Type != "package" && lib.Type != "referenceassembly"). - Select(lib => AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(lib.Name))); - - public readonly static IConfigurationProvider ConfigurationProvider = new MapperConfiguration(cfg => - { - foreach (var assembly in Assemblies) - { - foreach (var profileType in assembly.GetTypes().Where(o => typeof(Profile).IsAssignableFrom(o))) - { - cfg.AddProfile(profileType); - } - } - }); -} diff --git a/src/Easy.Result/Easy.Result.csproj b/src/Easy.Result/Easy.Result.csproj index 47405fa..ba90af6 100644 --- a/src/Easy.Result/Easy.Result.csproj +++ b/src/Easy.Result/Easy.Result.csproj @@ -6,15 +6,13 @@ True 1.0.2 + - - - - + diff --git a/src/Easy.Result/Easy/Aop/ApiResultProxy.cs b/src/Easy.Result/Easy/Aop/ApiResultProxy.cs new file mode 100644 index 0000000..1dd0a67 --- /dev/null +++ b/src/Easy.Result/Easy/Aop/ApiResultProxy.cs @@ -0,0 +1,56 @@ +using Easy.Exceptions; +using Easy.Result; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.Extensions.Logging; + +namespace Easy.Aop; + +public class ApiResultProxy : IAsyncActionFilter +{ + private readonly ILoggerFactory LoggerFactory; + public ApiResultProxy(ILoggerFactory loggerFactory) + { + LoggerFactory = loggerFactory; + } + public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) + { + var response = context.HttpContext.Response; + var logger = LoggerFactory.CreateLogger(context.Controller.GetType().Name); + try + { + await next(); + } + catch (BusinessException ex) + { + await response.WriteAsJsonAsync(new ApiResult() + { + Message = ex.Message, + Status = "RETRY", + }); + } + catch (ArgumentNullException ex) + { + const string message = $"参数不能为空"; + logger.LogError(ex, message); + + await response.WriteAsJsonAsync(new ApiResult() + { + Message = message, + Status = "RETRY", + }); + } + catch (Exception ex) + { + const string message = $"出错了,请稍后再试。"; + logger.LogError(ex, message); + + await response.WriteAsJsonAsync(new ApiResult() + { + Message = message, + Status = "RETRY", + }); + } + + } +} \ No newline at end of file diff --git a/src/Easy.Result/Easy/Aop/ErrApiResMiddleware.cs b/src/Easy.Result/Easy/Aop/ErrApiResMiddleware.cs deleted file mode 100644 index 7aa4cb6..0000000 --- a/src/Easy.Result/Easy/Aop/ErrApiResMiddleware.cs +++ /dev/null @@ -1,60 +0,0 @@ -using Easy.Result.Exceptions; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.Extensions.Logging; - -namespace Easy.Result.Middlewares; - -public class ErrApiResultMiddleware -{ - private readonly RequestDelegate _next; - - public ErrApiResultMiddleware(RequestDelegate next) - { - _next = next; - } - public async Task InvokeAsync(HttpContext context, ILoggerFactory loggerFactory) - { - if (context.Request.Path.Value.StartsWith("/api")) - { - var logger = loggerFactory.CreateLogger(nameof(ErrApiResultMiddleware)); - try - { - await _next(context); - } - catch (SvcException ex) - { - await context.Response.WriteAsJsonAsync(new ApiResult() - { - Message = ex.Message, - Status = "RETRY", - }); - } - catch (ArgumentNullException ex) - { - string message = $"ParamName:{ex.ParamName} is not null!message:{ex.Message}"; - await context.Response.WriteAsJsonAsync(new ApiResult() - { - Message = message, - Status = "RETRY", - }); - logger.LogError(new EventId(ex.HResult), ex, message); - } - catch (Exception ex) - { - string message = $"ErrMessage = {ex.Message}"; - logger.LogError(new EventId(ex.HResult), ex, message); - await context.Response.WriteAsJsonAsync(new ApiResult() - { - Message = "出错了,请稍后再试。", - Status = "RETRY", - }); - } - } - else - { - await _next(context); - } - - } -} diff --git a/src/Easy.Result/Easy/ApiResultConsts.cs b/src/Easy.Result/Easy/Consts/ApiResultConsts.cs similarity index 91% rename from src/Easy.Result/Easy/ApiResultConsts.cs rename to src/Easy.Result/Easy/Consts/ApiResultConsts.cs index 49a7111..b4c9498 100644 --- a/src/Easy.Result/Easy/ApiResultConsts.cs +++ b/src/Easy.Result/Easy/Consts/ApiResultConsts.cs @@ -1,4 +1,4 @@ -namespace Easy.Result; +namespace Easy.Consts; public class ApiResultConsts { diff --git a/src/Easy.Result/Easy/Extensions/ApiResultExtensions.cs b/src/Easy.Result/Easy/Extensions/ApiResultExtensions.cs index 73db32c..a6552e5 100644 --- a/src/Easy.Result/Easy/Extensions/ApiResultExtensions.cs +++ b/src/Easy.Result/Easy/Extensions/ApiResultExtensions.cs @@ -1,7 +1,7 @@ -using AutoMapper; -using Easy.Result.Common; +using Easy.Consts; +using Easy.Result; -namespace Easy.Result.Extensions; +namespace Easy.Extensions; public static class ApiResultExtensions { @@ -11,15 +11,7 @@ public static class ApiResultExtensions static public bool IsSuccess(this TSource source) where TSource : ApiResult { - return source.Status.Equals(ApiResultConsts.SUCCESS); - } - /// - /// 是重试 - /// - static public bool IsRETRY(this TSource source) - where TSource : ApiResult - { - return source.Status.Equals(ApiResultConsts.RETRY); + return source?.Status?.Equals(ApiResultConsts.SUCCESS) ?? false; } /// /// 成功 @@ -48,33 +40,4 @@ public static class ApiResultExtensions return source; } - - static public TEntity ToEntity(this IModel model) - where TEntity : IEntity, new() - { - return new Mapper(AutoMapReadonly.ConfigurationProvider).Map(model); - } - static public List ToEntities(this IModel models) - where TEntity : IEntity, new() - { - return new Mapper(AutoMapReadonly.ConfigurationProvider).Map>(models); - } - static public ApiResultValue ToModel(this IEntity entity) - where TModel : IModel, new() - { - return ApiResult.Value(new Mapper(AutoMapReadonly.ConfigurationProvider).Map(entity) ?? new()); - } - static public ApiResultValues ToModels(this IEntities entities) - { - return ApiResult.Value(new Mapper(AutoMapReadonly.ConfigurationProvider).Map>(entities.Data) ?? new()); - } - static public ApiResultPaged ToPagedModel(this IPaged paged) - { - return new ApiResultPaged() - { - Result = new Mapper(AutoMapReadonly.ConfigurationProvider).Map>(paged.Data) ?? new(), - PageSize = paged.PageSize, - PageCount = paged.PageCount - }; - } } \ No newline at end of file diff --git a/src/Easy.Result/Easy/Extensions/ApplicationBuilderExtensions.cs b/src/Easy.Result/Easy/Extensions/ApplicationBuilderExtensions.cs deleted file mode 100644 index 479db05..0000000 --- a/src/Easy.Result/Easy/Extensions/ApplicationBuilderExtensions.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Easy.Result.FluentValidation; -using Easy.Result.Middlewares; -using Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.DependencyInjection; - -namespace Easy.Result.Extensions; - -public static class ApplicationBuilderExtensions -{ - public static IApplicationBuilder UseErrApiResult(this IApplicationBuilder builder) - { - return builder.UseMiddleware(); - } - public static IMvcBuilder AddEasyFluentValidation(this IMvcBuilder builder) - { - return builder.AddMvcOptions(options => - { - options.Filters.Add(); - }); - } -} - diff --git a/src/Easy.Result/Easy/Extensions/ServiceCollectionServiceExtensions.cs b/src/Easy.Result/Easy/Extensions/ServiceCollectionServiceExtensions.cs new file mode 100644 index 0000000..d6fa131 --- /dev/null +++ b/src/Easy.Result/Easy/Extensions/ServiceCollectionServiceExtensions.cs @@ -0,0 +1,17 @@ +using Easy.Aop; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.DependencyInjection; + +namespace Easy.Extensions; +public static class ServiceCollectionServiceExtensions +{ + public static IServiceCollection AddApiResultProxy(this IServiceCollection services) + { + services.Configure(options => + { + options.Filters.Add(); + }); + return services; + } + +} diff --git a/src/Easy.Result/Easy/IEntities.cs b/src/Easy.Result/Easy/IEntities.cs deleted file mode 100644 index 39bd746..0000000 --- a/src/Easy.Result/Easy/IEntities.cs +++ /dev/null @@ -1,65 +0,0 @@ -namespace Easy.Result; - -public interface IEntities -{ - /// - /// 返回结果 - /// - public object Data { get; set; } - - - public static IEntities Create(List Data) - { - return new Entites() - { - Data = Data ?? new() - }; - } -} - -public sealed class Entites : IEntities -{ - /// - /// 返回结果 - /// - public object Data { get; set; } -} - -public interface IPaged : IEntities -{ - /// - /// 页面数量 - /// - public long PageCount { get; set; } - /// - /// 页面大小 - /// - public int PageSize { get; set; } - - public static IPaged Create(long PageCount, int PageSize, List Data) - { - return new Paged() - { - PageCount = PageCount, - Data = Data ?? new(), - PageSize = PageSize, - }; - } - -} - -public sealed class Paged : IPaged -{ - /// - /// 页面数量 - /// - public long PageCount { get; set; } - /// - /// 页面大小 - /// - public int PageSize { get; set; } - /// - /// 返回结果 - /// - public object Data { get; set; } -} \ No newline at end of file diff --git a/src/Easy.Result/Easy/IEntity.cs b/src/Easy.Result/Easy/IEntity.cs deleted file mode 100644 index e4a407d..0000000 --- a/src/Easy.Result/Easy/IEntity.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Easy.Result; - -public interface IEntity -{ - public long Id { get; set; } - -} diff --git a/src/Easy.Result/Easy/IModel.cs b/src/Easy.Result/Easy/IModel.cs deleted file mode 100644 index 7705e50..0000000 --- a/src/Easy.Result/Easy/IModel.cs +++ /dev/null @@ -1,8 +0,0 @@ -using AutoMapper; -using Easy.Result.Common; -namespace Easy.Result; - -public interface IModel -{ - -} diff --git a/src/Easy.Result/Easy/Result/ApiResult.cs b/src/Easy.Result/Easy/Result/ApiResult.cs index 5fa4adb..29df4dd 100644 --- a/src/Easy.Result/Easy/Result/ApiResult.cs +++ b/src/Easy.Result/Easy/Result/ApiResult.cs @@ -1,11 +1,11 @@ -using Easy.Result.Extensions; +using Easy.Extensions; namespace Easy.Result; /// /// ApiResult /// -public class ApiResult : IModel +public class ApiResult { public ApiResult() { } /// @@ -21,17 +21,26 @@ public class ApiResult : IModel { return Value(value).SUCCESS(); } - public static ApiResultValues ValueSuccess(List value) + public static ApiResultValues ValuesSuccess(List value) { - return Value(value).SUCCESS(); + return Values(value).SUCCESS(); + } + public static ApiResultPaged ValuesPagedSuccess(List value) + { + return ValuePaged(value).SUCCESS(); } + public static ApiResultValue Value(TValue value) { return new ApiResultValue(value); } - public static ApiResultValues Value(List value) + public static ApiResultValues Values(List value) { return new ApiResultValues(value); } + public static ApiResultPaged ValuePaged(List value) + { + return new ApiResultPaged(value); + } } diff --git a/src/Easy.Result/Easy/Result/ApiResultPaged.cs b/src/Easy.Result/Easy/Result/ApiResultPaged.cs index 9efcfac..6cd18e4 100644 --- a/src/Easy.Result/Easy/Result/ApiResultPaged.cs +++ b/src/Easy.Result/Easy/Result/ApiResultPaged.cs @@ -1,8 +1,12 @@ namespace Easy.Result; -public class ApiResultPaged : ApiResult +public class ApiResultPaged : ApiResultValues { public ApiResultPaged() { } + public ApiResultPaged(List value) + { + Results = value; + } /// /// 页面数量 /// @@ -11,8 +15,5 @@ public class ApiResultPaged : ApiResult /// 页面大小 /// public int PageSize { get; set; } - /// - /// 返回结果 - /// - public List Result { get; set; } + } \ No newline at end of file diff --git a/src/Easy.Result/Easy/Result/ApiResultValues.cs b/src/Easy.Result/Easy/Result/ApiResultValues.cs index d45d49c..6f7c2b7 100644 --- a/src/Easy.Result/Easy/Result/ApiResultValues.cs +++ b/src/Easy.Result/Easy/Result/ApiResultValues.cs @@ -5,7 +5,10 @@ public class ApiResultValues : ApiResult public ApiResultValues() { } public ApiResultValues(List value) { - Result = value; + Results = value; } - public List Result { get; set; } + /// + /// 返回结果 + /// + public List Results { get; set; } } diff --git a/src/Easy.Uow/Easy/Aop/UowProxy.cs b/src/Easy.Uow/Easy/Aop/UowProxy.cs index 0991a24..d19e68c 100644 --- a/src/Easy.Uow/Easy/Aop/UowProxy.cs +++ b/src/Easy.Uow/Easy/Aop/UowProxy.cs @@ -1,12 +1,5 @@ using Easy.Uow; using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.Extensions.DependencyInjection; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; namespace Easy.Aop; @@ -33,7 +26,7 @@ public class UowProxy : IAsyncActionFilter { await UnitOfWork.RollbackAsync(); } - catch + catch { throw ex; } diff --git a/src/Easy.Uow/Easy/Extensions/ServiceCollectionServiceExtensions.cs b/src/Easy.Uow/Easy/Extensions/ServiceCollectionServiceExtensions.cs index 4f11d3a..14af40c 100644 --- a/src/Easy.Uow/Easy/Extensions/ServiceCollectionServiceExtensions.cs +++ b/src/Easy.Uow/Easy/Extensions/ServiceCollectionServiceExtensions.cs @@ -1,16 +1,15 @@ using Easy.Aop; +using Easy.Realization; using Easy.Uow; using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; namespace Easy.Extensions; public static class ServiceCollectionServiceExtensions { - /// public static IServiceCollection AddUow(this IServiceCollection services) { - services.AddScoped(o => new UnitOfWork(o.GetService())); + services.AddScoped(); services.Configure(options => { options.Filters.Add(); diff --git a/src/Easy.Uow/Easy/Uow/UnitOfWork.cs b/src/Easy.Uow/Easy/Realization/UnitOfWork.cs similarity index 90% rename from src/Easy.Uow/Easy/Uow/UnitOfWork.cs rename to src/Easy.Uow/Easy/Realization/UnitOfWork.cs index 10498b2..05a4c49 100644 --- a/src/Easy.Uow/Easy/Uow/UnitOfWork.cs +++ b/src/Easy.Uow/Easy/Realization/UnitOfWork.cs @@ -1,7 +1,8 @@ -using Microsoft.EntityFrameworkCore; +using Easy.Uow; +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Storage; -namespace Easy.Uow; +namespace Easy.Realization; public class UnitOfWork : IAsyncDisposable, IUnitOfWork { @@ -36,5 +37,6 @@ public class UnitOfWork : IAsyncDisposable, IUnitOfWork { await Transaction.DisposeAsync(); await DbContext.DisposeAsync(); + GC.SuppressFinalize(this); } } \ No newline at end of file diff --git a/test/WebApplication/GraphDbContext.cs b/test/WebApplication/GraphDbContext.cs index 89992d0..d813572 100644 --- a/test/WebApplication/GraphDbContext.cs +++ b/test/WebApplication/GraphDbContext.cs @@ -1,5 +1,4 @@ using Microsoft.EntityFrameworkCore; -using System.Diagnostics.CodeAnalysis; namespace EFCore.BulkExtensions.Tests.ShadowProperties { @@ -13,7 +12,7 @@ namespace EFCore.BulkExtensions.Tests.ShadowProperties { base.OnModelCreating(modelBuilder); - + } } diff --git a/src/Easy.Result/Easy/PagingRequest.cs b/test/WebApplication/PagingRequest.cs similarity index 100% rename from src/Easy.Result/Easy/PagingRequest.cs rename to test/WebApplication/PagingRequest.cs