添加dbcontextbuildercreator的接口依赖支持替换,支持添加executordbcontext的applicationserivceprovider的注入
This commit is contained in:
parent
f09306530b
commit
4a050cf26a
|
@ -1,5 +1,7 @@
|
|||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using ShardingCore.Core.RuntimeContexts;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.Sharding;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
@ -24,6 +26,7 @@ namespace Sample.AutoCreateIfPresent
|
|||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.ApplyConfiguration(new OrderByHourMap());
|
||||
modelBuilder.ApplyConfiguration(new AreaDeviceMap());
|
||||
Console.WriteLine(this.IsExecutor);
|
||||
}
|
||||
|
||||
public IRouteTail RouteTail { get; set; }
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Core.DbContextTypeAwares;
|
||||
using ShardingCore.Core.ServiceProviders;
|
||||
|
||||
namespace ShardingCore.Core.DbContextOptionBuilderCreator
|
||||
{
|
||||
public class ActivatorDbContextOptionBuilderCreator:IDbContextOptionBuilderCreator
|
||||
{
|
||||
private readonly IShardingProvider _shardingProvider;
|
||||
private readonly IDbContextTypeAware _dbContextTypeAware;
|
||||
|
||||
public ActivatorDbContextOptionBuilderCreator(IShardingProvider shardingProvider,IDbContextTypeAware dbContextTypeAware)
|
||||
{
|
||||
_shardingProvider = shardingProvider;
|
||||
_dbContextTypeAware = dbContextTypeAware;
|
||||
}
|
||||
public DbContextOptionsBuilder CreateDbContextOptionBuilder()
|
||||
{
|
||||
var dbContextType = _dbContextTypeAware.GetContextType();
|
||||
Type type = typeof(DbContextOptionsBuilder<>);
|
||||
type = type.MakeGenericType(dbContextType);
|
||||
var dbContextOptionsBuilder = (DbContextOptionsBuilder)Activator.CreateInstance(type);
|
||||
if (_shardingProvider.ApplicationServiceProvider != null)
|
||||
{
|
||||
dbContextOptionsBuilder.UseApplicationServiceProvider(_shardingProvider.ApplicationServiceProvider);
|
||||
}
|
||||
return dbContextOptionsBuilder;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ShardingCore.Core.DbContextOptionBuilderCreator
|
||||
{
|
||||
public interface IDbContextOptionBuilderCreator
|
||||
{
|
||||
DbContextOptionsBuilder CreateDbContextOptionBuilder();
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@ using System;
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ShardingCore.Core.DbContextCreator;
|
||||
using ShardingCore.Core.DbContextOptionBuilderCreator;
|
||||
using ShardingCore.Core.DbContextTypeAwares;
|
||||
using ShardingCore.Core.EntityMetadatas;
|
||||
using ShardingCore.Core.QueryRouteManagers.Abstractions;
|
||||
|
@ -31,6 +32,7 @@ namespace ShardingCore.Core.RuntimeContexts
|
|||
Type DbContextType { get; }
|
||||
IDbContextTypeAware GetDbContextTypeAware();
|
||||
IShardingProvider GetShardingProvider();
|
||||
IDbContextOptionBuilderCreator GetDbContextOptionBuilderCreator();
|
||||
ShardingConfigOptions GetShardingConfigOptions();
|
||||
IShardingRouteConfigOptions GetShardingRouteConfigOptions();
|
||||
IShardingMigrationManager GetShardingMigrationManager();
|
||||
|
|
|
@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection;
|
|||
using Microsoft.Extensions.Logging;
|
||||
using ShardingCore.Bootstrappers;
|
||||
using ShardingCore.Core.DbContextCreator;
|
||||
using ShardingCore.Core.DbContextOptionBuilderCreator;
|
||||
using ShardingCore.Core.DbContextTypeAwares;
|
||||
using ShardingCore.Core.EntityMetadatas;
|
||||
using ShardingCore.Core.QueryRouteManagers.Abstractions;
|
||||
|
@ -87,6 +88,12 @@ namespace ShardingCore.Core.RuntimeContexts
|
|||
return _shardingProvider??=GetRequiredService<IShardingProvider>();
|
||||
}
|
||||
|
||||
private IDbContextOptionBuilderCreator _dbContextOptionBuilderCreator;
|
||||
public IDbContextOptionBuilderCreator GetDbContextOptionBuilderCreator()
|
||||
{
|
||||
return _dbContextOptionBuilderCreator??=GetRequiredService<IDbContextOptionBuilderCreator>();
|
||||
}
|
||||
|
||||
private ShardingConfigOptions _shardingConfigOptions;
|
||||
public ShardingConfigOptions GetShardingConfigOptions()
|
||||
{
|
||||
|
@ -323,6 +330,7 @@ namespace ShardingCore.Core.RuntimeContexts
|
|||
{
|
||||
GetDbContextTypeAware();
|
||||
GetShardingProvider();
|
||||
GetDbContextOptionBuilderCreator();
|
||||
GetShardingConfigOptions();
|
||||
GetShardingRouteConfigOptions();
|
||||
GetShardingMigrationManager();
|
||||
|
|
|
@ -80,7 +80,6 @@ namespace ShardingCore.EFCores
|
|||
shardingMigrationManager.Current.CurrentDataSourceName = migrateUnit.DataSourceName;
|
||||
|
||||
var dbContextOptions = DynamicShardingHelper.CreateShellDbContextOptions(shardingRuntimeContext,
|
||||
migrateUnit.ShellDbContext.GetType(),
|
||||
migrateUnit.DataSourceName);
|
||||
|
||||
using (var dbContext = dbContextCreator.CreateDbContext(migrateUnit.ShellDbContext,
|
||||
|
|
|
@ -101,7 +101,7 @@ namespace ShardingCore.Helpers
|
|||
{
|
||||
shardingMigrationManager.Current.CurrentDataSourceName = migrateUnit.DataSourceName;
|
||||
|
||||
var dbContextOptions = CreateShellDbContextOptions(shardingRuntimeContext,migrateUnit.ShellDbContext.GetType(),
|
||||
var dbContextOptions = CreateShellDbContextOptions(shardingRuntimeContext,
|
||||
migrateUnit.DataSourceName);
|
||||
|
||||
using (var dbContext = dbContextCreator.CreateDbContext(migrateUnit.ShellDbContext,
|
||||
|
@ -123,11 +123,11 @@ namespace ShardingCore.Helpers
|
|||
await TaskHelper.WhenAllFastFail(migrateTasks).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public static DbContextOptions CreateShellDbContextOptions(IShardingRuntimeContext shardingRuntimeContext,Type dbContextType,string dataSourceName)
|
||||
public static DbContextOptions CreateShellDbContextOptions(IShardingRuntimeContext shardingRuntimeContext,string dataSourceName)
|
||||
{
|
||||
var virtualDataSource = shardingRuntimeContext.GetVirtualDataSource();
|
||||
var shardingConfigOptions = shardingRuntimeContext.GetShardingConfigOptions();
|
||||
var dbContextOptionBuilder = DataSourceDbContext.CreateDbContextOptionBuilder(dbContextType);
|
||||
var dbContextOptionBuilder =shardingRuntimeContext.GetDbContextOptionBuilderCreator().CreateDbContextOptionBuilder();
|
||||
var connectionString = virtualDataSource.GetConnectionString(dataSourceName);
|
||||
virtualDataSource.UseDbContextOptionsBuilder(connectionString, dbContextOptionBuilder);
|
||||
shardingConfigOptions.ShardingMigrationConfigure?.Invoke(dbContextOptionBuilder);
|
||||
|
|
|
@ -154,8 +154,9 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
|
|||
try
|
||||
{
|
||||
//先创建dbcontext option builder
|
||||
var dbContextOptionsBuilder = CreateDbContextOptionBuilder(DbContextType).UseShardingOptions(_shardingRuntimeContext);
|
||||
|
||||
var dbContextOptionBuilderCreator = _shardingRuntimeContext.GetDbContextOptionBuilderCreator();
|
||||
var dbContextOptionsBuilder = dbContextOptionBuilderCreator.CreateDbContextOptionBuilder().UseShardingOptions(_shardingRuntimeContext);
|
||||
|
||||
if (IsDefault)
|
||||
{
|
||||
//如果是默认的需要使用shell的dbconnection为了保证可以使用事务
|
||||
|
@ -191,13 +192,6 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
|
|||
}
|
||||
}
|
||||
|
||||
public static DbContextOptionsBuilder CreateDbContextOptionBuilder(Type dbContextType)
|
||||
{
|
||||
Type type = typeof(DbContextOptionsBuilder<>);
|
||||
type = type.MakeGenericType(dbContextType);
|
||||
return (DbContextOptionsBuilder)Activator.CreateInstance(type);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 不支持并发后期发现直接报错而不是用lock
|
||||
/// </summary>
|
||||
|
|
|
@ -114,7 +114,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
|
|||
|
||||
private DbContextOptions CreateParallelDbContextOptions(string dataSourceName,CreateDbContextStrategyEnum strategy)
|
||||
{
|
||||
var dbContextOptionBuilder = DataSourceDbContext.CreateDbContextOptionBuilder(_shardingDbContext.GetType());
|
||||
var dbContextOptionBuilder =_shardingRuntimeContext.GetDbContextOptionBuilderCreator().CreateDbContextOptionBuilder();
|
||||
var connectionString = _actualConnectionStringManager.GetConnectionString(dataSourceName, CreateDbContextStrategyEnum.IndependentConnectionWrite==strategy);
|
||||
_virtualDataSource.UseDbContextOptionsBuilder(connectionString, dbContextOptionBuilder).UseShardingOptions(_shardingRuntimeContext);
|
||||
return dbContextOptionBuilder.Options;
|
||||
|
|
|
@ -31,6 +31,7 @@ using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
|
|||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using ShardingCore.Bootstrappers;
|
||||
using ShardingCore.Core.DbContextCreator;
|
||||
using ShardingCore.Core.DbContextOptionBuilderCreator;
|
||||
using ShardingCore.Core.DbContextTypeAwares;
|
||||
using ShardingCore.Core.QueryTrackers;
|
||||
using ShardingCore.Core.RuntimeContexts;
|
||||
|
@ -144,6 +145,7 @@ namespace ShardingCore
|
|||
.TryAddSingleton<IVirtualDataSourceConfigurationParams, SimpleVirtualDataSourceConfigurationParams>();
|
||||
//分表dbcontext创建
|
||||
services.TryAddSingleton<IDbContextCreator, ActivatorDbContextCreator<TShardingDbContext>>();
|
||||
services.TryAddSingleton<IDbContextOptionBuilderCreator, ActivatorDbContextOptionBuilderCreator>();
|
||||
|
||||
|
||||
// services.TryAddSingleton<IDataSourceInitializer<TShardingDbContext>, DataSourceInitializer<TShardingDbContext>>();
|
||||
|
|
Loading…
Reference in New Issue