修复启动bug和表接口bug修复实现,还未实现同数据库事务实现

This commit is contained in:
xuejiaming 2021-03-10 14:17:04 +08:00
parent b052861b9d
commit 031795b0f1
9 changed files with 94 additions and 96 deletions

View File

@ -1,62 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Extensions;
namespace ShardingCore.DbContexts.ShardingDbContexts
{
/*
* @Author: xjm
* @Description:
* @Date: 2021/3/4 16:11:18
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
public abstract class AbstractShardingTableDbContext : DbContext
{
public string Tail { get; }
public Dictionary<Type, VirtualTableDbContextConfig> VirtualTableConfigs { get; }
protected AbstractShardingTableDbContext(ShardingDbContextOptions options):base(options.DbContextOptions)
{
Tail = options.Tail;
VirtualTableConfigs = options.VirtualTableDbContextConfigs.ToDictionary(o => o.ShardingEntityType, o => o);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
OnShardingModelCreating(modelBuilder);
OnModelCreatingAfter(modelBuilder);
}
protected abstract void OnShardingModelCreating(ModelBuilder modelBuilder);
protected virtual void OnModelCreatingAfter(ModelBuilder modelBuilder)
{
if (!string.IsNullOrWhiteSpace(Tail))
{
if (VirtualTableConfigs.IsNotEmpty())
{
var mutableEntityTypes = modelBuilder.Model.GetEntityTypes().Where(o => VirtualTableConfigs.ContainsKey(o.ClrType));
foreach (var entityType in mutableEntityTypes)
{
var virtualTableConfig = VirtualTableConfigs[entityType.ClrType];
var shardingEntity = virtualTableConfig.ShardingEntityType;
var tailPrefix = virtualTableConfig.TailPrefix;
var entity = modelBuilder.Entity(shardingEntity);
var tableName = virtualTableConfig.OriginalTableName;
if (string.IsNullOrWhiteSpace(tableName))
throw new ArgumentNullException($"{shardingEntity}: not found original table name。");
#if DEBUG
Console.WriteLine($"映射表:[tableName]-->[{tableName}{tailPrefix}{Tail}]");
#endif
entity.ToTable($"{tableName}{tailPrefix}{Tail}");
}
}
}
}
}
}

View File

@ -0,0 +1,17 @@
namespace ShardingCore.DbContexts.ShardingDbContexts
{
/*
* @Author: xjm
* @Description:
* @Date: 2021/03/09 00:00:00
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
/// <summary>
///
/// </summary>
public interface IShardingTableAccessor
{
}
}

View File

@ -0,0 +1,7 @@
namespace ShardingCore.DbContexts.ShardingDbContexts
{
public class IShardingTableDbContext
{
}
}

View File

@ -0,0 +1,17 @@
namespace ShardingCore.DbContexts.ShardingDbContexts
{
/*
* @Author: xjm
* @Description:
* @Date: 2021/03/09 00:00:00
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
/// <summary>
///
/// </summary>
public interface IShardingTableScopeFactory
{
}
}

View File

@ -0,0 +1,18 @@
namespace ShardingCore.DbContexts.ShardingDbContexts
{
/*
* @Author: xjm
* @Description:
* @Date: 2021/03/09 13:04:52
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
/// <summary>
///
/// </summary>
public class ShardingTableContext
{
}
}

View File

@ -0,0 +1,17 @@
namespace ShardingCore.DbContexts.ShardingDbContexts
{
/*
* @Author: xjm
* @Description:
* @Date: 2021/03/09 00:00:00
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
/// <summary>
///
/// </summary>
public interface ShardingTableScope
{
}
}

View File

@ -0,0 +1,18 @@
namespace ShardingCore.DbContexts.ShardingDbContexts
{
/*
* @Author: xjm
* @Description:
* @Date: 2021/03/09 13:13:58
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
/// <summary>
///
/// </summary>
public class ShardingTableScopeFactory
{
}
}

View File

@ -1,34 +0,0 @@
using System;
namespace ShardingCore.DbContexts.ShardingDbContexts
{
/*
* @Author: xjm
* @Description:
* @Date: Friday, 01 January 2021 16:24:57
* @Email: 326308290@qq.com
*/
public class VirtualTableDbContextConfig
{
public VirtualTableDbContextConfig(Type shardingEntityType, string originalTableName, string tailPrefix)
{
ShardingEntityType = shardingEntityType;
OriginalTableName = originalTableName;
TailPrefix = tailPrefix;
}
/// <summary>
/// 分表实体类型
/// </summary>
public Type ShardingEntityType { get; }
/// <summary>
/// 原始表名不带后缀
/// </summary>
public string OriginalTableName { get; }
/// <summary>
/// 表尾巴前缀
/// </summary>
public string TailPrefix { get; }
}
}