添加dbcontext多构造参数支持[#133],发布x.4.2.7

This commit is contained in:
xuejiaming 2022-04-03 16:21:51 +08:00
parent 0a655416f5
commit 3a5e67c57a
25 changed files with 214 additions and 189 deletions

View File

@ -1,9 +1,9 @@
:start
::定义版本
set EFCORE2=2.4.2.06
set EFCORE3=3.4.2.06
set EFCORE5=5.4.2.06
set EFCORE6=6.4.2.06
set EFCORE2=2.4.2.07
set EFCORE3=3.4.2.07
set EFCORE5=5.4.2.07
set EFCORE6=6.4.2.07
::删除所有bin与obj下的文件
@echo off

View File

@ -50,12 +50,12 @@ namespace Sample.MySql
o.AddShardingTableRoute<SysUserModVirtualTableRoute>();
o.UseShardingQuery((conStr, builder) =>
{
builder.UseMySql(conStr, new MySqlServerVersion(new Version())).UseLoggerFactory(efLogger);
builder.UseMySql(conStr, new MySqlServerVersion(new Version())).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking).UseLoggerFactory(efLogger);
//builder.UseMySql(conStr, new MySqlServerVersion(new Version()));
});
o.UseShardingTransaction((connection, builder) =>
{
builder.UseMySql(connection, new MySqlServerVersion(new Version())).UseLoggerFactory(efLogger);
builder.UseMySql(connection, new MySqlServerVersion(new Version())).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking).UseLoggerFactory(efLogger);
});
})
.AddConfig(op =>

View File

@ -11,6 +11,10 @@ using ShardingCore;
using ShardingCore.TableExists;
using System;
using System.Diagnostics;
using Microsoft.Extensions.DependencyInjection.Extensions;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Sharding.ShardingComparision;
using ShardingCore.Sharding.ShardingComparision.Abstractions;
namespace Sample.SqlServer
{
@ -75,6 +79,7 @@ namespace Sample.SqlServer
// }).End();
services.AddHealthChecks().AddDbContextCheck<DefaultShardingDbContext>();
services.Replace(ServiceDescriptor.Singleton<IDbContextCreator<DefaultShardingDbContext>, ActivatorDbContextCreator<DefaultShardingDbContext>>());
//services.AddShardingDbContext<DefaultShardingDbContext, DefaultTableDbContext>(
// o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;")
// , op =>

View File

@ -38,34 +38,34 @@ namespace Sample.SqlServer3x.Controllers
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
Console.WriteLine("---------------开始-----------------");
var s = DateTime.Now.ToString("HHmmss");
Task.Run(() =>
{
try
{
var virtualTable = _virtualTableManager.GetVirtualTable(typeof(SysUserMod));
_virtualTableManager.AddPhysicTable(typeof(SysUserMod), new DefaultPhysicTable(virtualTable, s));
_shardingTableCreator.CreateTable<SysUserMod>("A", s);
}
catch (Exception e)
{
Console.WriteLine(e);
}
});
Task.Run(() =>
{
try
{
var virtualTable = _virtualTableManager.GetVirtualTable(typeof(SysUserModAbc));
_virtualTableManager.AddPhysicTable(typeof(SysUserModAbc), new DefaultPhysicTable(virtualTable, s));
_shardingTableCreator.CreateTable<SysUserModAbc>("A", s);
}
catch (Exception e)
{
Console.WriteLine(e);
}
});
//Console.WriteLine("---------------开始-----------------");
//var s = DateTime.Now.ToString("HHmmss");
//Task.Run(() =>
//{
// try
// {
// var virtualTable = _virtualTableManager.GetVirtualTable(typeof(SysUserMod));
// _virtualTableManager.AddPhysicTable(typeof(SysUserMod), new DefaultPhysicTable(virtualTable, s));
// _shardingTableCreator.CreateTable<SysUserMod>("A", s);
// }
// catch (Exception e)
// {
// Console.WriteLine(e);
// }
//});
//Task.Run(() =>
//{
// try
// {
// var virtualTable = _virtualTableManager.GetVirtualTable(typeof(SysUserModAbc));
// _virtualTableManager.AddPhysicTable(typeof(SysUserModAbc), new DefaultPhysicTable(virtualTable, s));
// _shardingTableCreator.CreateTable<SysUserModAbc>("A", s);
// }
// catch (Exception e)
// {
// Console.WriteLine(e);
// }
//});
//try
//{
// var virtualTable = _virtualTableManager.GetVirtualTable(typeof(SysUserMod));

View File

@ -1,6 +1,8 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Sample.SqlServer3x.Domain.Maps;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Sharding;
using ShardingCore.Sharding.Abstractions;
@ -14,10 +16,40 @@ namespace Sample.SqlServer3x
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
public interface IScopedService
{
}
public class ScopedService : IScopedService
{
}
public class CustomerDbContextCreator : IDbContextCreator<DefaultDbContext>
{
public DbContext CreateDbContext(DbContext mainDbContext, ShardingDbContextOptions shardingDbContextOptions)
{
var dbContext = new DefaultDbContext((DbContextOptions<DefaultDbContext>)shardingDbContextOptions.DbContextOptions,((DefaultDbContext)mainDbContext).ServiceProvider);
if (dbContext is IShardingTableDbContext shardingTableDbContext)
{
shardingTableDbContext.RouteTail = shardingDbContextOptions.RouteTail;
}
_ = dbContext.Model;
return dbContext;
}
}
public class DefaultDbContext : AbstractShardingDbContext, IShardingTableDbContext
{
public DefaultDbContext(DbContextOptions<DefaultDbContext> options) : base(options)
public IServiceProvider ServiceProvider { get; }
private readonly IScopedService _scopedService;
public DefaultDbContext(DbContextOptions<DefaultDbContext> options,IServiceProvider serviceProvider) : base(options)
{
ServiceProvider = serviceProvider;
_scopedService = serviceProvider.GetRequiredService<IScopedService>();
Console.WriteLine("DefaultDbContext ctor");
}

View File

@ -10,10 +10,12 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Sample.SqlServer3x.Domain.Entities;
using Sample.SqlServer3x.Shardings;
using ShardingCore;
using ShardingCore.Bootstrapers;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.TableExists;
namespace Sample.SqlServer3x
@ -75,6 +77,9 @@ namespace Sample.SqlServer3x
"Data Source=localhost;Initial Catalog=ShardingCoreCreate;Integrated Security=True;"
);
}).EnsureConfig();
services.AddScoped<IScopedService, ScopedService>();
services.Replace(
ServiceDescriptor.Singleton<IDbContextCreator<DefaultDbContext>, CustomerDbContextCreator>());
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

View File

@ -24,8 +24,6 @@ namespace Sample.SqlServerShardingAll.Controllers
}
public async Task<IActionResult> Query()
{
var virtualTableManager = ShardingContainer.GetService<IVirtualTableManager<MyDbContext>>();
virtualTableManager.AddVirtualTable()
#region

View File

@ -9,7 +9,11 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.Extensions.DependencyInjection.Extensions;
using ShardingCore;
using ShardingCore.Sharding.ShardingComparision;
using ShardingCore.Sharding.ShardingComparision.Abstractions;
namespace Samples.AbpSharding
{
@ -28,6 +32,7 @@ namespace Samples.AbpSharding
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Helpers;
using ShardingCore.Sharding.Abstractions;
namespace ShardingCore.Core.DbContextCreator
{
/// <summary>
///
/// </summary>
/// Author: xjm
/// Created: 2022/4/2 21:15:09
/// Email: 326308290@qq.com
public class ActivatorDbContextCreator<TShardingDbContext>: IDbContextCreator<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
{
private readonly Func<ShardingDbContextOptions, DbContext> _creator;
public ActivatorDbContextCreator()
{
ShardingCoreHelper.CheckContextConstructors<TShardingDbContext>();
_creator = ShardingCoreHelper.CreateActivator<TShardingDbContext>();
}
public DbContext CreateDbContext(DbContext mainDbContext, ShardingDbContextOptions shardingDbContextOptions)
{
var dbContext = _creator(shardingDbContextOptions);
if (dbContext is IShardingTableDbContext shardingTableDbContext)
{
shardingTableDbContext.RouteTail = shardingDbContextOptions.RouteTail;
}
_ = dbContext.Model;
return dbContext;
}
}
}

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Sharding.Abstractions;
namespace ShardingCore.Core.DbContextCreator
{
/// <summary>
///
/// </summary>
/// Author: xjm
/// Created: 2022/4/2 21:12:17
/// Email: 326308290@qq.com
public interface IDbContextCreator
{
public DbContext CreateDbContext(DbContext mainDbContext, ShardingDbContextOptions shardingDbContextOptions);
}
public interface IDbContextCreator<TShardingDbContext> : IDbContextCreator
where TShardingDbContext : DbContext, IShardingDbContext
{
}
}

View File

@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
namespace ShardingCore.DbContexts.ShardingDbContexts
namespace ShardingCore.Core.DbContextCreator
{
/*
* @Author: xjm

View File

@ -107,7 +107,7 @@ namespace ShardingCore.Core.ShardingConfigurations.ConfigBuilders
services.AddSingleton(sp => CreateShardingConfigurationOptions(isMultiConfig, configurationStrategy));
services.AddSingleton<IShardingReadWriteAccessor, ShardingReadWriteAccessor<TShardingDbContext>>();
services.AddInternalShardingCore();
services.AddInternalShardingCore<TShardingDbContext>();
return services;
}

View File

@ -18,7 +18,6 @@ using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine;
using ShardingCore.DbContexts;
using ShardingCore.DIExtensions;
using ShardingCore.EFCores;
using ShardingCore.EFCores.OptionsExtensions;
@ -30,6 +29,7 @@ using ShardingCore.Sharding.ShardingQueryExecutors;
using ShardingCore.TableCreator;
using System;
using Microsoft.EntityFrameworkCore.Query;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Core.QueryTrackers;
using ShardingCore.Core.ShardingConfigurations;
using ShardingCore.Core.UnionAllMergeShardingProviders;
@ -79,7 +79,7 @@ namespace ShardingCore
public static ShardingCoreConfigBuilder<TShardingDbContext> AddShardingConfigure<TShardingDbContext>(this IServiceCollection services)
where TShardingDbContext : DbContext, IShardingDbContext
{
ShardingCoreHelper.CheckContextConstructors<TShardingDbContext>();
//ShardingCoreHelper.CheckContextConstructors<TShardingDbContext>();
return new ShardingCoreConfigBuilder<TShardingDbContext>(services);
}
@ -89,38 +89,36 @@ namespace ShardingCore
var connectionString = virtualDataSource.GetConnectionString(virtualDataSource.DefaultDataSourceName);
virtualDataSource.ConfigurationParams.UseDbContextOptionsBuilder(connectionString, dbContextOptionsBuilder).UseSharding<TShardingDbContext>();
}
internal static IServiceCollection AddInternalShardingCore(this IServiceCollection services)
internal static IServiceCollection AddInternalShardingCore<TShardingDbContext>(this IServiceCollection services) where TShardingDbContext : DbContext, IShardingDbContext
{
//虚拟数据源管理者
services.TryAddSingleton(typeof(IVirtualDataSourceManager<>), typeof(VirtualDataSourceManager<>));
services.TryAddSingleton<IVirtualDataSourceManager<TShardingDbContext>, VirtualDataSourceManager<TShardingDbContext>>();
services.TryAddSingleton<IVirtualDataSourceAccessor, VirtualDataSourceAccessor>();
//添加创建TActualDbContext创建者
services.TryAddSingleton(typeof(IShardingDbContextCreatorConfig<>), typeof(DefaultShardingDbContextCreatorConfig<>));
//分表dbcontext创建
services.TryAddSingleton<IDbContextCreator<TShardingDbContext>, ActivatorDbContextCreator<TShardingDbContext>>();
services.TryAddSingleton(typeof(IDataSourceInitializer<>), typeof(DataSourceInitializer<>));
services.TryAddSingleton(typeof(ITrackerManager<>), typeof(TrackerManager<>));
services.TryAddSingleton(typeof(IStreamMergeContextFactory<>), typeof(StreamMergeContextFactory<>));
services.TryAddSingleton(typeof(IShardingTableCreator<>), typeof(ShardingTableCreator<>));
services.TryAddSingleton<IDataSourceInitializer<TShardingDbContext>, DataSourceInitializer<TShardingDbContext>>();
services.TryAddSingleton<ITrackerManager<TShardingDbContext>, TrackerManager<TShardingDbContext>>();
services.TryAddSingleton<IStreamMergeContextFactory<TShardingDbContext>, StreamMergeContextFactory<TShardingDbContext>>();
services.TryAddSingleton<IShardingTableCreator<TShardingDbContext>, ShardingTableCreator<TShardingDbContext>>();
//虚拟数据源管理
services.TryAddSingleton(typeof(IVirtualDataSourceRouteManager<>), typeof(VirtualDataSourceRouteManager<>));
services.TryAddSingleton(typeof(IDataSourceRouteRuleEngine<>), typeof(DataSourceRouteRuleEngine<>));
services.TryAddSingleton(typeof(IDataSourceRouteRuleEngineFactory<>), typeof(DataSourceRouteRuleEngineFactory<>));
services.TryAddSingleton<IVirtualDataSourceRouteManager<TShardingDbContext>, VirtualDataSourceRouteManager<TShardingDbContext>>();
services.TryAddSingleton<IDataSourceRouteRuleEngine<TShardingDbContext>, DataSourceRouteRuleEngine<TShardingDbContext>>();
services.TryAddSingleton<IDataSourceRouteRuleEngineFactory<TShardingDbContext>, DataSourceRouteRuleEngineFactory<TShardingDbContext>>();
//读写分离链接创建工厂
services.TryAddSingleton<IReadWriteConnectorFactory, ReadWriteConnectorFactory>();
//虚拟表管理
services.TryAddSingleton(typeof(IVirtualTableManager<>), typeof(VirtualTableManager<>));
//分表dbcontext创建
services.TryAddSingleton(typeof(IShardingDbContextFactory<>), typeof(ShardingDbContextFactory<>));
services.TryAddSingleton<IVirtualTableManager<TShardingDbContext>, VirtualTableManager<TShardingDbContext>>();
//分表分库对象元信息管理
services.TryAddSingleton(typeof(IEntityMetadataManager<>), typeof(DefaultEntityMetadataManager<>));
services.TryAddSingleton<IEntityMetadataManager<TShardingDbContext>, DefaultEntityMetadataManager<TShardingDbContext>>();
//分表引擎
services.TryAddSingleton(typeof(ITableRouteRuleEngine<>), typeof(TableRouteRuleEngine<>));
services.TryAddSingleton<ITableRouteRuleEngineFactory<TShardingDbContext>, TableRouteRuleEngineFactory<TShardingDbContext>>();
services.TryAddSingleton<ITableRouteRuleEngine<TShardingDbContext>, TableRouteRuleEngine<TShardingDbContext>>();
//分表引擎工程
services.TryAddSingleton(typeof(ITableRouteRuleEngineFactory<>), typeof(TableRouteRuleEngineFactory<>));
services.TryAddSingleton(typeof(IParallelTableManager<>), typeof(ParallelTableManager<>));
services.TryAddSingleton<IParallelTableManager<TShardingDbContext>, ParallelTableManager<TShardingDbContext>>();
services.TryAddSingleton<IRouteTailFactory, RouteTailFactory>();
services.TryAddSingleton<IShardingComplierExecutor, DefaultShardingComplierExecutor>();
services.TryAddSingleton<IQueryCompilerContextFactory, QueryCompilerContextFactory>();

View File

@ -1,19 +0,0 @@
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.DbContexts.ShardingDbContexts;
using ShardingCore.Sharding.Abstractions;
namespace ShardingCore.DbContexts
{
/*
* @Author: xjm
* @Description:
* @Date: Thursday, 24 December 2020 08:22:23
* @Email: 326308290@qq.com
*/
public interface IShardingDbContextFactory<TShardingDbContext> where TShardingDbContext:DbContext,IShardingDbContext
{
DbContext Create(ShardingDbContextOptions shardingDbContextOptions);
DbContext Create(DbContextOptions dbContextOptions, IRouteTail routeTail);
}
}

View File

@ -1,40 +0,0 @@
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.DbContexts.ShardingDbContexts;
using ShardingCore.Sharding.Abstractions;
namespace ShardingCore.DbContexts
{
/*
* @Author: xjm
* @Description:
* @Date: Thursday, 24 December 2020 08:22:48
* @Email: 326308290@qq.com
*/
public class ShardingDbContextFactory<TShardingDbContext> : IShardingDbContextFactory<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
{
private readonly IShardingDbContextCreatorConfig<TShardingDbContext> _shardingDbContextCreatorConfig;
public ShardingDbContextFactory(IShardingDbContextCreatorConfig<TShardingDbContext> shardingDbContextCreatorConfig)
{
_shardingDbContextCreatorConfig = shardingDbContextCreatorConfig;
}
public DbContext Create(ShardingDbContextOptions shardingDbContextOptions)
{
var routeTail=shardingDbContextOptions.RouteTail;
var dbContext = _shardingDbContextCreatorConfig.Creator(shardingDbContextOptions);
if (dbContext is IShardingTableDbContext shardingTableDbContext)
{
shardingTableDbContext.RouteTail = routeTail;
}
_ = dbContext.Model;
return dbContext;
}
public DbContext Create(DbContextOptions dbContextOptions, IRouteTail routeTail)
{
return this.Create(new ShardingDbContextOptions(dbContextOptions, routeTail));
}
}
}

View File

@ -1,31 +0,0 @@
using Microsoft.EntityFrameworkCore;
using ShardingCore.DbContexts.ShardingDbContexts;
using ShardingCore.Helpers;
using ShardingCore.Sharding.Abstractions;
using System;
namespace ShardingCore
{
/*
* @Author: xjm
* @Description:
* @Date: 2021/3/5 17:30:10
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
public class DefaultShardingDbContextCreatorConfig<TShardingDbContext> : IShardingDbContextCreatorConfig<TShardingDbContext>
where TShardingDbContext : DbContext, IShardingDbContext
{
private readonly Func<ShardingDbContextOptions, DbContext> _creator;
public DefaultShardingDbContextCreatorConfig()
{
_creator = ShardingCoreHelper.CreateActivator<TShardingDbContext>();
}
public Type ShardingDbContextType => typeof(TShardingDbContext);
public DbContext Creator(ShardingDbContextOptions shardingDbContextOptions)
{
return _creator(shardingDbContextOptions);
}
}
}

View File

@ -2,7 +2,7 @@ using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using ShardingCore.DbContexts.ShardingDbContexts;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Sharding.Abstractions;
namespace ShardingCore.EFCores

View File

@ -9,7 +9,7 @@ using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.Core.VirtualTables;
using ShardingCore.DbContexts.ShardingDbContexts;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Extensions;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Utils;

View File

@ -7,7 +7,7 @@ using System.Reflection;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core;
using ShardingCore.Core.VirtualTables;
using ShardingCore.DbContexts.ShardingDbContexts;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Utils;

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
namespace ShardingCore.Extensions
{
/// <summary>
///
/// </summary>
/// Author: xjm
/// Created: 2022/4/3 15:15:11
/// Email: 326308290@qq.com
public static class IDbContextCreatorExtension
{
public static DbContext CreateDbContext(this IDbContextCreator dbContextCreator,DbContext mainDbContext, DbContextOptions dbContextOptions,
IRouteTail routeTail)
{
return dbContextCreator.CreateDbContext(mainDbContext,
new ShardingDbContextOptions(dbContextOptions, routeTail));
}
}
}

View File

@ -4,7 +4,7 @@ using System.Linq.Expressions;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core;
using ShardingCore.DbContexts.ShardingDbContexts;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;
using ShardingCore.Sharding.Abstractions;

View File

@ -1,22 +0,0 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.DbContexts.ShardingDbContexts;
using ShardingCore.Sharding.Abstractions;
namespace ShardingCore
{
/*
* @Author: xjm
* @Description:
* @Date: 2021/3/4 13:11:16
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
public interface IShardingDbContextCreatorConfig<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
{
DbContext Creator(ShardingDbContextOptions shardingDbContextOptions);
}
}

View File

@ -13,8 +13,7 @@ using Microsoft.Extensions.Logging;
using ShardingCore.Core;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.DbContexts;
using ShardingCore.DbContexts.ShardingDbContexts;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;
using ShardingCore.Infrastructures;
@ -34,7 +33,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
private static readonly IComparer<string> _comparer = new NoShardingFirstComparer();
public bool IsDefault { get; }
public int DbContextCount => _dataSourceDbContexts.Count;
private readonly IShardingDbContextFactory<TShardingDbContext> _shardingDbContextFactory;
private readonly IDbContextCreator<TShardingDbContext> _dbContextCreator;
private readonly ActualConnectionStringManager<TShardingDbContext> _actualConnectionStringManager;
private readonly IVirtualDataSource<TShardingDbContext> _virtualDataSource;
@ -68,19 +67,19 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
/// <param name="dataSourceName"></param>
/// <param name="isDefault"></param>
/// <param name="shardingDbContext"></param>
/// <param name="shardingDbContextFactory"></param>
/// <param name="dbContextCreator"></param>
/// <param name="actualConnectionStringManager"></param>
public DataSourceDbContext(string dataSourceName,
bool isDefault,
DbContext shardingDbContext,
IShardingDbContextFactory<TShardingDbContext> shardingDbContextFactory,
IDbContextCreator<TShardingDbContext> dbContextCreator,
ActualConnectionStringManager<TShardingDbContext> actualConnectionStringManager)
{
DataSourceName = dataSourceName;
IsDefault = isDefault;
_shardingDbContext = shardingDbContext;
_virtualDataSource = (IVirtualDataSource<TShardingDbContext>)((IShardingDbContext)shardingDbContext).GetVirtualDataSource();
_shardingDbContextFactory = shardingDbContextFactory;
_dbContextCreator = dbContextCreator;
_actualConnectionStringManager = actualConnectionStringManager;
_logger = ShardingContainer.GetService<ILogger<DataSourceDbContext<TShardingDbContext>>>();
@ -167,7 +166,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
var cacheKey = routeTail.GetRouteTailIdentity();
if (!_dataSourceDbContexts.TryGetValue(cacheKey, out var dbContext))
{
dbContext = _shardingDbContextFactory.Create(CreateShareDbContextOptionsBuilder(), routeTail);
dbContext = _dbContextCreator.CreateDbContext(_shardingDbContext,CreateShareDbContextOptionsBuilder(), routeTail);
_dataSourceDbContexts.Add(cacheKey, dbContext);
ShardingDbTransaction();
}

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.DbContexts.ShardingDbContexts;
using ShardingCore.Core.DbContextCreator;
namespace ShardingCore.Sharding.ShardingDbContextExecutors
{

View File

@ -11,8 +11,7 @@ using ShardingCore.Core.EntityMetadatas;
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
using ShardingCore.DbContexts;
using ShardingCore.DbContexts.ShardingDbContexts;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;
using ShardingCore.Sharding.Abstractions;
@ -38,7 +37,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
private readonly ConcurrentDictionary<string, IDataSourceDbContext> _dbContextCaches = new ConcurrentDictionary<string, IDataSourceDbContext>();
private readonly IVirtualDataSource<TShardingDbContext> _virtualDataSource;
private readonly IVirtualTableManager<TShardingDbContext> _virtualTableManager;
private readonly IShardingDbContextFactory<TShardingDbContext> _shardingDbContextFactory;
private readonly IDbContextCreator<TShardingDbContext> _dbContextCreator;
private readonly IRouteTailFactory _routeTailFactory;
private readonly ActualConnectionStringManager<TShardingDbContext> _actualConnectionStringManager;
private readonly IEntityMetadataManager<TShardingDbContext> _entityMetadataManager;
@ -62,7 +61,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
_shardingDbContext = shardingDbContext;
_virtualDataSource = ShardingContainer.GetRequiredCurrentVirtualDataSource<TShardingDbContext>();
_virtualTableManager = ShardingContainer.GetService<IVirtualTableManager<TShardingDbContext>>();
_shardingDbContextFactory = ShardingContainer.GetService<IShardingDbContextFactory<TShardingDbContext>>();
_dbContextCreator = ShardingContainer.GetService<IDbContextCreator<TShardingDbContext>>();
_entityMetadataManager = ShardingContainer.GetService<IEntityMetadataManager<TShardingDbContext>>();
_routeTailFactory = ShardingContainer.GetService<IRouteTailFactory>();
_actualConnectionStringManager = new ActualConnectionStringManager<TShardingDbContext>(_virtualDataSource);
@ -72,7 +71,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
private IDataSourceDbContext GetDataSourceDbContext(string dataSourceName)
{
return _dbContextCaches.GetOrAdd(dataSourceName, dsname => new DataSourceDbContext<TShardingDbContext>(dsname, _virtualDataSource.IsDefault(dsname), _shardingDbContext, _shardingDbContextFactory, _actualConnectionStringManager));
return _dbContextCaches.GetOrAdd(dataSourceName, dsname => new DataSourceDbContext<TShardingDbContext>(dsname, _virtualDataSource.IsDefault(dsname), _shardingDbContext, _dbContextCreator, _actualConnectionStringManager));
}
/// <summary>
@ -92,7 +91,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
else
{
var parallelDbContextOptions = CreateParallelDbContextOptions(dataSourceName);
var dbContext = _shardingDbContextFactory.Create(parallelDbContextOptions, routeTail);
var dbContext = _dbContextCreator.CreateDbContext(_shardingDbContext, parallelDbContextOptions, routeTail);
dbContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
return dbContext;
}