This commit is contained in:
parent
c4931470fe
commit
723dd0e7f4
|
@ -46,19 +46,17 @@ namespace ShardingCore.Bootstrappers
|
|||
// private readonly string _virtualTableName;
|
||||
// private readonly Expression<Func<TEntity,bool>> _queryFilterExpression;
|
||||
private readonly Type _shardingEntityType;
|
||||
private readonly IShardingEntityConfigOptions<TShardingDbContext> _shardingEntityConfigOptions;
|
||||
private readonly IVirtualDataSourceManager<TShardingDbContext> _virtualDataSourceManager;
|
||||
private readonly IVirtualDataSourceRouteManager<TShardingDbContext> _virtualDataSourceRouteManager;
|
||||
private readonly IVirtualTableManager<TShardingDbContext> _virtualTableManager;
|
||||
private readonly IEntityMetadataManager<TShardingDbContext> _entityMetadataManager;
|
||||
private readonly IShardingEntityConfigOptions _shardingEntityConfigOptions;
|
||||
private readonly IVirtualDataSourceRouteManager _virtualDataSourceRouteManager;
|
||||
private readonly IVirtualTableManager _virtualTableManager;
|
||||
private readonly IEntityMetadataManager _entityMetadataManager;
|
||||
private readonly IJobManager _jobManager;
|
||||
|
||||
public EntityMetadataInitializer(
|
||||
IShardingEntityConfigOptions<TShardingDbContext> shardingEntityConfigOptions,
|
||||
IVirtualDataSourceManager<TShardingDbContext> virtualDataSourceManager,
|
||||
IVirtualDataSourceRouteManager<TShardingDbContext> virtualDataSourceRouteManager,
|
||||
IVirtualTableManager<TShardingDbContext> virtualTableManager,
|
||||
IEntityMetadataManager<TShardingDbContext> entityMetadataManager,
|
||||
IShardingEntityConfigOptions shardingEntityConfigOptions,
|
||||
IVirtualDataSourceRouteManager virtualDataSourceRouteManager,
|
||||
IVirtualTableManager virtualTableManager,
|
||||
IEntityMetadataManager entityMetadataManager,
|
||||
IJobManager jobManager
|
||||
)
|
||||
{
|
||||
|
@ -67,7 +65,6 @@ namespace ShardingCore.Bootstrappers
|
|||
// _virtualTableName = entityMetadataEnsureParams.VirtualTableName;
|
||||
// _queryFilterExpression = entityMetadataEnsureParams.EntityType.GetAnnotations().FirstOrDefault(o=>o.Name== QueryFilter)?.Value as Expression<Func<TEntity, bool>>;
|
||||
_shardingEntityConfigOptions = shardingEntityConfigOptions;
|
||||
_virtualDataSourceManager = virtualDataSourceManager;
|
||||
_virtualDataSourceRouteManager = virtualDataSourceRouteManager;
|
||||
_virtualTableManager = virtualTableManager;
|
||||
_entityMetadataManager = entityMetadataManager;
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace ShardingCore.Core.EntityMetadatas
|
||||
{
|
||||
/// <summary>
|
||||
/// 分片 对象元数据信息管理 可以通过依赖注入获取
|
||||
/// </summary>
|
||||
/// <typeparam name="TShardingDbContext"></typeparam>
|
||||
public interface IEntityMetadataManager<TShardingDbContext>: IEntityMetadataManager where TShardingDbContext:DbContext,IShardingDbContext
|
||||
{
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
namespace ShardingCore.Core
|
||||
{
|
||||
public interface IShardingEntityType
|
||||
{
|
||||
string GetLogicTableName();
|
||||
bool IsSingleKey { get; }
|
||||
}
|
||||
}
|
|
@ -1,10 +1,19 @@
|
|||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Core.EntityMetadatas;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualTables;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
|
||||
namespace ShardingCore.Core
|
||||
{
|
||||
|
||||
public interface IShardingRuntimeContext
|
||||
{
|
||||
IEntityMetadataManager GetEntityMetadataManager();
|
||||
IVirtualTableManager GetVirtualTableManager();
|
||||
IRouteTailFactory GetRouteTailFactory();
|
||||
IShardingRuntimeModel GetShardingRuntimeModel();
|
||||
IShardingRuntimeModel GetOrCreateShardingRuntimeModel(DbContext dbContext);
|
||||
object GetService(Type serviceType);
|
||||
TService GetService<TService>();
|
||||
}
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ShardingCore.Core
|
||||
{
|
||||
|
||||
public interface IShardingRuntimeModel
|
||||
{
|
||||
|
||||
IShardingEntityType GetShardingEntityType(Type entityType);
|
||||
List<IShardingEntityType> GetShardingEntityTypes();
|
||||
}
|
||||
}
|
|
@ -3,10 +3,10 @@ using ShardingCore.Sharding.Abstractions;
|
|||
|
||||
namespace ShardingCore.Core.ShardingConfigurations.Abstractions
|
||||
{
|
||||
public interface IShardingConfigurationOptions<TShardingDbContext> where TShardingDbContext:DbContext,IShardingDbContext
|
||||
public interface IShardingConfigurationOptions
|
||||
{
|
||||
public void AddShardingGlobalConfigOptions(ShardingConfigOptions<TShardingDbContext> shardingConfigOptions);
|
||||
public void AddShardingGlobalConfigOptions(ShardingConfigOptions shardingConfigOptions);
|
||||
|
||||
public ShardingConfigOptions<TShardingDbContext>[] GetAllShardingGlobalConfigOptions();
|
||||
public ShardingConfigOptions[] GetAllShardingGlobalConfigOptions();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,26 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace ShardingCore.Core.ShardingConfigurations.Abstractions
|
||||
{
|
||||
public interface IShardingEntityConfigOptions<TShardingDbContext> : IShardingEntityConfigOptions where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
/// <summary>
|
||||
/// 添加分库路由
|
||||
/// </summary>
|
||||
/// <typeparam name="TRoute"></typeparam>
|
||||
void AddShardingDataSourceRoute<TRoute>() where TRoute : IVirtualDataSourceRoute;
|
||||
/// <summary>
|
||||
/// 添加分表路由
|
||||
/// </summary>
|
||||
/// <typeparam name="TRoute"></typeparam>
|
||||
void AddShardingTableRoute<TRoute>() where TRoute : IVirtualTableRoute;
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@ using System.Data.Common;
|
|||
|
||||
namespace ShardingCore.Core.ShardingConfigurations
|
||||
{
|
||||
public class ShardingConfigOptions<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
|
||||
public class ShardingConfigOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// 配置id,如果是单配置可以用guid代替,如果是多配置该属性表示每个配置的id
|
||||
|
|
|
@ -8,14 +8,14 @@ using System.Linq;
|
|||
|
||||
namespace ShardingCore.Core.ShardingConfigurations
|
||||
{
|
||||
public class ShardingMultiConfigurationOptions<TShardingDbContext> : IShardingConfigurationOptions<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
|
||||
public class ShardingMultiConfigurationOptions : IShardingConfigurationOptions
|
||||
{
|
||||
public ShardingConfigurationStrategyEnum ShardingConfigurationStrategy { get; set; } =
|
||||
ShardingConfigurationStrategyEnum.ThrowIfNull;
|
||||
|
||||
private Dictionary<string, ShardingConfigOptions<TShardingDbContext>> _shardingGlobalConfigOptions = new ();
|
||||
private Dictionary<string, ShardingConfigOptions> _shardingGlobalConfigOptions = new ();
|
||||
|
||||
public void AddShardingGlobalConfigOptions(ShardingConfigOptions<TShardingDbContext> shardingConfigOptions)
|
||||
public void AddShardingGlobalConfigOptions(ShardingConfigOptions shardingConfigOptions)
|
||||
{
|
||||
if (_shardingGlobalConfigOptions.ContainsKey(shardingConfigOptions.ConfigId))
|
||||
throw new ShardingCoreInvalidOperationException($"repeat add config id:[{shardingConfigOptions.ConfigId}]");
|
||||
|
@ -23,7 +23,7 @@ namespace ShardingCore.Core.ShardingConfigurations
|
|||
_shardingGlobalConfigOptions.Add(shardingConfigOptions.ConfigId, shardingConfigOptions);
|
||||
}
|
||||
|
||||
public ShardingConfigOptions<TShardingDbContext>[] GetAllShardingGlobalConfigOptions()
|
||||
public ShardingConfigOptions[] GetAllShardingGlobalConfigOptions()
|
||||
{
|
||||
return _shardingGlobalConfigOptions.Values.ToArray();
|
||||
}
|
||||
|
|
|
@ -6,21 +6,21 @@ using ShardingCore.Sharding.Abstractions;
|
|||
|
||||
namespace ShardingCore.Core.ShardingConfigurations
|
||||
{
|
||||
public class ShardingSingleConfigurationOptions<TShardingDbContext> : IShardingConfigurationOptions<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
|
||||
public class ShardingSingleConfigurationOptions : IShardingConfigurationOptions
|
||||
{
|
||||
|
||||
private ShardingConfigOptions<TShardingDbContext> _shardingConfigOptions;
|
||||
private ShardingConfigOptions _shardingConfigOptions;
|
||||
public ShardingConfigurationStrategyEnum ShardingConfigurationStrategy { get; set; } =
|
||||
ShardingConfigurationStrategyEnum.ThrowIfNull;
|
||||
|
||||
public void AddShardingGlobalConfigOptions(ShardingConfigOptions<TShardingDbContext> shardingConfigOptions)
|
||||
public void AddShardingGlobalConfigOptions(ShardingConfigOptions shardingConfigOptions)
|
||||
{
|
||||
if (_shardingConfigOptions != null)
|
||||
throw new ShardingCoreInvalidOperationException($"repeat add {nameof(ShardingConfigOptions<TShardingDbContext>)}");
|
||||
throw new ShardingCoreInvalidOperationException($"repeat add {nameof(ShardingConfigOptions)}");
|
||||
_shardingConfigOptions= shardingConfigOptions;
|
||||
}
|
||||
|
||||
public ShardingConfigOptions<TShardingDbContext>[] GetAllShardingGlobalConfigOptions()
|
||||
public ShardingConfigOptions[] GetAllShardingGlobalConfigOptions()
|
||||
{
|
||||
return new[] { _shardingConfigOptions };
|
||||
}
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
using System;
|
||||
|
||||
namespace ShardingCore.Core.ShardingDatabaseProviders
|
||||
{
|
||||
|
||||
public interface IShardingDatabaseProvider
|
||||
{
|
||||
Type GetShardingDbContextType();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace ShardingCore.Core.ShardingDatabaseProviders
|
||||
{
|
||||
public class ShardingDatabaseProvider<TShardingDbContext> : IShardingDatabaseProvider
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
private readonly Type _shardingDbContextType;
|
||||
|
||||
public ShardingDatabaseProvider()
|
||||
{
|
||||
_shardingDbContextType = typeof(TShardingDbContext);
|
||||
}
|
||||
|
||||
public Type GetShardingDbContextType()
|
||||
{
|
||||
return _shardingDbContextType;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -22,12 +22,14 @@ namespace ShardingCore.Core
|
|||
private object INIT_LOCK = new object();
|
||||
private IServiceCollection _serviceMap = new ServiceCollection();
|
||||
|
||||
private IServiceProvider _serviceProvider;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private IServiceProvider _applicationServiceProvider;
|
||||
|
||||
|
||||
private ShardingRuntimeContext()
|
||||
{
|
||||
_serviceProvider = _serviceMap.BuildServiceProvider();
|
||||
_serviceProvider.GetRequiredService<IShardingBootstrapper>().Start();
|
||||
}
|
||||
|
||||
private static readonly ShardingRuntimeContext _instance = new ShardingRuntimeContext();
|
||||
|
|
|
@ -19,7 +19,4 @@ namespace ShardingCore.Core.TrackerManagers
|
|||
bool EntityUseTrack(Type entityType);
|
||||
bool IsDbContextModel(Type entityType);
|
||||
}
|
||||
public interface ITrackerManager<TShardingDbContext>: ITrackerManager where TShardingDbContext:DbContext,IShardingDbContext
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,8 +18,7 @@ using ShardingCore.TableExists.Abstractions;
|
|||
|
||||
namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions
|
||||
{
|
||||
public abstract class AbstractVirtualDataSourceConfigurationParams<TShardingDbContext>:IVirtualDataSourceConfigurationParams<TShardingDbContext>
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
public abstract class AbstractVirtualDataSourceConfigurationParams:IVirtualDataSourceConfigurationParams
|
||||
{
|
||||
public abstract string ConfigId { get; }
|
||||
public abstract int Priority { get; }
|
||||
|
@ -36,7 +35,7 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions
|
|||
public virtual IShardingComparer ShardingComparer { get; } = new CSharpLanguageShardingComparer();
|
||||
|
||||
public virtual ITableEnsureManager TableEnsureManager { get; } =
|
||||
new EmptyTableEnsureManager<TShardingDbContext>();
|
||||
new EmptyTableEnsureManager();
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -89,10 +89,4 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions
|
|||
/// <returns></returns>
|
||||
bool UseReadWriteSeparation();
|
||||
}
|
||||
|
||||
public interface IVirtualDataSourceConfigurationParams<TShardingDbContext> : IVirtualDataSourceConfigurationParams
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,16 +25,8 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions
|
|||
/// <param name="configId"></param>
|
||||
/// <returns></returns>
|
||||
VirtualDataSourceScope CreateScope(string configId);
|
||||
|
||||
}
|
||||
|
||||
public interface IVirtualDataSourceManager<TShardingDbContext> : IVirtualDataSourceManager
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
IVirtualDataSource<TShardingDbContext> GetCurrentVirtualDataSource();
|
||||
IVirtualDataSource<TShardingDbContext> GetVirtualDataSource(string configId);
|
||||
List<IVirtualDataSource<TShardingDbContext>> GetAllVirtualDataSources();
|
||||
bool AddVirtualDataSource(IVirtualDataSourceConfigurationParams<TShardingDbContext> configurationParams);
|
||||
bool AddVirtualDataSource(IVirtualDataSourceConfigurationParams configurationParams);
|
||||
void SetDefaultIfMultiConfiguration();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,11 +27,4 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources.Abstractions
|
|||
/// <exception cref="ShardingCoreInvalidOperationException">对象未配置分库</exception>
|
||||
bool AddVirtualDataSourceRoute(IVirtualDataSourceRoute virtualDataSourceRoute);
|
||||
}
|
||||
|
||||
public interface IVirtualDataSourceRouteManager<TShardingDbContext> : IVirtualDataSourceRouteManager
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
IVirtualDataSourceRoute<TEntity> GetRoute<TEntity>() where TEntity:class;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -129,12 +129,4 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
|
|||
|
||||
IDictionary<string, string> GetDataSources();
|
||||
}
|
||||
/// <summary>
|
||||
/// 虚拟数据源 连接所有的实际数据源
|
||||
/// </summary>
|
||||
public interface IVirtualDataSource<TShardingDbContext> : IVirtualDataSource
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
IVirtualDataSourceRoute<TEntity> GetRoute<TEntity>() where TEntity:class;
|
||||
}
|
||||
}
|
|
@ -17,11 +17,10 @@ using ShardingCore.Core.ShardingConfigurations.Abstractions;
|
|||
|
||||
namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
|
||||
{
|
||||
public class SimpleVirtualDataSourceConfigurationParams<TShardingDbContext>: AbstractVirtualDataSourceConfigurationParams<TShardingDbContext>
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
public class SimpleVirtualDataSourceConfigurationParams: AbstractVirtualDataSourceConfigurationParams
|
||||
{
|
||||
private readonly ShardingConfigOptions<TShardingDbContext> _options;
|
||||
private readonly IShardingEntityConfigOptions<TShardingDbContext> _shardingEntityConfigOptions;
|
||||
private readonly ShardingConfigOptions _options;
|
||||
private readonly IShardingEntityConfigOptions _shardingEntityConfigOptions;
|
||||
public override string ConfigId { get; }
|
||||
public override int Priority { get; }
|
||||
public override int MaxQueryConnectionsLimit { get; }
|
||||
|
@ -37,9 +36,9 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
|
|||
public override IShardingComparer ShardingComparer { get; }
|
||||
public override ITableEnsureManager TableEnsureManager { get; }
|
||||
|
||||
public SimpleVirtualDataSourceConfigurationParams(IServiceProvider serviceProvider,ShardingConfigOptions<TShardingDbContext> options)
|
||||
public SimpleVirtualDataSourceConfigurationParams(IServiceProvider serviceProvider,ShardingConfigOptions options)
|
||||
{
|
||||
_shardingEntityConfigOptions = serviceProvider.GetService<IShardingEntityConfigOptions<TShardingDbContext>>();
|
||||
_shardingEntityConfigOptions = serviceProvider.GetService<IShardingEntityConfigOptions>();
|
||||
_options = options;
|
||||
ConfigId = options.ConfigId;
|
||||
Priority = options.Priority;
|
||||
|
@ -51,7 +50,7 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
|
|||
ShardingComparer = options.ReplaceShardingComparerFactory?.Invoke(serviceProvider) ??
|
||||
new CSharpLanguageShardingComparer();
|
||||
TableEnsureManager = options.TableEnsureManagerFactory?.Invoke(serviceProvider) ??
|
||||
new EmptyTableEnsureManager<TShardingDbContext>();
|
||||
new EmptyTableEnsureManager();
|
||||
if (options.ShardingReadWriteSeparationOptions != null)
|
||||
{
|
||||
if (options.ShardingReadWriteSeparationOptions.ReadWriteNodeSeparationConfigure != null)
|
||||
|
|
|
@ -28,13 +28,13 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
|
|||
* @Date: Friday, 05 February 2021 15:21:04
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class VirtualDataSource<TShardingDbContext> : IVirtualDataSource<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
|
||||
public class VirtualDataSource : IVirtualDataSource
|
||||
{
|
||||
public IVirtualDataSourceConfigurationParams ConfigurationParams { get; }
|
||||
public IConnectionStringManager ConnectionStringManager { get; }
|
||||
|
||||
private readonly IEntityMetadataManager<TShardingDbContext> _entityMetadataManager;
|
||||
private readonly IVirtualDataSourceRouteManager<TShardingDbContext> _dataSourceRouteManager;
|
||||
private readonly IEntityMetadataManager _entityMetadataManager;
|
||||
private readonly IVirtualDataSourceRouteManager _dataSourceRouteManager;
|
||||
|
||||
private readonly IPhysicDataSourcePool _physicDataSourcePool;
|
||||
|
||||
|
@ -44,7 +44,7 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
|
|||
public string DefaultConnectionString { get; private set; }
|
||||
public bool UseReadWriteSeparation { get; }
|
||||
|
||||
public VirtualDataSource(IEntityMetadataManager<TShardingDbContext> entityMetadataManager, IVirtualDataSourceRouteManager<TShardingDbContext> dataSourceRouteManager, IVirtualDataSourceConfigurationParams<TShardingDbContext> configurationParams)
|
||||
public VirtualDataSource(IEntityMetadataManager entityMetadataManager, IVirtualDataSourceRouteManager dataSourceRouteManager, IVirtualDataSourceConfigurationParams configurationParams)
|
||||
{
|
||||
Check.NotNull(configurationParams, nameof(configurationParams));
|
||||
Check.NotNull(configurationParams.ExtraDataSources, nameof(configurationParams.ExtraDataSources));
|
||||
|
@ -99,11 +99,6 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
|
|||
return _dataSourceRouteManager.GetRoute(entityType);
|
||||
}
|
||||
|
||||
public IVirtualDataSourceRoute<TEntity> GetRoute<TEntity>() where TEntity : class
|
||||
{
|
||||
return _dataSourceRouteManager.GetRoute<TEntity>();
|
||||
}
|
||||
|
||||
public List<string> RouteTo(Type entityType, ShardingDataSourceRouteConfig routeRouteConfig)
|
||||
{
|
||||
if (!_entityMetadataManager.IsShardingDataSource(entityType))
|
||||
|
@ -227,7 +222,7 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
|
|||
DbContextOptionsBuilder dbContextOptionsBuilder)
|
||||
{
|
||||
var doUseDbContextOptionsBuilder = ConfigurationParams.UseDbContextOptionsBuilder(connectionString, dbContextOptionsBuilder);
|
||||
doUseDbContextOptionsBuilder.UseInnerDbContextSharding<TShardingDbContext>();
|
||||
doUseDbContextOptionsBuilder.UseInnerDbContextSharding();
|
||||
ConfigurationParams.UseExecutorDbContextOptionBuilder(dbContextOptionsBuilder);
|
||||
return doUseDbContextOptionsBuilder;
|
||||
}
|
||||
|
@ -236,7 +231,7 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
|
|||
DbContextOptionsBuilder dbContextOptionsBuilder)
|
||||
{
|
||||
var doUseDbContextOptionsBuilder = ConfigurationParams.UseDbContextOptionsBuilder(dbConnection, dbContextOptionsBuilder);
|
||||
doUseDbContextOptionsBuilder.UseInnerDbContextSharding<TShardingDbContext>();
|
||||
doUseDbContextOptionsBuilder.UseInnerDbContextSharding();
|
||||
ConfigurationParams.UseExecutorDbContextOptionBuilder(dbContextOptionsBuilder);
|
||||
return doUseDbContextOptionsBuilder;
|
||||
}
|
||||
|
|
|
@ -16,18 +16,18 @@ using ShardingCore.Sharding.Abstractions;
|
|||
|
||||
namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
|
||||
{
|
||||
public class VirtualDataSourceManager<TShardingDbContext> : IVirtualDataSourceManager<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
|
||||
public class VirtualDataSourceManager : IVirtualDataSourceManager
|
||||
{
|
||||
private readonly IShardingConfigurationOptions<TShardingDbContext> _options;
|
||||
private readonly IEntityMetadataManager<TShardingDbContext> _entityMetadataManager;
|
||||
private readonly IVirtualDataSourceRouteManager<TShardingDbContext> _virtualDataSourceRouteManager;
|
||||
private readonly IShardingConfigurationOptions _options;
|
||||
private readonly IEntityMetadataManager _entityMetadataManager;
|
||||
private readonly IVirtualDataSourceRouteManager _virtualDataSourceRouteManager;
|
||||
private readonly IVirtualDataSourceAccessor _virtualDataSourceAccessor;
|
||||
|
||||
private readonly ConcurrentDictionary<string, IVirtualDataSource> _virtualDataSources = new();
|
||||
|
||||
private string _defaultConfigId;
|
||||
private IVirtualDataSource _defaultVirtualDataSource;
|
||||
public VirtualDataSourceManager(IServiceProvider serviceProvider, IShardingConfigurationOptions<TShardingDbContext> options, IEntityMetadataManager<TShardingDbContext> entityMetadataManager, IVirtualDataSourceRouteManager<TShardingDbContext> virtualDataSourceRouteManager, IVirtualDataSourceAccessor virtualDataSourceAccessor)
|
||||
public VirtualDataSourceManager(IServiceProvider serviceProvider, IShardingConfigurationOptions options, IEntityMetadataManager entityMetadataManager, IVirtualDataSourceRouteManager virtualDataSourceRouteManager, IVirtualDataSourceAccessor virtualDataSourceAccessor)
|
||||
{
|
||||
|
||||
_options = options;
|
||||
|
@ -37,19 +37,19 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
|
|||
if (allShardingGlobalConfigOptions.IsEmpty())
|
||||
throw new ArgumentException($"sharding virtual data source is empty");
|
||||
_virtualDataSourceAccessor = virtualDataSourceAccessor;
|
||||
if (options is ShardingMultiConfigurationOptions<TShardingDbContext> shardingMultiConfigurationOptions)
|
||||
if (options is ShardingMultiConfigurationOptions shardingMultiConfigurationOptions)
|
||||
{
|
||||
IsMultiShardingConfiguration = true;
|
||||
ShardingConfigurationStrategy = shardingMultiConfigurationOptions.ShardingConfigurationStrategy;
|
||||
}
|
||||
else if (options is ShardingSingleConfigurationOptions<TShardingDbContext> shardingSingleConfigurationOptions)
|
||||
else if (options is ShardingSingleConfigurationOptions shardingSingleConfigurationOptions)
|
||||
{
|
||||
IsMultiShardingConfiguration = false;
|
||||
ShardingConfigurationStrategy = shardingSingleConfigurationOptions.ShardingConfigurationStrategy;
|
||||
}
|
||||
foreach (var shardingGlobalConfigOption in allShardingGlobalConfigOptions)
|
||||
{
|
||||
var simpleVirtualDataSourceConfigurationParams = new SimpleVirtualDataSourceConfigurationParams<TShardingDbContext>(serviceProvider, shardingGlobalConfigOption);
|
||||
var simpleVirtualDataSourceConfigurationParams = new SimpleVirtualDataSourceConfigurationParams(serviceProvider, shardingGlobalConfigOption);
|
||||
AddVirtualDataSource(simpleVirtualDataSourceConfigurationParams);
|
||||
}
|
||||
if (!IsMultiShardingConfiguration)
|
||||
|
@ -92,12 +92,6 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
|
|||
$"unknown {nameof(ShardingConfigurationStrategyEnum)}:[{ShardingConfigurationStrategy}]");
|
||||
}
|
||||
}
|
||||
|
||||
IVirtualDataSource<TShardingDbContext> IVirtualDataSourceManager<TShardingDbContext>.GetVirtualDataSource(string configId)
|
||||
{
|
||||
return (IVirtualDataSource<TShardingDbContext>)GetVirtualDataSource(configId);
|
||||
}
|
||||
|
||||
public IVirtualDataSource GetVirtualDataSource(string configId)
|
||||
{
|
||||
var hasValue = _virtualDataSources.TryGetValue(configId, out var virtualDataSource);
|
||||
|
@ -116,21 +110,16 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
|
|||
}
|
||||
}
|
||||
|
||||
List<IVirtualDataSource<TShardingDbContext>> IVirtualDataSourceManager<TShardingDbContext>.GetAllVirtualDataSources()
|
||||
{
|
||||
return GetAllVirtualDataSources().Select(o => (IVirtualDataSource<TShardingDbContext>)o).ToList();
|
||||
}
|
||||
|
||||
public bool ContansConfigId(string configId)
|
||||
{
|
||||
return _virtualDataSources.ContainsKey(configId);
|
||||
}
|
||||
|
||||
public bool AddVirtualDataSource(IVirtualDataSourceConfigurationParams<TShardingDbContext> configurationParams)
|
||||
public bool AddVirtualDataSource(IVirtualDataSourceConfigurationParams configurationParams)
|
||||
{
|
||||
if (!IsMultiShardingConfiguration&&_virtualDataSources.IsNotEmpty())
|
||||
throw new NotSupportedException("not support multi sharding configuration");
|
||||
var dataSource = new VirtualDataSource<TShardingDbContext>(_entityMetadataManager, _virtualDataSourceRouteManager, configurationParams);
|
||||
var dataSource = new VirtualDataSource(_entityMetadataManager, _virtualDataSourceRouteManager, configurationParams);
|
||||
dataSource.CheckVirtualDataSource();
|
||||
return _virtualDataSources.TryAdd(dataSource.ConfigId, dataSource);
|
||||
}
|
||||
|
@ -147,12 +136,6 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
IVirtualDataSource<TShardingDbContext> IVirtualDataSourceManager<TShardingDbContext>.GetCurrentVirtualDataSource()
|
||||
{
|
||||
return (IVirtualDataSource<TShardingDbContext>)GetCurrentVirtualDataSource();
|
||||
}
|
||||
|
||||
public List<IVirtualDataSource> GetAllVirtualDataSources()
|
||||
{
|
||||
if (!IsMultiShardingConfiguration)
|
||||
|
|
|
@ -16,13 +16,12 @@ using ShardingCore.Sharding.Abstractions;
|
|||
|
||||
namespace ShardingCore.Core.VirtualDatabase.VirtualDataSources
|
||||
{
|
||||
public class VirtualDataSourceRouteManager<TShardingDbContext> : IVirtualDataSourceRouteManager<TShardingDbContext>
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
public class VirtualDataSourceRouteManager : IVirtualDataSourceRouteManager
|
||||
{
|
||||
private readonly IEntityMetadataManager<TShardingDbContext> _entityMetadataManager;
|
||||
private readonly IEntityMetadataManager _entityMetadataManager;
|
||||
private readonly ConcurrentDictionary<Type, IVirtualDataSourceRoute> _dataSourceVirtualRoutes = new ConcurrentDictionary<Type, IVirtualDataSourceRoute>();
|
||||
|
||||
public VirtualDataSourceRouteManager(IEntityMetadataManager<TShardingDbContext> entityMetadataManager)
|
||||
public VirtualDataSourceRouteManager(IEntityMetadataManager entityMetadataManager)
|
||||
{
|
||||
_entityMetadataManager = entityMetadataManager;
|
||||
}
|
||||
|
|
|
@ -57,11 +57,4 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualTables
|
|||
/// <param name="physicTable"></param>
|
||||
bool AddPhysicTable(Type shardingEntityType, IPhysicTable physicTable);
|
||||
}
|
||||
/// <summary>
|
||||
/// 虚拟表管理者 virtual table manager
|
||||
/// </summary>
|
||||
public interface IVirtualTableManager<TShardingDbContext> : IVirtualTableManager where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
|
||||
}
|
||||
}
|
|
@ -21,15 +21,15 @@ namespace ShardingCore.Core.VirtualDatabase.VirtualTables
|
|||
/// <summary>
|
||||
/// 同一个数据库下的虚拟表管理者
|
||||
/// </summary>
|
||||
public class VirtualTableManager<TShardingDbContext> : IVirtualTableManager<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
|
||||
public class VirtualTableManager : IVirtualTableManager
|
||||
{
|
||||
private readonly IEntityMetadataManager<TShardingDbContext> _entityMetadataManager;
|
||||
private readonly IEntityMetadataManager _entityMetadataManager;
|
||||
|
||||
/// <summary>
|
||||
/// {entityType,virtualTableType}
|
||||
/// </summary>
|
||||
private readonly ConcurrentDictionary<Type, IVirtualTable> _shardingVirtualTables = new ConcurrentDictionary<Type, IVirtualTable>();
|
||||
public VirtualTableManager(IEntityMetadataManager<TShardingDbContext> entityMetadataManager)
|
||||
public VirtualTableManager(IEntityMetadataManager entityMetadataManager)
|
||||
{
|
||||
_entityMetadataManager = entityMetadataManager;
|
||||
}
|
||||
|
|
|
@ -21,9 +21,9 @@ namespace ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine
|
|||
*/
|
||||
public class DataSourceRouteRuleEngine<TShardingDbContext> : IDataSourceRouteRuleEngine<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
private readonly IEntityMetadataManager<TShardingDbContext> _entityMetadataManager;
|
||||
private readonly IEntityMetadataManager _entityMetadataManager;
|
||||
|
||||
public DataSourceRouteRuleEngine(IEntityMetadataManager<TShardingDbContext> entityMetadataManager)
|
||||
public DataSourceRouteRuleEngine(IEntityMetadataManager entityMetadataManager)
|
||||
{
|
||||
_entityMetadataManager = entityMetadataManager;
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
using System.Linq;
|
||||
using ShardingCore.Core.EntityMetadatas;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine;
|
||||
|
@ -12,8 +14,15 @@ namespace ShardingCore.Core.VirtualRoutes
|
|||
* @Date: Sunday, 22 August 2021 14:58:58
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class RouteTailFactory:IRouteTailFactory
|
||||
public class RouteTailFactory : IRouteTailFactory
|
||||
{
|
||||
private readonly IEntityMetadataManager _entityMetadataManager;
|
||||
|
||||
public RouteTailFactory(IEntityMetadataManager entityMetadataManager)
|
||||
{
|
||||
_entityMetadataManager = entityMetadataManager;
|
||||
}
|
||||
|
||||
public IRouteTail Create(string tail)
|
||||
{
|
||||
return Create(tail, true);
|
||||
|
@ -33,7 +42,7 @@ namespace ShardingCore.Core.VirtualRoutes
|
|||
|
||||
public IRouteTail Create(TableRouteResult tableRouteResult)
|
||||
{
|
||||
return Create(tableRouteResult,true);
|
||||
return Create(tableRouteResult, true);
|
||||
}
|
||||
|
||||
public IRouteTail Create(TableRouteResult tableRouteResult, bool cache)
|
||||
|
@ -61,7 +70,10 @@ namespace ShardingCore.Core.VirtualRoutes
|
|||
return new NoCacheSingleQueryRouteTail(tableRouteResult);
|
||||
}
|
||||
}
|
||||
return new MultiQueryRouteTail(tableRouteResult);
|
||||
|
||||
var isShardingTableQuery = tableRouteResult.ReplaceTables.Select(o => o.EntityType)
|
||||
.Any(o => _entityMetadataManager.IsShardingTable(o));
|
||||
return new MultiQueryRouteTail(tableRouteResult, isShardingTableQuery);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -22,14 +22,13 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails
|
|||
private readonly ISet<Type> _entityTypes;
|
||||
private readonly bool _isShardingTableQuery;
|
||||
|
||||
public MultiQueryRouteTail(TableRouteResult tableRouteResult)
|
||||
public MultiQueryRouteTail(TableRouteResult tableRouteResult,bool isShardingTableQuery)
|
||||
{
|
||||
if (tableRouteResult.ReplaceTables.IsEmpty() || tableRouteResult.ReplaceTables.Count <= 1) throw new ArgumentException("route result replace tables must greater than 1");
|
||||
_tableRouteResult = tableRouteResult;
|
||||
_modelCacheKey = RANDOM_MODEL_CACHE_KEY+Guid.NewGuid().ToString("n");
|
||||
_entityTypes = tableRouteResult.ReplaceTables.Select(o=>o.EntityType).ToHashSet();
|
||||
var entityMetadataManager = (IEntityMetadataManager)ShardingRuntimeContext.GetInstance().GetService(typeof(IEntityMetadataManager<>).GetGenericType0(tableRouteResult.ShardingDbContextType));
|
||||
_isShardingTableQuery = _entityTypes.Any(o => entityMetadataManager.IsShardingTable(o));
|
||||
_isShardingTableQuery = isShardingTableQuery;
|
||||
}
|
||||
public string GetRouteTailIdentity()
|
||||
{
|
||||
|
|
|
@ -12,8 +12,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
|
|||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
|
||||
public interface ITableRouteRuleEngine<TShardingDbContext>
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
public interface ITableRouteRuleEngine
|
||||
{
|
||||
IEnumerable<TableRouteResult> Route(TableRouteRuleContext tableRouteRuleContext);
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ using System.Linq;
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Core.EntityMetadatas;
|
||||
using ShardingCore.Core.PhysicTables;
|
||||
using ShardingCore.Core.ShardingDatabaseProviders;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualTables;
|
||||
using ShardingCore.Core.VirtualTables;
|
||||
using ShardingCore.Exceptions;
|
||||
|
@ -19,15 +20,17 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
|
|||
* @Date: Thursday, 28 January 2021 10:51:59
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class TableRouteRuleEngine<TShardingDbContext> : ITableRouteRuleEngine<TShardingDbContext> where TShardingDbContext:DbContext,IShardingDbContext
|
||||
public class TableRouteRuleEngine : ITableRouteRuleEngine
|
||||
{
|
||||
private readonly IVirtualTableManager<TShardingDbContext> _virtualTableManager;
|
||||
private readonly IEntityMetadataManager<TShardingDbContext> _entityMetadataManager;
|
||||
private readonly IVirtualTableManager _virtualTableManager;
|
||||
private readonly IEntityMetadataManager _entityMetadataManager;
|
||||
private readonly IShardingDatabaseProvider _shardingDatabaseProvider;
|
||||
|
||||
public TableRouteRuleEngine(IVirtualTableManager<TShardingDbContext> virtualTableManager,IEntityMetadataManager<TShardingDbContext> entityMetadataManager)
|
||||
public TableRouteRuleEngine(IVirtualTableManager virtualTableManager,IEntityMetadataManager entityMetadataManager,IShardingDatabaseProvider shardingDatabaseProvider)
|
||||
{
|
||||
_virtualTableManager = virtualTableManager;
|
||||
_entityMetadataManager = entityMetadataManager;
|
||||
_shardingDatabaseProvider = shardingDatabaseProvider;
|
||||
}
|
||||
|
||||
public IEnumerable<TableRouteResult> Route(TableRouteRuleContext tableRouteRuleContext)
|
||||
|
@ -57,7 +60,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
|
|||
}
|
||||
}
|
||||
|
||||
return routeMaps.Select(o => o.Value).Cartesian().Select(o => new TableRouteResult(o,typeof(TShardingDbContext)));
|
||||
return routeMaps.Select(o => o.Value).Cartesian().Select(o => new TableRouteResult(o,_shardingDatabaseProvider.GetShardingDbContextType()));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -18,11 +18,11 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
|
|||
/// <summary>
|
||||
/// 表路由规则引擎工厂
|
||||
/// </summary>
|
||||
public class TableRouteRuleEngineFactory<TShardingDbContext> : ITableRouteRuleEngineFactory<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
|
||||
public class TableRouteRuleEngineFactory : ITableRouteRuleEngineFactory
|
||||
{
|
||||
private readonly ITableRouteRuleEngine<TShardingDbContext> _tableRouteRuleEngine;
|
||||
private readonly ITableRouteRuleEngine _tableRouteRuleEngine;
|
||||
|
||||
public TableRouteRuleEngineFactory(ITableRouteRuleEngine<TShardingDbContext> tableRouteRuleEngine)
|
||||
public TableRouteRuleEngineFactory(ITableRouteRuleEngine tableRouteRuleEngine)
|
||||
{
|
||||
_tableRouteRuleEngine = tableRouteRuleEngine;
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ using ShardingCore.TableCreator;
|
|||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
using ShardingCore.Bootstrappers;
|
||||
using ShardingCore.Core;
|
||||
using ShardingCore.Core.DbContextCreator;
|
||||
using ShardingCore.Core.QueryTrackers;
|
||||
using ShardingCore.Core.ShardingConfigurations;
|
||||
|
@ -163,22 +164,22 @@ namespace ShardingCore
|
|||
}
|
||||
|
||||
|
||||
private static DbContextOptionsBuilder UseShardingWrapMark(this DbContextOptionsBuilder optionsBuilder)
|
||||
private static DbContextOptionsBuilder UseShardingWrapMark(this DbContextOptionsBuilder optionsBuilder,IShardingRuntimeContext shardingRuntimeContext)
|
||||
{
|
||||
var extension = optionsBuilder.CreateOrGetExtension();
|
||||
var extension = optionsBuilder.CreateOrGetExtension(shardingRuntimeContext);
|
||||
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
|
||||
return optionsBuilder;
|
||||
}
|
||||
|
||||
private static ShardingWrapOptionsExtension CreateOrGetExtension(this DbContextOptionsBuilder optionsBuilder)
|
||||
private static ShardingWrapOptionsExtension CreateOrGetExtension(this DbContextOptionsBuilder optionsBuilder,IShardingRuntimeContext shardingRuntimeContext)
|
||||
=> optionsBuilder.Options.FindExtension<ShardingWrapOptionsExtension>() ??
|
||||
new ShardingWrapOptionsExtension();
|
||||
new ShardingWrapOptionsExtension(shardingRuntimeContext);
|
||||
|
||||
public static DbContextOptionsBuilder UseInnerDbContextSharding<TShardingDbContext>(this DbContextOptionsBuilder optionsBuilder) where TShardingDbContext : DbContext, IShardingDbContext
|
||||
public static DbContextOptionsBuilder UseInnerDbContextSharding(this DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
return optionsBuilder.ReplaceService<IModelCacheKeyFactory, ShardingModelCacheKeyFactory>()
|
||||
.ReplaceService<IModelSource,ShardingModelSource>()
|
||||
.ReplaceService<IModelCustomizer, ShardingModelCustomizer<TShardingDbContext>>();
|
||||
.ReplaceService<IModelCustomizer, ShardingModelCustomizer>();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -23,17 +23,14 @@ namespace ShardingCore.EFCores.OptionsExtensions
|
|||
public ShardingWrapOptionsExtension(IShardingRuntimeContext shardingRuntimeContext)
|
||||
{
|
||||
_shardingRuntimeContext = shardingRuntimeContext;
|
||||
Console.WriteLine("ShardingWrapOptionsExtension ctor");
|
||||
}
|
||||
public void ApplyServices(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IShardingRuntimeContext>(sp => _shardingRuntimeContext);
|
||||
Console.WriteLine("ShardingWrapOptionsExtension ApplyServices");
|
||||
}
|
||||
|
||||
public void Validate(IDbContextOptions options)
|
||||
{
|
||||
Console.WriteLine("ShardingWrapOptionsExtension Validate");
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ using System.Collections.Generic;
|
|||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using ShardingCore.Core.EntityMetadatas;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualTables;
|
||||
|
@ -30,12 +31,14 @@ namespace ShardingCore.EFCores
|
|||
where TEntity : class
|
||||
{
|
||||
private readonly IShardingDbContext _context;
|
||||
private readonly IShardingRuntimeContext _shardingRuntimeContext;
|
||||
|
||||
#if EFCORE5 || EFCORE6
|
||||
|
||||
public ShardingInternalDbSet(DbContext context, string entityTypeName) : base(context, entityTypeName)
|
||||
{
|
||||
_context = (IShardingDbContext)context;
|
||||
_shardingRuntimeContext=context.GetService<IShardingRuntimeContext>();
|
||||
}
|
||||
#endif
|
||||
#if EFCORE2 || EFCORE3
|
||||
|
@ -68,9 +71,7 @@ namespace ShardingCore.EFCores
|
|||
{
|
||||
if (null == _virtualTableManager)
|
||||
{
|
||||
_virtualTableManager =
|
||||
(IVirtualTableManager) ShardingContainer.GetService(
|
||||
typeof(IVirtualTableManager<>).GetGenericType0(_context.GetType()));
|
||||
_virtualTableManager = _shardingRuntimeContext.GetVirtualTableManager();
|
||||
}
|
||||
|
||||
return _virtualTableManager;
|
||||
|
@ -85,9 +86,7 @@ namespace ShardingCore.EFCores
|
|||
{
|
||||
if (null == _entityMetadataManager)
|
||||
{
|
||||
_entityMetadataManager =
|
||||
(IEntityMetadataManager) ShardingContainer.GetService(
|
||||
typeof(IEntityMetadataManager<>).GetGenericType0(_context.GetType()));
|
||||
_entityMetadataManager =_shardingRuntimeContext.GetEntityMetadataManager();
|
||||
}
|
||||
|
||||
return _entityMetadataManager;
|
||||
|
@ -399,24 +398,30 @@ namespace ShardingCore.EFCores
|
|||
if (keyValues.Length == 1)
|
||||
{
|
||||
var entityMetadata = EntityMetadataManager.TryGet(typeof(TEntity));
|
||||
|
||||
//单key字段
|
||||
if (null != entityMetadata&& entityMetadata.IsSingleKey)
|
||||
{
|
||||
var isShardingDataSource = entityMetadata.IsShardingDataSource();
|
||||
var shardingDataSourceFieldIsKey = entityMetadata.ShardingDataSourceFieldIsKey();
|
||||
if (isShardingDataSource && !shardingDataSourceFieldIsKey)
|
||||
return null;
|
||||
var isShardingTable = entityMetadata.IsShardingTable();
|
||||
var shardingTableFieldIsKey = entityMetadata.ShardingTableFieldIsKey();
|
||||
if (isShardingTable && !shardingTableFieldIsKey)
|
||||
return null;
|
||||
var primaryKeyValue = keyValues[0];
|
||||
if (primaryKeyValue != null)
|
||||
if (null != entityMetadata)
|
||||
{
|
||||
var shardingRuntimeModel = _shardingRuntimeContext.GetOrCreateShardingRuntimeModel((DbContext)_context);
|
||||
var shardingEntityType = shardingRuntimeModel.GetShardingEntityType(typeof(TEntity));
|
||||
if(shardingEntityType!=null&&shardingEntityType.IsSingleKey)
|
||||
{
|
||||
var dataSourceName = GetDataSourceName(primaryKeyValue);
|
||||
var tableTail = VirtualTableManager.GetTableTail<TEntity>(primaryKeyValue);
|
||||
var routeTail = ShardingContainer.GetService<IRouteTailFactory>().Create(tableTail);
|
||||
return _context.GetDbContext(dataSourceName, false, routeTail);
|
||||
var isShardingDataSource = entityMetadata.IsShardingDataSource();
|
||||
var shardingDataSourceFieldIsKey = entityMetadata.ShardingDataSourceFieldIsKey();
|
||||
if (isShardingDataSource && !shardingDataSourceFieldIsKey)
|
||||
return null;
|
||||
var isShardingTable = entityMetadata.IsShardingTable();
|
||||
var shardingTableFieldIsKey = entityMetadata.ShardingTableFieldIsKey();
|
||||
if (isShardingTable && !shardingTableFieldIsKey)
|
||||
return null;
|
||||
var primaryKeyValue = keyValues[0];
|
||||
if (primaryKeyValue != null)
|
||||
{
|
||||
var dataSourceName = GetDataSourceName(primaryKeyValue);
|
||||
var tableTail = VirtualTableManager.GetTableTail<TEntity>(primaryKeyValue);
|
||||
var routeTail = _shardingRuntimeContext.GetRouteTailFactory().Create(tableTail);;
|
||||
return _context.GetDbContext(dataSourceName, false, routeTail);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,8 +4,10 @@ using System.Linq;
|
|||
using System.Text;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ShardingCore.Core;
|
||||
using ShardingCore.Core.EntityMetadatas;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualTables;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
|
@ -25,23 +27,24 @@ namespace ShardingCore.EFCores
|
|||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingModelCustomizer<TShardingDbContext> : ModelCustomizer where TShardingDbContext : DbContext, IShardingDbContext
|
||||
public class ShardingModelCustomizer : ModelCustomizer
|
||||
{
|
||||
private static readonly ILogger<ShardingModelCustomizer<TShardingDbContext>> _logger =
|
||||
InternalLoggerFactory.CreateLogger<ShardingModelCustomizer<TShardingDbContext>>();
|
||||
private Type _shardingDbContextType => typeof(TShardingDbContext);
|
||||
private readonly IEntityMetadataManager<TShardingDbContext> _entityMetadataManager;
|
||||
private static readonly ILogger<ShardingModelCustomizer> _logger =
|
||||
InternalLoggerFactory.CreateLogger<ShardingModelCustomizer>();
|
||||
|
||||
public ShardingModelCustomizer(ModelCustomizerDependencies dependencies) : base(dependencies)
|
||||
{
|
||||
_entityMetadataManager = ShardingContainer.GetService<IEntityMetadataManager<TShardingDbContext>>();
|
||||
}
|
||||
|
||||
public override void Customize(ModelBuilder modelBuilder, DbContext context)
|
||||
{
|
||||
base.Customize(modelBuilder, context);
|
||||
|
||||
if (context is IShardingTableDbContext shardingTableDbContext&& shardingTableDbContext.RouteTail !=null&& shardingTableDbContext.RouteTail.IsShardingTableQuery())
|
||||
{
|
||||
|
||||
var shardingRuntimeContext = context.GetService<IShardingRuntimeContext>();
|
||||
var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
|
||||
var isMultiEntityQuery = shardingTableDbContext.RouteTail.IsMultiEntityQuery();
|
||||
if (!isMultiEntityQuery)
|
||||
{
|
||||
|
@ -49,36 +52,37 @@ namespace ShardingCore.EFCores
|
|||
var tail = singleQueryRouteTail.GetTail();
|
||||
|
||||
//设置分表
|
||||
var mutableEntityTypes = modelBuilder.Model.GetEntityTypes().Where(o => _entityMetadataManager.IsShardingTable(o.ClrType)).ToArray();
|
||||
var mutableEntityTypes = modelBuilder.Model.GetEntityTypes().Where(o => entityMetadataManager.IsShardingTable(o.ClrType)).ToArray();
|
||||
foreach (var entityType in mutableEntityTypes)
|
||||
{
|
||||
MappingToTable(entityType.ClrType, modelBuilder, tail);
|
||||
MappingToTable(entityMetadataManager,entityType, modelBuilder, tail);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var multiQueryRouteTail = (IMultiQueryRouteTail) shardingTableDbContext.RouteTail;
|
||||
var entityTypes = multiQueryRouteTail.GetEntityTypes();
|
||||
var mutableEntityTypes = modelBuilder.Model.GetEntityTypes().Where(o => _entityMetadataManager.IsShardingTable(o.ClrType) && entityTypes.Contains(o.ClrType)).ToArray();
|
||||
var mutableEntityTypes = modelBuilder.Model.GetEntityTypes().Where(o => entityMetadataManager.IsShardingTable(o.ClrType) && entityTypes.Contains(o.ClrType)).ToArray();
|
||||
foreach (var entityType in mutableEntityTypes)
|
||||
{
|
||||
var queryTail = multiQueryRouteTail.GetEntityTail(entityType.ClrType);
|
||||
if (queryTail != null)
|
||||
{
|
||||
MappingToTable(entityType.ClrType, modelBuilder, queryTail);
|
||||
MappingToTable(entityMetadataManager,entityType, modelBuilder, queryTail);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void MappingToTable(Type clrType, ModelBuilder modelBuilder, string tail)
|
||||
private void MappingToTable(IEntityMetadataManager entityMetadataManager,IMutableEntityType mutableEntityType, ModelBuilder modelBuilder, string tail)
|
||||
{
|
||||
var entityMetadata = _entityMetadataManager.TryGet(clrType);
|
||||
var clrType = mutableEntityType.ClrType;
|
||||
var entityMetadata = entityMetadataManager.TryGet(clrType);
|
||||
var shardingEntity = entityMetadata.EntityType;
|
||||
var tableSeparator = entityMetadata.TableSeparator;
|
||||
var entity = modelBuilder.Entity(shardingEntity);
|
||||
var tableName = entityMetadata.VirtualTableName;
|
||||
var tableName = mutableEntityType.GetTableName();
|
||||
if (string.IsNullOrWhiteSpace(tableName))
|
||||
throw new ArgumentNullException($"{shardingEntity}: not found original table name。");
|
||||
_logger.LogDebug($"mapping table :[tableName]-->[{tableName}{tableSeparator}{tail}]");
|
||||
|
|
|
@ -16,6 +16,7 @@ using Microsoft.Extensions.DependencyInjection;
|
|||
using ShardingCore.Core;
|
||||
using ShardingCore.Core.EntityMetadatas;
|
||||
using ShardingCore.EFCores;
|
||||
using ShardingCore.Exceptions;
|
||||
using ShardingCore.Utils;
|
||||
|
||||
namespace ShardingCore.Extensions
|
||||
|
@ -40,7 +41,8 @@ namespace ShardingCore.Extensions
|
|||
|
||||
var contextModel = dbContext.Model as Model;
|
||||
#endif
|
||||
var entityMetadataManager = (IEntityMetadataManager)ShardingContainer.GetService(typeof(IEntityMetadataManager<>).GetGenericType0(dbContext.GetType()));
|
||||
var shardingRuntimeContext = dbContext.GetRequireService<IShardingRuntimeContext>();
|
||||
var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
|
||||
|
||||
#if EFCORE6
|
||||
var contextModelRelationalModel = contextModel.GetRelationalModel() as RelationalModel;
|
||||
|
@ -116,7 +118,8 @@ namespace ShardingCore.Extensions
|
|||
|
||||
var contextModel = dbContext.Model as Model;
|
||||
#endif
|
||||
var entityMetadataManager = (IEntityMetadataManager)ShardingContainer.GetService(typeof(IEntityMetadataManager<>).GetGenericType0(dbContext.GetType()));
|
||||
var shardingRuntimeContext = dbContext.GetRequireService<IShardingRuntimeContext>();
|
||||
var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
|
||||
|
||||
#if EFCORE6
|
||||
var contextModelRelationalModel = contextModel.GetRelationalModel() as RelationalModel;
|
||||
|
@ -323,5 +326,16 @@ namespace ShardingCore.Extensions
|
|||
//return entry?.Entity;
|
||||
}
|
||||
|
||||
public static TService GetRequireService<TService>(this DbContext dbContext) where TService:class
|
||||
{
|
||||
var service = dbContext.GetService<TService>();
|
||||
if (service == null)
|
||||
{
|
||||
throw new ShardingCoreInvalidOperationException($"cant resolve:[{typeof(TService)}]");
|
||||
}
|
||||
|
||||
return service;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -18,6 +18,7 @@ using System.Collections.Concurrent;
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using ShardingCore.Sharding.MergeEngines.Abstractions;
|
||||
|
||||
|
||||
|
@ -81,7 +82,7 @@ namespace ShardingCore.Sharding
|
|||
|
||||
|
||||
public StreamMergeContext(IMergeQueryCompilerContext mergeQueryCompilerContext,IParseResult parseResult,IQueryable rewriteQueryable,IOptimizeResult optimizeResult,
|
||||
IRouteTailFactory routeTailFactory)
|
||||
IRouteTailFactory routeTailFactory,ITrackerManager trackerManager,IShardingEntityConfigOptions shardingEntityConfigOptions)
|
||||
{
|
||||
MergeQueryCompilerContext = mergeQueryCompilerContext;
|
||||
ParseResult = parseResult;
|
||||
|
@ -89,8 +90,8 @@ namespace ShardingCore.Sharding
|
|||
OptimizeResult = optimizeResult;
|
||||
_routeTailFactory = routeTailFactory;
|
||||
QueryEntities= MergeQueryCompilerContext.GetQueryEntities().Keys.ToHashSet();
|
||||
_trackerManager = ShardingContainer.GetTrackerManager(mergeQueryCompilerContext.GetShardingDbContextType());
|
||||
_shardingEntityConfigOptions = ShardingContainer.GetRequiredShardingEntityConfigOption(mergeQueryCompilerContext.GetShardingDbContextType());
|
||||
_trackerManager =trackerManager;
|
||||
_shardingEntityConfigOptions = shardingEntityConfigOptions;
|
||||
_parallelDbContexts = new ConcurrentDictionary<DbContext, object>();
|
||||
Orders = parseResult.GetOrderByContext().PropertyOrders.ToArray();
|
||||
Skip = parseResult.GetPaginationContext().Skip;
|
||||
|
|
|
@ -3,6 +3,8 @@ using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine;
|
|||
using ShardingCore.Sharding.Abstractions;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Core.ShardingConfigurations.Abstractions;
|
||||
using ShardingCore.Core.TrackerManagers;
|
||||
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding.MergeContexts;
|
||||
|
@ -22,22 +24,26 @@ namespace ShardingCore.Sharding
|
|||
private readonly IQueryableParseEngine _queryableParseEngine;
|
||||
private readonly IQueryableRewriteEngine _queryableRewriteEngine;
|
||||
private readonly IQueryableOptimizeEngine _queryableOptimizeEngine;
|
||||
private readonly ITrackerManager _trackerManager;
|
||||
private readonly IShardingEntityConfigOptions _shardingEntityConfigOptions;
|
||||
|
||||
public StreamMergeContextFactory(IRouteTailFactory routeTailFactory
|
||||
, IQueryableParseEngine queryableParseEngine, IQueryableRewriteEngine queryableRewriteEngine, IQueryableOptimizeEngine queryableOptimizeEngine
|
||||
)
|
||||
, IQueryableParseEngine queryableParseEngine, IQueryableRewriteEngine queryableRewriteEngine, IQueryableOptimizeEngine queryableOptimizeEngine,
|
||||
ITrackerManager trackerManager,IShardingEntityConfigOptions shardingEntityConfigOptions)
|
||||
{
|
||||
_routeTailFactory = routeTailFactory;
|
||||
_queryableParseEngine = queryableParseEngine;
|
||||
_queryableRewriteEngine = queryableRewriteEngine;
|
||||
_queryableOptimizeEngine = queryableOptimizeEngine;
|
||||
_trackerManager = trackerManager;
|
||||
_shardingEntityConfigOptions = shardingEntityConfigOptions;
|
||||
}
|
||||
public StreamMergeContext Create(IMergeQueryCompilerContext mergeQueryCompilerContext)
|
||||
{
|
||||
var parseResult = _queryableParseEngine.Parse(mergeQueryCompilerContext);
|
||||
var rewriteQueryable = _queryableRewriteEngine.GetRewriteQueryable(mergeQueryCompilerContext, parseResult);
|
||||
var optimizeResult = _queryableOptimizeEngine.Optimize(mergeQueryCompilerContext, parseResult, rewriteQueryable);
|
||||
return new StreamMergeContext(mergeQueryCompilerContext, parseResult, rewriteQueryable,optimizeResult, _routeTailFactory);
|
||||
return new StreamMergeContext(mergeQueryCompilerContext, parseResult, rewriteQueryable,optimizeResult, _routeTailFactory,_trackerManager,_shardingEntityConfigOptions);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -13,7 +13,4 @@ namespace ShardingCore.TableExists.Abstractions
|
|||
ISet<string> GetExistTables(string dataSourceName);
|
||||
ISet<string> GetExistTables(IShardingDbContext shardingDbContext, string dataSourceName);
|
||||
}
|
||||
public interface ITableEnsureManager<TShardingDbContext>: ITableEnsureManager where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ using ShardingCore.TableExists.Abstractions;
|
|||
|
||||
namespace ShardingCore.TableExists
|
||||
{
|
||||
public class EmptyTableEnsureManager<TShardingDbContext> : ITableEnsureManager<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
|
||||
public class EmptyTableEnsureManager : ITableEnsureManager
|
||||
{
|
||||
public ISet<string> GetExistTables(string dataSourceName)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue