diff --git a/nuget-publish.bat b/nuget-publish.bat index b19f6d90..b32ca65b 100644 --- a/nuget-publish.bat +++ b/nuget-publish.bat @@ -1,8 +1,8 @@ :start ::定义版本 -set EFCORE2=2.2.0.15 -set EFCORE3=3.2.0.15 -set EFCORE5=5.2.0.15 +set EFCORE2=2.2.0.16 +set EFCORE3=3.2.0.16 +set EFCORE5=5.2.0.16 ::删除所有bin与obj下的文件 @echo off diff --git a/src/ShardingCore/Core/ShardingAccessors/Abstractions/IShardingAccessor.cs b/src/ShardingCore/Core/ShardingAccessors/Abstractions/IShardingAccessor.cs deleted file mode 100644 index 60755302..00000000 --- a/src/ShardingCore/Core/ShardingAccessors/Abstractions/IShardingAccessor.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace ShardingCore.Core.ShardingAccessors.Abstractions -{ -/* -* @Author: xjm -* @Description: -* @Date: Tuesday, 22 December 2020 15:13:44 -* @Email: 326308290@qq.com -*/ - public interface IShardingAccessor - { - ShardingContext ShardingContext { get; set; } - } -} \ No newline at end of file diff --git a/src/ShardingCore/Core/ShardingAccessors/Abstractions/IShardingScopeFactory.cs b/src/ShardingCore/Core/ShardingAccessors/Abstractions/IShardingScopeFactory.cs deleted file mode 100644 index 2f45b8a8..00000000 --- a/src/ShardingCore/Core/ShardingAccessors/Abstractions/IShardingScopeFactory.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace ShardingCore.Core.ShardingAccessors -{ -/* -* @Author: xjm -* @Description: -* @Date: Wednesday, 23 December 2020 07:51:00 -* @Email: 326308290@qq.com -*/ - /// - /// 查询scope创建 - /// - public interface IShardingScopeFactory - { - /// - /// 创建查询scope - /// - /// - ShardingScope CreateScope(); - } -} \ No newline at end of file diff --git a/src/ShardingCore/Core/ShardingAccessors/ShardingAccessor.cs b/src/ShardingCore/Core/ShardingAccessors/ShardingAccessor.cs deleted file mode 100644 index bd38a13a..00000000 --- a/src/ShardingCore/Core/ShardingAccessors/ShardingAccessor.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System.Threading; -using ShardingCore.Core.ShardingAccessors.Abstractions; -using ShardingCore.Core.VirtualTables; - -namespace ShardingCore.Core.ShardingAccessors -{ -/* -* @Author: xjm -* @Description: -* @Date: Tuesday, 22 December 2020 15:14:15 -* @Email: 326308290@qq.com -*/ - /// - /// 分表访问器 - /// - public class ShardingAccessor : IShardingAccessor - { - private static AsyncLocal _shardingContext = new AsyncLocal(); - - /// - /// 分表访问器 - /// - public ShardingAccessor(IVirtualTableManager virtualTableManager) - { - VirtualTableManager = virtualTableManager; - } - - /// - public ShardingContext ShardingContext - { - get => _shardingContext.Value; - set => _shardingContext.Value = value; - } - - /// - /// 虚拟表管理者 - /// - public IVirtualTableManager VirtualTableManager { get; } - } -} \ No newline at end of file diff --git a/src/ShardingCore/Core/ShardingAccessors/ShardingContext.cs b/src/ShardingCore/Core/ShardingAccessors/ShardingContext.cs deleted file mode 100644 index a71cb2b2..00000000 --- a/src/ShardingCore/Core/ShardingAccessors/ShardingContext.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System; -using System.Collections.Generic; -using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine; -using ShardingCore.Core.VirtualTables; -using ShardingCore.Extensions; - -namespace ShardingCore.Core.ShardingAccessors -{ -/* -* @Author: xjm -* @Description: -* @Date: Tuesday, 22 December 2020 15:04:47 -* @Email: 326308290@qq.com -*/ - public class ShardingContext - { - private ShardingContext(RouteResult routeResult) - { - foreach (var physicTable in routeResult.ReplaceTables) - { - _shardingTables.Add(physicTable.EntityType, physicTable.Tail); - } - } - - /// - /// 分表操作上下文 key:物理表名 value:虚拟表和本次分表tails - /// - private readonly Dictionary _shardingTables = new Dictionary(); - - - /// - /// 创建一个分表上下文 - /// - /// - public static ShardingContext Create(RouteResult routeResult) - { - return new ShardingContext(routeResult); - } - - /// - /// 获取分表信息 - /// - /// - /// - public string GetContextQueryTail(Type entityType) - { - if (_shardingTables.ContainsKey(entityType)) - return _shardingTables[entityType]; - return null; - } - - /// - /// 是否是空的 - /// - /// - public bool IsEmpty() - { - return _shardingTables.IsEmpty(); - } - } - -} \ No newline at end of file diff --git a/src/ShardingCore/Core/ShardingAccessors/ShardingScope.cs b/src/ShardingCore/Core/ShardingAccessors/ShardingScope.cs deleted file mode 100644 index 7e9dd732..00000000 --- a/src/ShardingCore/Core/ShardingAccessors/ShardingScope.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using ShardingCore.Core.ShardingAccessors.Abstractions; - -namespace ShardingCore.Core.ShardingAccessors -{ -/* -* @Author: xjm -* @Description: -* @Date: Wednesday, 23 December 2020 07:51:30 -* @Email: 326308290@qq.com -*/ - public class ShardingScope : IDisposable - { - - /// - /// 分表配置访问器 - /// - public IShardingAccessor ShardingAccessor { get; } - - /// - /// 构造函数 - /// - /// - public ShardingScope(IShardingAccessor shardingAccessor) - { - shardingAccessor.ShardingContext = null; - ShardingAccessor = shardingAccessor; - } - - /// - /// 回收 - /// - public void Dispose() - { - ShardingAccessor.ShardingContext = null; - } - } -} \ No newline at end of file diff --git a/src/ShardingCore/Core/ShardingAccessors/ShardingScopeFactory.cs b/src/ShardingCore/Core/ShardingAccessors/ShardingScopeFactory.cs deleted file mode 100644 index 61c03f03..00000000 --- a/src/ShardingCore/Core/ShardingAccessors/ShardingScopeFactory.cs +++ /dev/null @@ -1,35 +0,0 @@ -using ShardingCore.Core.ShardingAccessors.Abstractions; - -namespace ShardingCore.Core.ShardingAccessors -{ -/* -* @Author: xjm -* @Description: -* @Date: Wednesday, 23 December 2020 08:11:06 -* @Email: 326308290@qq.com -*/ - /// - /// 分表查询环境创建 - /// - public class ShardingScopeFactory : IShardingScopeFactory - { - private readonly IShardingAccessor _shardingAccessor; - - /// - /// 构造函数 - /// - /// - public ShardingScopeFactory(IShardingAccessor shardingAccessor) - { - _shardingAccessor = shardingAccessor; - } - /// - /// 创建scope - /// - /// - public ShardingScope CreateScope() - { - return new ShardingScope(_shardingAccessor); - } - } -} \ No newline at end of file diff --git a/src/ShardingCore/DIExtension.cs b/src/ShardingCore/DIExtension.cs index 2fb7dbb0..8cdc6865 100644 --- a/src/ShardingCore/DIExtension.cs +++ b/src/ShardingCore/DIExtension.cs @@ -14,8 +14,6 @@ using ShardingCore.TableCreator; using System; using ShardingCore.Core.QueryRouteManagers; using ShardingCore.Core.QueryRouteManagers.Abstractions; -using ShardingCore.Core.ShardingAccessors; -using ShardingCore.Core.ShardingAccessors.Abstractions; using ShardingCore.Core.ShardingPage; using ShardingCore.Core.ShardingPage.Abstractions; using ShardingCore.Core.VirtualRoutes; @@ -69,7 +67,43 @@ namespace ShardingCore - services.AddSingleton(); + return services; + } + public static IServiceCollection AddShardingDbContext(this IServiceCollection services, + Action optionsAction = null, + Action> configure=null, + ServiceLifetime contextLifetime = ServiceLifetime.Scoped, + ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) + where TActualDbContext : DbContext, IShardingTableDbContext + where TShardingDbContext : DbContext, IShardingTableDbContext + { + if (configure == null) + throw new ArgumentNullException($"AddShardingDbContext params is null :{nameof(configure)}"); + + ShardingCoreHelper.CheckContextConstructors(); + var shardingConfigOptions = new ShardingConfigOption(); + configure?.Invoke(shardingConfigOptions); + services.AddSingleton>(sp=> shardingConfigOptions); + + + //添加创建TActualDbContext 的 创建者 + var config = new ShardingDbContextOptionsBuilderConfig(shardingConfigOptions.SameConnectionConfigure,shardingConfigOptions.DefaultQueryConfigure); + services.AddSingleton>(sp=> config); + + //添加创建TActualDbContext创建者 + services.AddSingleton>(sp=> new DefaultShardingDbContextCreatorConfig(typeof(TActualDbContext))); + + + Action shardingOptionAction = (sp, option) => + { + optionsAction?.Invoke(sp,option); + option.UseSharding(); + }; + services.AddDbContext(shardingOptionAction, contextLifetime, optionsLifetime); + services.AddInternalShardingCore(); + + + return services; } @@ -85,9 +119,6 @@ namespace ShardingCore services.AddSingleton(); //分表引擎 services.AddSingleton(); - //services.AddSingleton(typeof(IVirtualTable<>), typeof(OneDbVirtualTable<>)); - services.AddSingleton(); - services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); @@ -98,6 +129,7 @@ namespace ShardingCore //sharding page services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); return services; } diff --git a/src/ShardingCore/EFCores/ShardingModelCustomizer.cs b/src/ShardingCore/EFCores/ShardingModelCustomizer.cs index 0476d10b..3d774cbd 100644 --- a/src/ShardingCore/EFCores/ShardingModelCustomizer.cs +++ b/src/ShardingCore/EFCores/ShardingModelCustomizer.cs @@ -5,7 +5,6 @@ using System.Text; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.Extensions.DependencyInjection; -using ShardingCore.Core.ShardingAccessors.Abstractions; using ShardingCore.Core.VirtualRoutes.RouteTails.Abstractions; using ShardingCore.Core.VirtualTables; using ShardingCore.DbContexts.ShardingDbContexts; diff --git a/src/ShardingCore/Sharding/PaginationConfigurations/PaginationBuilder.cs b/src/ShardingCore/Sharding/PaginationConfigurations/PaginationBuilder.cs index 74afc8ed..74771828 100644 --- a/src/ShardingCore/Sharding/PaginationConfigurations/PaginationBuilder.cs +++ b/src/ShardingCore/Sharding/PaginationConfigurations/PaginationBuilder.cs @@ -43,15 +43,15 @@ namespace ShardingCore.Sharding.PaginationConfigurations _metadata.ReverseTotalGe = reverseTotalGe; return this; } - /// - /// 配置当分表数目小于多少后直接取到内存不在流式处理 - /// - /// - /// - public PaginationBuilder ConfigTakeInMemoryCountIfLe(int count) - { - _metadata.TakeInMemoryCountIfLe = count; - return this; - } + ///// + ///// 配置当分表数目小于多少后直接取到内存不在流式处理 + ///// + ///// + ///// + //public PaginationBuilder ConfigTakeInMemoryCountIfLe(int count) + //{ + // _metadata.TakeInMemoryCountIfLe = count; + // return this; + //} } } diff --git a/src/ShardingCore/Sharding/StreamMergeContextFactory.cs b/src/ShardingCore/Sharding/StreamMergeContextFactory.cs index 655ed11e..a78d5563 100644 --- a/src/ShardingCore/Sharding/StreamMergeContextFactory.cs +++ b/src/ShardingCore/Sharding/StreamMergeContextFactory.cs @@ -1,7 +1,6 @@ using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine; using ShardingCore.Sharding.Abstractions; using System.Linq; -using ShardingCore.Core.ShardingAccessors; using ShardingCore.Core.VirtualRoutes.RouteTails.Abstractions; namespace ShardingCore.Sharding