Refactor/framework optimization from review#29
Merged
Conversation
…r management - Added LogRepository for handling log aggregate operations with EF Core. - Introduced UserRepository for user management with EF Core, including methods for finding, inserting, and updating users. - Created SqlSugar sharding interfaces and options for user-specific database access. - Developed UserHashSplitSqlSugarClientFactory and UserSpecificSqlSugarClientProvider for sharding support. - Established UserDemoDbContext and UserSharingDemoDbContext for SqlSugar database contexts. - Implemented base context and repository classes for SqlSugar integration. - Added serialization service for custom object handling in SqlSugar. - Created SqlSugarQueryableExtensions for applying specifications in queries. - Developed tests for verifying persistence registration and service configurations.
将 EFCore/SqlSugar 持久化实现项目从 src 目录迁移至 MS.Microservice.Persistence 子目录,新增独立解决方案和基础单元测试,调整所有引用路径,未涉及业务逻辑变更,便于后续维护与扩展。
新增 NetworkShareAccessor,支持以指定凭据临时访问 SMB 网络共享,自动处理凭据冲突与连接释放,底层基于 WNetUseConnection/WNetCancelConnection2。配套新增 xUnit 单元测试,覆盖参数校验、路径提取、连接流程、异常与边界场景。props 配置允许 unsafe 代码块。
There was a problem hiding this comment.
Pull request overview
This PR continues the “framework optimization” refactor by splitting persistence concerns out of MS.Microservice.Infrastructure into two new modules (MS.Microservice.Persistence.EFCore and MS.Microservice.Persistence.SqlSugar) while keeping services.AddInfrastructure(configuration) as a stable facade for the Web host. It also introduces a new NetworkShareAccessor helper for temporarily connecting to Windows network shares and adds/updates architecture + registration tests to enforce the new boundaries.
Changes:
- Split EF Core + SqlSugar persistence (DbContexts, repositories, options, query helpers, DI registration) into new
MS.Microservice.Persistence.*projects and update solution + references accordingly. - Keep
MS.Microservice.Infrastructureas a facade that forwards persistence registration to the new modules, and update Web startup and Autofac wiring to the new namespaces. - Add a
NetworkShareAccessorimplementation plus tests (note: current test content includes hard-coded credentials and environment-dependent behavior).
Reviewed changes
Copilot reviewed 76 out of 76 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| test/MS.Microservice.Infrastructure.Tests/MS.Microservice.Infrastructure.Tests.csproj | Add references to new persistence projects for Infrastructure test suite. |
| test/MS.Microservice.Infrastructure.Tests/FileSystem/NetworkShareAccessorTests.cs | Add tests for NetworkShareAccessor (currently includes hard-coded credentials / env coupling). |
| test/MS.Microservice.Infrastructure.Tests/DomainEvents/DomainEventDispatchTests.cs | Update ActivationDbContext namespace import to Persistence.EFCore. |
| test/MS.Microservice.Infrastructure.Tests/DependencyInjection/PersistenceRegistrationTests.cs | Add DI registration tests for EFCore, SqlSugar, and Infrastructure facade. |
| test/MS.Microservice.Core.Tests/MS.Microservice.Core.Tests.csproj | Add references to new persistence projects for architecture tests. |
| test/MS.Microservice.Core.Tests/Architecture/LayerDependencyTests.cs | Allow the two new Persistence assemblies in dependency checks. |
| test/MS.Microservice.Core.Tests/Architecture/ArchitectureTests.cs | Update infra assembly selection + add guard that Infrastructure no longer defines persistence implementation namespaces. |
| src/MS.Microservice.Web/Program.cs | Update ActivationDbContext import to Persistence.EFCore. |
| src/MS.Microservice.Web/MS.Microservice.Web.csproj | Add reference to Persistence.EFCore (Web uses EFCore types). |
| src/MS.Microservice.Web/Infrastructure/Extensions/IServiceCollectionExtensions.cs | Remove legacy SqlSugar DI namespace; comment points to new persistence registration method. |
| src/MS.Microservice.Web/Infrastructure/AutofacModules/DomainServiceModule.cs | Update repository implementation namespace to Persistence.EFCore. |
| src/MS.Microservice.Web/Infrastructure/ActivationDbContextSeed.cs | Update ActivationDbContext import to Persistence.EFCore. |
| src/MS.Microservice.Infrastructure/WolverineExtensions.cs | Update ActivationDbContext import to Persistence.EFCore. |
| src/MS.Microservice.Infrastructure/SqlSugar/SqlSugarSerializeService.cs | Removed (moved into Persistence.SqlSugar). |
| src/MS.Microservice.Infrastructure/SqlSugar/SqlSugarClientBuilderOptions.cs | Removed (moved into Persistence.SqlSugar). |
| src/MS.Microservice.Infrastructure/SqlSugar/Repository/UserDemoRepository.cs | Removed (moved into Persistence.SqlSugar). |
| src/MS.Microservice.Infrastructure/SqlSugar/Microsoft/Extension/DependencyInjection/SqlSugarServiceCollectionExtensions.cs | Removed legacy SqlSugar DI extensions (replaced by Persistence.SqlSugar registration). |
| src/MS.Microservice.Infrastructure/SqlSugar/Converters/ObjectJsonConverter.cs | Removed (moved into Persistence.SqlSugar). |
| src/MS.Microservice.Infrastructure/SqlSugar/Advance/Sharding/UserHashSplitSqlSugarClientFactory.cs | Removed (moved into Persistence.SqlSugar). |
| src/MS.Microservice.Infrastructure/SqlSugar/Advance/Sharding/ShardingServiceCollectionExtensions.cs | Removed (moved into Persistence.SqlSugar). |
| src/MS.Microservice.Infrastructure/SqlSugar/Advance/Sharding/ShardingOptions.cs | Removed (moved into Persistence.SqlSugar). |
| src/MS.Microservice.Infrastructure/SqlSugar/Advance/Sharding/IUserHashSplitSqlSugarClientFactory.cs | Removed (moved into Persistence.SqlSugar). |
| src/MS.Microservice.Infrastructure/SqlSugar/Advance/Sharding/IShardingAttribute.cs | Removed (moved into Persistence.SqlSugar). |
| src/MS.Microservice.Infrastructure/MS.Microservice.Infrastructure.csproj | Drop EFCore design/tools/relational + SqlSugar package refs; add project refs to new persistence projects. |
| src/MS.Microservice.Infrastructure/Microsoft/Extensions/DependencyInjection/InfrastructureServiceCollectionExtensions.cs | Switch persistence registration to call new persistence module extension methods; adjust registration order. |
| src/MS.Microservice.Infrastructure/FileSystem/NetworkShareAccessor.cs | Add network share connector using WNet APIs with retry/disconnect logic. |
| src/MS.Microservice.Infrastructure/DbContext/SqlSugar/UserSharingDemoDbContext.cs | Removed (moved into Persistence.SqlSugar). |
| src/MS.Microservice.Infrastructure/DbContext/SqlSugar/UserDemoDbContext.cs | Removed (moved into Persistence.SqlSugar). |
| src/MS.Microservice.Infrastructure/DbContext/SqlSugar/SqlSugarDbContext.cs | Removed (moved into Persistence.SqlSugar). |
| src/MS.Microservice.Infrastructure/DbContext/Microsoft/Extension/DependencyInjection/DbContextServiceCollectionExtensions.cs | Removed EFCore Npgsql registration from Infrastructure (moved into Persistence.EFCore). |
| MS.Microservice.slnx | Add new Persistence solution folders and projects. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/test/MS.Microservice.Persistence.SqlSugar.Tests/SqlSugarSerializeServiceTests.cs | Add tests for SqlSugar serializer service. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/test/MS.Microservice.Persistence.SqlSugar.Tests/SqlSugarPersistenceServiceCollectionExtensionsTests.cs | Add argument validation tests for SqlSugar persistence DI extensions. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/test/MS.Microservice.Persistence.SqlSugar.Tests/SqlSugarOptionsTests.cs | Add default-value tests for SqlSugar options/builder options. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/test/MS.Microservice.Persistence.SqlSugar.Tests/MS.Microservice.Persistence.SqlSugar.Tests.csproj | Add SqlSugar persistence test project. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/test/MS.Microservice.Persistence.SqlSugar.Tests/GlobalUsings.cs | Add global usings for SqlSugar persistence test project. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/SqlSugarSerializeService.cs | Add SqlSugar serializer implementation in new module. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/SqlSugarQueryableExtensions.cs | Update namespaces/references to Persistence.SqlSugar. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/SqlSugarOptions.cs | Move options type into Persistence.SqlSugar namespace. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/SqlSugarClientBuilderOptions.cs | Add builder options type in Persistence.SqlSugar. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/Specification/SqlSugarIncludeVisitor.cs | Move include visitor into Persistence.SqlSugar namespace. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/Repository/UserDemoRepository.cs | Add SqlSugar repository implementation in new module. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/MS.Microservice.Persistence.SqlSugar.csproj | Add SqlSugar persistence project. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/Microsoft/Extensions/DependencyInjection/SqlSugarPersistenceServiceCollectionExtensions.cs | Add SqlSugar persistence DI registration extensions. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/Microsoft/Extension/DependencyInjection/SqlSugarServiceCollectionCompatibilityExtensions.cs | Add compatibility AddSqlSugarService extension in legacy namespace. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/DbContext/UserSharingDemoDbContext.cs | Add sharded SqlSugar context in new module. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/DbContext/UserDemoDbContext.cs | Add SqlSugar demo context in new module. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/DbContext/SqlSugarDbContext.cs | Add generic SqlSugar repository base in new module. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/DbContext/BaseContext.cs | Move/retarget base context into Persistence.SqlSugar namespace and simplify comments. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/Converters/ObjectJsonConverter.cs | Add converter implementation in new module (contains a string-handling logic issue). |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/Advance/Sharding/UserSpecificSqlSugarClientProvider.cs | Move/retarget provider into Persistence.SqlSugar namespace. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/Advance/Sharding/UserHashSplitSqlSugarClientFactory.cs | Add sharding client factory in new module. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/Advance/Sharding/ShardingServiceCollectionExtensions.cs | Add sharding registration in new module. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/Advance/Sharding/ShardingOptions.cs | Add sharding options in new module. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/Advance/Sharding/IUserSpecificSqlSugarClientProvider.cs | Move/retarget interface namespace to Persistence.SqlSugar. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/Advance/Sharding/IUserHashSplitSqlSugarClientFactory.cs | Add sharding factory interface in new module. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/src/MS.Microservice.Persistence.SqlSugar/Advance/Sharding/IShardingAttribute.cs | Add marker interface in new module. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.SqlSugar/MS.Microservice.Persistence.SqlSugar.slnx | Add standalone solution descriptor for SqlSugar persistence module. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.EFCore/test/MS.Microservice.Persistence.EFCore.Tests/MsPlatformDbContextSettingsTests.cs | Add tests for EFCore DbContext settings defaults/behavior. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.EFCore/test/MS.Microservice.Persistence.EFCore.Tests/MS.Microservice.Persistence.EFCore.Tests.csproj | Add EFCore persistence test project. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.EFCore/test/MS.Microservice.Persistence.EFCore.Tests/GlobalUsings.cs | Add global usings for EFCore persistence test project. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.EFCore/test/MS.Microservice.Persistence.EFCore.Tests/EfCorePersistenceServiceCollectionExtensionsTests.cs | Add DI extension argument validation + missing connection string tests for EFCore persistence. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.EFCore/src/MS.Microservice.Persistence.EFCore/Repository/UserRepository.cs | Move repository into Persistence.EFCore namespace and update DbContext import. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.EFCore/src/MS.Microservice.Persistence.EFCore/Repository/LogRepository.cs | Move repository into Persistence.EFCore namespace and tidy using list. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.EFCore/src/MS.Microservice.Persistence.EFCore/MS.Microservice.Persistence.EFCore.csproj | Add EFCore persistence project and package ownership for EFCore tooling/design. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.EFCore/src/MS.Microservice.Persistence.EFCore/Microsoft/Extensions/DependencyInjection/EfCorePersistenceServiceCollectionExtensions.cs | Add EFCore persistence DI registration extensions + connection string validation. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.EFCore/src/MS.Microservice.Persistence.EFCore/EntityConfigurations/LogEntityTypeConfiguration.cs | Move entity config into Persistence.EFCore namespace. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.EFCore/src/MS.Microservice.Persistence.EFCore/EntityConfigurations/IdentityModelEntityTypeConfiguration.cs | Move identity model configs into Persistence.EFCore namespace. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.EFCore/src/MS.Microservice.Persistence.EFCore/EfCoreIncludeVisitor.cs | Move include visitor into Persistence.EFCore namespace. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.EFCore/src/MS.Microservice.Persistence.EFCore/DbContext/SoftDeleteQueryExtensions.cs | Move soft-delete query filter extensions into Persistence.EFCore namespace. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.EFCore/src/MS.Microservice.Persistence.EFCore/DbContext/MsPlatformDbContextSettings.cs | Move DbContext settings into Persistence.EFCore namespace. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.EFCore/src/MS.Microservice.Persistence.EFCore/DbContext/EFCoreQueryableExtensions.cs | Move EF Core specification application helpers into Persistence.EFCore namespace (remove incorrect NPOI using). |
| MS.Microservice.Persistence/MS.Microservice.Persistence.EFCore/src/MS.Microservice.Persistence.EFCore/DbContext/ActivationDbContext.cs | Move DbContext into Persistence.EFCore and decouple from Wolverine message bus construction. |
| MS.Microservice.Persistence/MS.Microservice.Persistence.EFCore/MS.Microservice.Persistence.EFCore.slnx | Add standalone solution descriptor for EFCore persistence module. |
| docs/framework-optimization-roadmap.md | Update roadmap to reflect completed persistence split and remaining TODOs. |
| Directory.Build.props | Enable global unsafe blocks (currently unnecessary). |
Comment on lines
+10
to
+12
| private const string TestSharePath = @"\\192.168.1.2\工作目录"; | ||
| private const string TestUserName = @"shuai.mao@kingsunsoft.com"; | ||
| private const string TestPassword = "Marsonshine123"; |
| // IsShareAlreadyAccessible 检查 | ||
| // ================================================================ | ||
|
|
||
| [Fact] |
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
| <AllowUnsafeBlocks>true</AllowUnsafeBlocks> |
Comment on lines
+12
to
+25
| public SugarParameter ParameterConverter<T>(object columnValue, int columnIndex) | ||
| { | ||
| string value; | ||
| if (columnValue == null) | ||
| return new SugarParameter("@" + columnIndex, null); | ||
|
|
||
| if (columnValue.GetType() == typeof(string)) | ||
| { | ||
| value = columnValue.ToString()!; | ||
| } | ||
|
|
||
| value = SerializeService.SerializeObject(columnValue); | ||
| return new SugarParameter("@" + columnIndex, value); | ||
| } |
Comment on lines
+38
to
+41
| () => new() | ||
| { | ||
| ConnectionString = configuration.GetConnectionString("Default") ?? string.Empty, | ||
| IsAutoCloseConnection = sqlSugarOptions.IsAutoCloseConnection, |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.