From 54bdf3ff0705075ba2c3aa8036a4835fe23e1b50 Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Fri, 15 Apr 2022 16:35:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E6=8F=90=E5=89=8D=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E6=8F=90=E7=A4=BA=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ValuesController.cs | 1 + .../DbContexts/DefaultShardingDbContext.cs | 1 + .../DbContexts/DefaultShardingDbContext1.cs | 26 ----------------- samples/Sample.SqlServer/Startup.cs | 2 +- .../Sample.SqlServer3x/DefaultDbContext.cs | 4 ++- .../ActivatorDbContextCreator.cs | 1 - .../Core/VirtualRoutes/RouteTailFactory.cs | 28 +++++++++++++++++-- .../Abstractions/IMultiQueryRouteTail.cs | 2 +- .../Abstractions/INoCacheRouteTail.cs | 2 +- .../Abstractions/IRouteTailFactory.cs | 9 +++++- .../EFCores/EFCore2x/ShardingModelSource.cs | 4 +++ .../EFCores/EFCore3x/ShardingModelSource.cs | 4 +++ .../EFCores/EFCore5x/ShardingModelSource.cs | 4 +++ .../EFCores/EFCore6x/ShardingModelSource.cs | 4 +++ .../Abstractions/IShardingTableDbContext.cs | 4 +++ 15 files changed, 62 insertions(+), 34 deletions(-) delete mode 100644 samples/Sample.SqlServer/DbContexts/DefaultShardingDbContext1.cs diff --git a/samples/Sample.SqlServer/Controllers/ValuesController.cs b/samples/Sample.SqlServer/Controllers/ValuesController.cs index ca0b30ca..55508ff6 100644 --- a/samples/Sample.SqlServer/Controllers/ValuesController.cs +++ b/samples/Sample.SqlServer/Controllers/ValuesController.cs @@ -38,6 +38,7 @@ namespace Sample.SqlServer.Controllers public ValuesController(DefaultShardingDbContext defaultTableDbContext, IShardingRouteManager shardingRouteManager) { _defaultTableDbContext = defaultTableDbContext; + _ = defaultTableDbContext.Model; _shardingRouteManager = shardingRouteManager; } diff --git a/samples/Sample.SqlServer/DbContexts/DefaultShardingDbContext.cs b/samples/Sample.SqlServer/DbContexts/DefaultShardingDbContext.cs index 0846bc8a..38089466 100644 --- a/samples/Sample.SqlServer/DbContexts/DefaultShardingDbContext.cs +++ b/samples/Sample.SqlServer/DbContexts/DefaultShardingDbContext.cs @@ -15,6 +15,7 @@ namespace Sample.SqlServer.DbContexts { public DefaultShardingDbContext(DbContextOptions options) : base(options) { + //Database.SetCommandTimeout(10000); } protected override void OnModelCreating(ModelBuilder modelBuilder) diff --git a/samples/Sample.SqlServer/DbContexts/DefaultShardingDbContext1.cs b/samples/Sample.SqlServer/DbContexts/DefaultShardingDbContext1.cs deleted file mode 100644 index 40c15dcb..00000000 --- a/samples/Sample.SqlServer/DbContexts/DefaultShardingDbContext1.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; -using Sample.SqlServer.Domain.Maps; -using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; -using ShardingCore.Sharding; -using ShardingCore.Sharding.Abstractions; - -namespace Sample.SqlServer.DbContexts -{ - public class DefaultShardingDbContext1:AbstractShardingDbContext, IShardingTableDbContext - { - public DefaultShardingDbContext1(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - base.OnModelCreating(modelBuilder); - } - - public IRouteTail RouteTail { get; set; } - } -} diff --git a/samples/Sample.SqlServer/Startup.cs b/samples/Sample.SqlServer/Startup.cs index d07cb2f1..a17d81c1 100644 --- a/samples/Sample.SqlServer/Startup.cs +++ b/samples/Sample.SqlServer/Startup.cs @@ -79,7 +79,7 @@ namespace Sample.SqlServer // }).End(); services.AddHealthChecks().AddDbContextCheck(); - services.Replace(ServiceDescriptor.Singleton, ActivatorDbContextCreator>()); + //services.Replace(ServiceDescriptor.Singleton, ActivatorDbContextCreator>()); //services.AddShardingDbContext( // o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;") // , op => diff --git a/samples/Sample.SqlServer3x/DefaultDbContext.cs b/samples/Sample.SqlServer3x/DefaultDbContext.cs index 3cf0081c..048abc3e 100644 --- a/samples/Sample.SqlServer3x/DefaultDbContext.cs +++ b/samples/Sample.SqlServer3x/DefaultDbContext.cs @@ -1,5 +1,6 @@ using System; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Internal; using Microsoft.Extensions.DependencyInjection; using Sample.SqlServer3x.Domain.Maps; using ShardingCore.Core.DbContextCreator; @@ -31,7 +32,7 @@ namespace Sample.SqlServer3x public DbContext CreateDbContext(DbContext mainDbContext, ShardingDbContextOptions shardingDbContextOptions) { var dbContext = new DefaultDbContext((DbContextOptions)shardingDbContextOptions.DbContextOptions,((DefaultDbContext)mainDbContext).ServiceProvider); - + Console.WriteLine("IsFrozen" + shardingDbContextOptions.DbContextOptions.IsFrozen); if (dbContext is IShardingTableDbContext shardingTableDbContext) { shardingTableDbContext.RouteTail = shardingDbContextOptions.RouteTail; @@ -50,6 +51,7 @@ namespace Sample.SqlServer3x { ServiceProvider = serviceProvider; _scopedService = serviceProvider.GetRequiredService(); + //Database.SetCommandTimeout(10000); Console.WriteLine("DefaultDbContext ctor"); } diff --git a/src/ShardingCore/Core/DbContextCreator/ActivatorDbContextCreator.cs b/src/ShardingCore/Core/DbContextCreator/ActivatorDbContextCreator.cs index 80cecb7a..48e29081 100644 --- a/src/ShardingCore/Core/DbContextCreator/ActivatorDbContextCreator.cs +++ b/src/ShardingCore/Core/DbContextCreator/ActivatorDbContextCreator.cs @@ -28,7 +28,6 @@ namespace ShardingCore.Core.DbContextCreator public DbContext CreateDbContext(DbContext mainDbContext, ShardingDbContextOptions shardingDbContextOptions) { var dbContext = _creator(shardingDbContextOptions); - if (dbContext is IShardingTableDbContext shardingTableDbContext) { shardingTableDbContext.RouteTail = shardingDbContextOptions.RouteTail; diff --git a/src/ShardingCore/Core/VirtualRoutes/RouteTailFactory.cs b/src/ShardingCore/Core/VirtualRoutes/RouteTailFactory.cs index 5d699e49..be188964 100644 --- a/src/ShardingCore/Core/VirtualRoutes/RouteTailFactory.cs +++ b/src/ShardingCore/Core/VirtualRoutes/RouteTailFactory.cs @@ -32,11 +32,35 @@ namespace ShardingCore.Core.VirtualRoutes } public IRouteTail Create(TableRouteResult tableRouteResult) + { + return Create(tableRouteResult,true); + } + + public IRouteTail Create(TableRouteResult tableRouteResult, bool cache) { if (tableRouteResult == null || tableRouteResult.ReplaceTables.IsEmpty()) - return new SingleQueryRouteTail(string.Empty); + { + if (cache) + { + return new SingleQueryRouteTail(string.Empty); + } + else + { + return new NoCacheSingleQueryRouteTail(string.Empty); + } + } + if (tableRouteResult.ReplaceTables.Count == 1) - return new SingleQueryRouteTail(tableRouteResult); + { + if (cache) + { + return new SingleQueryRouteTail(tableRouteResult); + } + else + { + return new NoCacheSingleQueryRouteTail(tableRouteResult); + } + } return new MultiQueryRouteTail(tableRouteResult); } } diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/Abstractions/IMultiQueryRouteTail.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/Abstractions/IMultiQueryRouteTail.cs index 1d1c29d7..73e887d6 100644 --- a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/Abstractions/IMultiQueryRouteTail.cs +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/Abstractions/IMultiQueryRouteTail.cs @@ -9,7 +9,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions * @Date: Sunday, 22 August 2021 09:44:54 * @Email: 326308290@qq.com */ - public interface IMultiQueryRouteTail: INoCacheRouteTail + public interface IMultiQueryRouteTail: IRouteTail, INoCacheRouteTail { /// /// 获取对象类型的应该后缀 diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/Abstractions/INoCacheRouteTail.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/Abstractions/INoCacheRouteTail.cs index dc1c50ad..9558037a 100644 --- a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/Abstractions/INoCacheRouteTail.cs +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/Abstractions/INoCacheRouteTail.cs @@ -12,7 +12,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions /// Author: xjm /// Created: 2022/4/15 13:22:07 /// Email: 326308290@qq.com - public interface INoCacheRouteTail:IRouteTail + public interface INoCacheRouteTail { } } diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/Abstractions/IRouteTailFactory.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/Abstractions/IRouteTailFactory.cs index bcfe5471..22a60637 100644 --- a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/Abstractions/IRouteTailFactory.cs +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/RouteTails/Abstractions/IRouteTailFactory.cs @@ -24,10 +24,17 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions /// IRouteTail Create(string tail, bool cache); /// - /// dbcontextģͲᱻ + /// ·Ĭϵᱻ /// /// /// IRouteTail Create(TableRouteResult tableRouteResult); + /// + /// ·ĬϵǷ·ɸcache϶ + /// + /// + /// + /// + IRouteTail Create(TableRouteResult tableRouteResult, bool cache); } } \ No newline at end of file diff --git a/src/ShardingCore/EFCores/EFCore2x/ShardingModelSource.cs b/src/ShardingCore/EFCores/EFCore2x/ShardingModelSource.cs index a6208891..b35699ff 100644 --- a/src/ShardingCore/EFCores/EFCore2x/ShardingModelSource.cs +++ b/src/ShardingCore/EFCores/EFCore2x/ShardingModelSource.cs @@ -45,6 +45,10 @@ namespace ShardingCore.EFCores { if (context is IShardingTableDbContext shardingTableDbContext) { + if (shardingTableDbContext.RouteTail is null) + { + throw new ShardingCoreInvalidOperationException("db context model is inited before RouteTail set value"); + } if (shardingTableDbContext.RouteTail is INoCacheRouteTail) { var multiModel = CreateModel(context, conventionSetBuilder, validator); diff --git a/src/ShardingCore/EFCores/EFCore3x/ShardingModelSource.cs b/src/ShardingCore/EFCores/EFCore3x/ShardingModelSource.cs index f7e018cf..bb3548e0 100644 --- a/src/ShardingCore/EFCores/EFCore3x/ShardingModelSource.cs +++ b/src/ShardingCore/EFCores/EFCore3x/ShardingModelSource.cs @@ -53,6 +53,10 @@ namespace ShardingCore.EFCores var waitSeconds = 3; if (context is IShardingTableDbContext shardingTableDbContext) { + if (shardingTableDbContext.RouteTail is null) + { + throw new ShardingCoreInvalidOperationException("db context model is inited before RouteTail set value"); + } if (shardingTableDbContext.RouteTail is INoCacheRouteTail) { var noCacheModel = CreateModel(context, conventionSetBuilder); diff --git a/src/ShardingCore/EFCores/EFCore5x/ShardingModelSource.cs b/src/ShardingCore/EFCores/EFCore5x/ShardingModelSource.cs index 0bd5d0be..1503c3a1 100644 --- a/src/ShardingCore/EFCores/EFCore5x/ShardingModelSource.cs +++ b/src/ShardingCore/EFCores/EFCore5x/ShardingModelSource.cs @@ -69,6 +69,10 @@ namespace ShardingCore.EFCores var waitSeconds = 3; if (context is IShardingTableDbContext shardingTableDbContext) { + if (shardingTableDbContext.RouteTail is null) + { + throw new ShardingCoreInvalidOperationException("db context model is inited before RouteTail set value"); + } if (shardingTableDbContext.RouteTail is INoCacheRouteTail) { var noCacheModel = CreateModel(context, conventionSetBuilder, modelDependencies); diff --git a/src/ShardingCore/EFCores/EFCore6x/ShardingModelSource.cs b/src/ShardingCore/EFCores/EFCore6x/ShardingModelSource.cs index c03774df..38ff71bc 100644 --- a/src/ShardingCore/EFCores/EFCore6x/ShardingModelSource.cs +++ b/src/ShardingCore/EFCores/EFCore6x/ShardingModelSource.cs @@ -83,6 +83,10 @@ namespace ShardingCore.EFCores var waitSeconds = 3; if (context is IShardingTableDbContext shardingTableDbContext) { + if (shardingTableDbContext.RouteTail is null) + { + throw new ShardingCoreInvalidOperationException("db context model is inited before RouteTail set value"); + } if (shardingTableDbContext.RouteTail is INoCacheRouteTail) { var noCacheModel = this.CreateModel(context, modelCreationDependencies.ConventionSetBuilder, modelCreationDependencies.ModelDependencies); diff --git a/src/ShardingCore/Sharding/Abstractions/IShardingTableDbContext.cs b/src/ShardingCore/Sharding/Abstractions/IShardingTableDbContext.cs index 5ec2f6cc..1f8e510b 100644 --- a/src/ShardingCore/Sharding/Abstractions/IShardingTableDbContext.cs +++ b/src/ShardingCore/Sharding/Abstractions/IShardingTableDbContext.cs @@ -11,6 +11,10 @@ namespace ShardingCore.Sharding.Abstractions */ public interface IShardingTableDbContext { + /// + /// 无需实现 + /// IRouteTail RouteTail { get; set; } } + }