sharding/src/ShardingCore/DbContexts/ShardingDbContextFactory.cs

65 lines
2.6 KiB
C#
Raw Normal View History

using System;
2021-08-15 11:07:03 +08:00
using System.Data.Common;
using System.Linq;
using Microsoft.EntityFrameworkCore;
2021-03-13 08:15:08 +08:00
using Microsoft.Extensions.DependencyInjection;
2021-03-08 14:59:32 +08:00
using ShardingCore.Core.VirtualTables;
2021-03-13 08:34:35 +08:00
using ShardingCore.DbContexts.Abstractions;
2021-01-26 10:18:49 +08:00
using ShardingCore.DbContexts.ShardingDbContexts;
2021-03-08 14:59:32 +08:00
using ShardingCore.DbContexts.VirtualDbContexts;
using ShardingCore.Extensions;
2021-08-15 11:07:03 +08:00
using ShardingCore.Sharding.Abstractions;
2021-01-26 10:18:49 +08:00
namespace ShardingCore.DbContexts
{
/*
* @Author: xjm
* @Description:
* @Date: Thursday, 24 December 2020 08:22:48
* @Email: 326308290@qq.com
*/
public class ShardingDbContextFactory:IShardingDbContextFactory
{
private readonly IShardingCoreOptions _shardingCoreOptions;
private readonly IShardingTableScopeFactory _shardingTableScopeFactory;
2021-03-13 08:34:35 +08:00
private readonly IDbContextCreateFilterManager _dbContextCreateFilterManager;
2021-08-15 07:32:50 +08:00
private readonly IDbContextOptionsProvider _dbContextOptionsProvider;
2021-08-15 07:32:50 +08:00
public ShardingDbContextFactory(IShardingCoreOptions shardingCoreOptions,IShardingTableScopeFactory shardingTableScopeFactory, IDbContextCreateFilterManager dbContextCreateFilterManager,IDbContextOptionsProvider dbContextOptionsProvider)
{
_shardingCoreOptions = shardingCoreOptions;
_shardingTableScopeFactory = shardingTableScopeFactory;
2021-03-13 08:34:35 +08:00
_dbContextCreateFilterManager = dbContextCreateFilterManager;
2021-08-15 07:32:50 +08:00
_dbContextOptionsProvider = dbContextOptionsProvider;
}
2021-08-15 07:32:50 +08:00
public DbContext Create(ShardingDbContextOptions shardingDbContextOptions)
2021-01-26 10:18:49 +08:00
{
2021-08-15 21:31:59 +08:00
var tail=shardingDbContextOptions.Tail;
2021-08-15 07:32:50 +08:00
var shardingConfigEntry = _shardingCoreOptions.GetShardingConfig();
2021-08-15 21:31:59 +08:00
var dbContext = shardingConfigEntry.Creator(shardingDbContextOptions);
if (!string.IsNullOrWhiteSpace(tail) && dbContext is IShardingTableDbContext shardingTableDbContext)
{
2021-08-15 21:31:59 +08:00
shardingTableDbContext.SetShardingTableDbContextTail(tail);
}
2021-03-13 08:15:08 +08:00
2021-08-15 21:31:59 +08:00
var filters = _dbContextCreateFilterManager.GetFilters();
if (filters.Any())
{
foreach (var dbContextCreateFilter in filters)
2021-03-13 08:15:08 +08:00
{
2021-08-15 21:31:59 +08:00
dbContextCreateFilter.CreateAfter(dbContext);
2021-03-13 08:15:08 +08:00
}
}
2021-08-15 21:31:59 +08:00
var dbContextModel = dbContext.Model;
return dbContext;
2021-01-26 10:18:49 +08:00
}
2021-03-08 14:59:32 +08:00
2021-08-15 11:07:03 +08:00
public DbContext Create(DbConnection dbConnection,string tail)
2021-03-08 14:59:32 +08:00
{
var shardingDbContextOptions =
2021-08-15 11:07:03 +08:00
new ShardingDbContextOptions(_dbContextOptionsProvider.GetDbContextOptions(dbConnection), tail);
2021-08-15 07:32:50 +08:00
return Create(shardingDbContextOptions);
2021-03-08 14:59:32 +08:00
}
2021-01-26 10:18:49 +08:00
}
}