修改对DbContext创建后的事件
This commit is contained in:
parent
77c733b4b0
commit
f76ed78dca
|
@ -1,8 +1,8 @@
|
|||
:start
|
||||
::定义版本
|
||||
set EFCORE2=2.0.0.4
|
||||
set EFCORE3=3.0.0.4
|
||||
set EFCORE5=5.0.0.4
|
||||
set EFCORE2=2.1.0.0
|
||||
set EFCORE3=3.1.0.0
|
||||
set EFCORE5=5.1.0.0
|
||||
|
||||
::删除所有bin与obj下的文件
|
||||
@echo off
|
||||
|
|
|
@ -4,6 +4,7 @@ using System.Linq;
|
|||
using System.Text;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes;
|
||||
using ShardingCore.DbContexts.Abstractions;
|
||||
using ShardingCore.DbContexts.ShardingDbContexts;
|
||||
using ShardingCore.Exceptions;
|
||||
using ShardingCore.Extensions;
|
||||
|
@ -103,5 +104,22 @@ namespace ShardingCore
|
|||
/// 是否需要在启动时创建分表
|
||||
/// </summary>
|
||||
public bool? CreateShardingTableOnStart { get; set; }
|
||||
|
||||
public readonly List<Type> _filters = new List<Type>();
|
||||
/// <summary>
|
||||
/// 添加filter过滤器
|
||||
/// </summary>
|
||||
/// <typeparam name="TFilter"></typeparam>
|
||||
public void AddDbContextCreateFilter<TFilter>() where TFilter : class, IDbContextCreateFilter
|
||||
{
|
||||
if (_filters.Contains(typeof(TFilter)))
|
||||
throw new ArgumentException("请勿重复添加DbContextCreateFilter");
|
||||
_filters.Add(typeof(TFilter));
|
||||
}
|
||||
|
||||
public List<Type> GetFilters()
|
||||
{
|
||||
return _filters;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ using ShardingCore.Core.VirtualDataSources;
|
|||
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RoutingRuleEngine;
|
||||
using ShardingCore.Core.VirtualTables;
|
||||
using ShardingCore.DbContexts;
|
||||
using ShardingCore.DbContexts.Abstractions;
|
||||
using ShardingCore.DbContexts.ShardingDbContexts;
|
||||
using ShardingCore.DbContexts.VirtualDbContexts;
|
||||
using ShardingCore.TableCreator;
|
||||
|
@ -24,6 +25,7 @@ namespace ShardingCore
|
|||
|
||||
public static IServiceCollection AddShardingCore(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IDbContextCreateFilterManager, DbContextCreateFilterManager>();
|
||||
services.AddSingleton<IStreamMergeContextFactory, StreamMergeContextFactory>();
|
||||
services.AddScoped<IVirtualDbContext, VirtualDbContext>();
|
||||
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace ShardingCore.DbContexts.Abstractions
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2021/3/13 8:19:26
|
||||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class DbContextCreateFilterManager: IDbContextCreateFilterManager
|
||||
{
|
||||
private readonly List<IDbContextCreateFilter> _filters = new List<IDbContextCreateFilter>();
|
||||
public void RegisterFilter(IDbContextCreateFilter filter)
|
||||
{
|
||||
if (null == filter)
|
||||
throw new ArgumentNullException(nameof(filter));
|
||||
if(!_filters.Contains(filter))
|
||||
_filters.Add(filter);
|
||||
}
|
||||
|
||||
public List<IDbContextCreateFilter> GetFilters()
|
||||
{
|
||||
return _filters;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ShardingCore.DbContexts.Abstractions
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2021/3/13 8:17:41
|
||||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public interface IDbContextCreateFilter
|
||||
{
|
||||
/// <summary>
|
||||
/// dbContext创建完成后
|
||||
/// </summary>
|
||||
/// <param name="dbContext"></param>
|
||||
/// <param name="s"></param>
|
||||
void CreateAfter(DbContext dbContext, IServiceProvider s);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ShardingCore.DbContexts.Abstractions
|
||||
{
|
||||
|
@ -10,6 +11,8 @@ namespace ShardingCore.DbContexts.Abstractions
|
|||
*/
|
||||
public interface IDbContextCreateFilterManager
|
||||
{
|
||||
|
||||
|
||||
void RegisterFilter(IDbContextCreateFilter filter);
|
||||
List<IDbContextCreateFilter> GetFilters();
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@ using System;
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ShardingCore.Core.VirtualTables;
|
||||
using ShardingCore.DbContexts.Abstractions;
|
||||
using ShardingCore.DbContexts.ShardingDbContexts;
|
||||
using ShardingCore.DbContexts.VirtualDbContexts;
|
||||
using ShardingCore.Extensions;
|
||||
|
@ -17,14 +18,14 @@ namespace ShardingCore.DbContexts
|
|||
public class ShardingDbContextFactory:IShardingDbContextFactory
|
||||
{
|
||||
private readonly IShardingCoreOptions _shardingCoreOptions;
|
||||
private readonly IVirtualTableManager _virtualTableManager;
|
||||
private readonly IShardingTableScopeFactory _shardingTableScopeFactory;
|
||||
private readonly IDbContextCreateFilterManager _dbContextCreateFilterManager;
|
||||
|
||||
public ShardingDbContextFactory(IShardingCoreOptions shardingCoreOptions,IVirtualTableManager virtualTableManager,IShardingTableScopeFactory shardingTableScopeFactory)
|
||||
public ShardingDbContextFactory(IShardingCoreOptions shardingCoreOptions,IShardingTableScopeFactory shardingTableScopeFactory, IDbContextCreateFilterManager dbContextCreateFilterManager)
|
||||
{
|
||||
_shardingCoreOptions = shardingCoreOptions;
|
||||
_virtualTableManager = virtualTableManager;
|
||||
_shardingTableScopeFactory = shardingTableScopeFactory;
|
||||
_dbContextCreateFilterManager = dbContextCreateFilterManager;
|
||||
}
|
||||
public DbContext Create(string connectKey, ShardingDbContextOptions shardingDbContextOptions,IServiceProvider serviceProvider)
|
||||
{
|
||||
|
@ -40,18 +41,21 @@ namespace ShardingCore.DbContexts
|
|||
modelChangeKey = $"sharding_{tail}";
|
||||
}
|
||||
scope.ShardingTableAccessor.Context = ShardingTableContext.Create(connectKey,tail);
|
||||
var dbcontext= shardingConfigEntry.Creator(shardingDbContextOptions);
|
||||
if (modelChangeKey != null&&dbcontext is IShardingTableDbContext shardingTableDbContext)
|
||||
var dbContext= shardingConfigEntry.Creator(shardingDbContextOptions);
|
||||
if (modelChangeKey != null&& dbContext is IShardingTableDbContext shardingTableDbContext)
|
||||
{
|
||||
shardingTableDbContext.ModelChangeKey = modelChangeKey;
|
||||
}
|
||||
|
||||
if (serviceProvider != null)
|
||||
{
|
||||
|
||||
foreach (var dbContextCreateFilter in _dbContextCreateFilterManager.GetFilters())
|
||||
{
|
||||
dbContextCreateFilter.CreateAfter(dbContext, serviceProvider);
|
||||
}
|
||||
}
|
||||
var dbContextModel = dbcontext.Model;
|
||||
return dbcontext;
|
||||
var dbContextModel = dbContext.Model;
|
||||
return dbContext;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||
using System.Text;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes;
|
||||
using ShardingCore.DbContexts.Abstractions;
|
||||
using ShardingCore.DbContexts.ShardingDbContexts;
|
||||
|
||||
namespace ShardingCore
|
||||
|
@ -41,5 +42,13 @@ namespace ShardingCore
|
|||
/// </summary>
|
||||
bool? CreateShardingTableOnStart { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 添加filter过滤器
|
||||
/// </summary>
|
||||
/// <typeparam name="TFilter"></typeparam>
|
||||
public void AddDbContextCreateFilter<TFilter>() where TFilter : class, IDbContextCreateFilter;
|
||||
|
||||
public List<Type> GetFilters();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ using ShardingCore.Core.VirtualRoutes.DataSourceRoutes;
|
|||
using ShardingCore.Core.VirtualRoutes.TableRoutes;
|
||||
using ShardingCore.Core.VirtualTables;
|
||||
using ShardingCore.DbContexts;
|
||||
using ShardingCore.DbContexts.Abstractions;
|
||||
using ShardingCore.DbContexts.ShardingDbContexts;
|
||||
using ShardingCore.DbContexts.VirtualDbContexts;
|
||||
using ShardingCore.Exceptions;
|
||||
|
@ -39,11 +40,12 @@ namespace ShardingCore
|
|||
private readonly IShardingTableCreator _tableCreator;
|
||||
private readonly ILogger<ShardingBootstrapper> _logger;
|
||||
private readonly IShardingDbContextFactory _shardingDbContextFactory;
|
||||
private readonly IDbContextCreateFilterManager _dbContextCreateFilterManager;
|
||||
|
||||
public ShardingBootstrapper(IServiceProvider serviceProvider, IShardingCoreOptions shardingCoreOptions,
|
||||
IVirtualDataSourceManager virtualDataSourceManager, IVirtualTableManager virtualTableManager
|
||||
, IShardingTableCreator tableCreator, ILogger<ShardingBootstrapper> logger,
|
||||
IShardingDbContextFactory shardingDbContextFactory)
|
||||
IShardingDbContextFactory shardingDbContextFactory,IDbContextCreateFilterManager dbContextCreateFilterManager)
|
||||
{
|
||||
ShardingContainer.SetServices(serviceProvider);
|
||||
_serviceProvider = serviceProvider;
|
||||
|
@ -53,10 +55,15 @@ namespace ShardingCore
|
|||
_tableCreator = tableCreator;
|
||||
_logger = logger;
|
||||
_shardingDbContextFactory = shardingDbContextFactory;
|
||||
_dbContextCreateFilterManager = dbContextCreateFilterManager;
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
foreach (var filter in _shardingCoreOptions.GetFilters())
|
||||
{
|
||||
_dbContextCreateFilterManager.RegisterFilter((IDbContextCreateFilter)Activator.CreateInstance(filter));
|
||||
}
|
||||
EnsureCreated();
|
||||
//_shardingCoreOptions.GetShardingConfigs().Select(o=>o.ConnectKey).ForEach(connectKey=> _virtualDataSourceManager.AddShardingConnectKey(connectKey));
|
||||
var isShardingDataSource = _shardingCoreOptions.GetShardingConfigs().Count > 1;
|
||||
|
|
Loading…
Reference in New Issue