ow
This commit is contained in:
commit
f05d2d215f
678 files changed
+73865
No files matched your search
@@ -0,0 +1,89 @@
|
||||
using CloudBuilder.Core.Application.Builders;
|
||||
using CloudBuilder.Core.Application.Options;
|
||||
using CloudBuilder.Core.Application.Packs;
|
||||
using CloudBuilder.Core.DatabaseAccessor;
|
||||
using CloudBuilder.Core.DatabaseAccessor.Entity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.EFCore
|
||||
{
|
||||
[PackLevel(15)]
|
||||
public class EntityFrameworkPack : CloudBuilderPack
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取 模块级别
|
||||
/// </summary>
|
||||
public override PackLevel Level => PackLevel.Framework;
|
||||
|
||||
/// <summary>
|
||||
/// 获取 模块启动顺序,模块启动的顺序先按级别启动,级别内部再按此顺序启动
|
||||
/// </summary>
|
||||
public override int Order => 2;
|
||||
|
||||
/// <summary>
|
||||
/// 将注册实体类服务添加到依赖注入服务容器中
|
||||
/// 各模块然后自动注入实体类(含多关键字和无关键字)
|
||||
/// </summary>
|
||||
/// <param name="services">依赖注入服务容器</param>
|
||||
/// <returns></returns>
|
||||
public override IServiceCollection AddServices(IServiceCollection services, CloudBuilderBuilder builder)
|
||||
{
|
||||
var baseType = typeof(IMasterRegistEntityType<ModelBuilder>);
|
||||
|
||||
var allAssemblies = CloudBuilderBuilder.GetAssemblies();
|
||||
|
||||
var types = allAssemblies
|
||||
.SelectMany(a => a.DefinedTypes)
|
||||
.Select(type => type.AsType())
|
||||
.Where(type => type != baseType && baseType.IsAssignableFrom(type))
|
||||
.Where(x => x.IsClass).ToList();
|
||||
|
||||
foreach (var implementType in types)
|
||||
{
|
||||
services.AddSingleton(baseType, implementType);
|
||||
}
|
||||
|
||||
services.TryAddScoped(typeof(IRepository<>), typeof(Repository<>));
|
||||
services.TryAddScoped(typeof(IUnitOfWork), typeof(UnitOfWork));
|
||||
|
||||
//当你直接在控制器方法上使用[UnitOfWork] 特性时,ASP.NET Core 的过滤器管道会自动实例化这个特性,不需要手动注册。
|
||||
//如果你的 UnitOfWorkAttribute 没有构造函数依赖,那么不需要注册:
|
||||
services.TryAddScoped<UnitOfWorkAttribute>();
|
||||
|
||||
services.TryAddKeyedScoped<DbContext, SecondaryDbContext>(SecondaryDbContext.SECONDARY_DB_CONTEXT);
|
||||
services.TryAddKeyedScoped<DbContext, MasterDbContext>(MasterDbContext.MASTER_DB_CONTEXT);
|
||||
|
||||
services.AddDbContext<MasterDbContext>(opts =>
|
||||
{
|
||||
var connStr = builder.CloudBuilderOptions
|
||||
.GetDbContextOptions(MasterDbContext.MASTER_DB_CONTEXT)
|
||||
.ConnectionString;
|
||||
|
||||
opts.UseMySql(connStr, ServerVersion.AutoDetect(connStr));
|
||||
});
|
||||
|
||||
services.AddDbContext<SecondaryDbContext>(opts =>
|
||||
opts.UseSqlServer(builder.CloudBuilderOptions.GetDbContextOptions(SecondaryDbContext.SECONDARY_DB_CONTEXT).ConnectionString));
|
||||
|
||||
// services.AddDbContext<MasterDbContext>(opts =>
|
||||
//opts.UseSqlServer(builder.CloudBuilderOptions.GetDbContextOptions(MasterDbContext.MASTER_DB_CONTEXT).ConnectionString));
|
||||
|
||||
//services.AddDbContext<SecondaryDbContext>(opts =>
|
||||
//{
|
||||
// var connStr = builder.CloudBuilderOptions
|
||||
// .GetDbContextOptions(SecondaryDbContext.SECONDARY_DB_CONTEXT)
|
||||
// .ConnectionString;
|
||||
|
||||
// opts.UseMySql(connStr, ServerVersion.AutoDetect(connStr));
|
||||
//});
|
||||
|
||||
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.EFCore
|
||||
{
|
||||
/// <summary>
|
||||
/// 每一个模块注册实体类
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public interface IMasterRegistEntityType<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// 注入所有Entity实体类,调用AddEntityType和RegistKeys
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
void RegistEntity(T builder);
|
||||
|
||||
/// <summary>
|
||||
/// 如果Entity实体类不是单一主键,要单独重写注入(自动生成)
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
void RegistKeys(T builder);
|
||||
|
||||
/// <summary>
|
||||
/// 注入所有Entity实体类
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <param name="assemblyName"></param>
|
||||
void AddEntityType(T builder, string assemblyName);
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.EFCore
|
||||
{
|
||||
/// <summary>
|
||||
/// 每一个模块注册从库实体类
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public interface ISecondaryRegistEntityType<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// 注入所有Entity实体类,调用AddEntityType和RegistKeys
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
void RegistEntity(T builder);
|
||||
|
||||
/// <summary>
|
||||
/// 如果Entity实体类不是单一主键,要单独重写注入(自动生成)
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
void RegistKeys(T builder);
|
||||
|
||||
/// <summary>
|
||||
/// 注入所有Entity实体类
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <param name="assemblyName"></param>
|
||||
void AddEntityType(T builder, string assemblyName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using CloudBuilder.Core.Authorization;
|
||||
using CloudBuilder.Core.DatabaseAccessor.Entity;
|
||||
using CloudBuilder.Core.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.EFCore
|
||||
{
|
||||
public class RegistEntityHelper
|
||||
{
|
||||
public static void AddMasterEntityType(ModelBuilder builder, string _assemblyName)
|
||||
{
|
||||
var eType = typeof(IEntity<>);
|
||||
AddMasterEntityType(builder, _assemblyName, eType);
|
||||
}
|
||||
|
||||
public static void AddSecondaryEntityType(ModelBuilder builder, string _assemblyName)
|
||||
{
|
||||
var eType = typeof(ISecondary);
|
||||
AddMasterEntityType(builder, _assemblyName, eType);
|
||||
}
|
||||
|
||||
public static void AddMasterEntityType(ModelBuilder builder, string _assemblyName, Type eType)
|
||||
{
|
||||
var mType = typeof(IEntity<>);
|
||||
var sType = typeof(ISecondary);
|
||||
|
||||
var entityTypes = Assembly.Load(new AssemblyName(_assemblyName)).GetTypes()
|
||||
.Where(type => !string.IsNullOrWhiteSpace(type.Namespace))
|
||||
.Where(type => type.GetTypeInfo().IsClass)
|
||||
.Where(type => type.GetTypeInfo().BaseType != null)
|
||||
.Where(type => type.HasImplementedRawGeneric(eType)).ToList();
|
||||
|
||||
if (eType == mType) entityTypes = entityTypes.Where(type => !type.HasImplementedRawGeneric(sType)).ToList();
|
||||
|
||||
NotMappedAttribute notMapped = null;
|
||||
foreach (var entityType in entityTypes)
|
||||
{
|
||||
//排除实体类标记[NotMapped]
|
||||
notMapped = entityType.GetCustomAttribute<NotMappedAttribute>();
|
||||
if (notMapped != null) continue;
|
||||
|
||||
if (builder.Model.FindEntityType(entityType) != null)
|
||||
continue;
|
||||
builder.Model.AddEntityType(entityType);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user