feat: 完善种子数据功能
This commit is contained in:
@@ -9,7 +9,7 @@ namespace Yi.Framework.Core.Attributes
|
|||||||
/// 1、[AppService]:自动去找接口,如果存在就是接口,如果不存在就是本身
|
/// 1、[AppService]:自动去找接口,如果存在就是接口,如果不存在就是本身
|
||||||
/// 2、[AppService(ServiceType = typeof(注册抽象或者接口或者本身))],手动去注册,放type即可
|
/// 2、[AppService(ServiceType = typeof(注册抽象或者接口或者本身))],手动去注册,放type即可
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
|
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
|
||||||
public class AppServiceAttribute : Attribute
|
public class AppServiceAttribute : Attribute
|
||||||
{
|
{
|
||||||
public AppServiceAttribute() { }
|
public AppServiceAttribute() { }
|
||||||
|
|||||||
@@ -46,42 +46,53 @@ namespace Yi.Framework.Core.Extensions
|
|||||||
|
|
||||||
private static void RegIocByAttribute(IServiceCollection services, Type type)
|
private static void RegIocByAttribute(IServiceCollection services, Type type)
|
||||||
{
|
{
|
||||||
var serviceAttribute = type.GetCustomAttribute<AppServiceAttribute>();
|
var serviceAttributes = type.GetCustomAttributes<AppServiceAttribute>();
|
||||||
if (serviceAttribute is not null)
|
if (serviceAttributes is null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//处理多个特性注入情况
|
||||||
|
foreach (var serviceAttribute in serviceAttributes)
|
||||||
{
|
{
|
||||||
//泛型类需要单独进行处理
|
|
||||||
//情况1:使用自定义[AppService(ServiceType = typeof(注册抽象或者接口))],手动去注册,放type即可
|
|
||||||
var serviceType = serviceAttribute.ServiceType;
|
|
||||||
//情况2 自动去找接口,如果存在就是接口,如果不存在就是本身
|
|
||||||
if (serviceType == null)
|
|
||||||
{
|
|
||||||
//获取最远靠近的接口
|
|
||||||
var p = type.GetInterfaces().ToList();
|
|
||||||
var firstInter = type.GetInterfaces().Where(u => u.Name ==$"I{type.Name}").LastOrDefault();
|
|
||||||
if (firstInter is null)
|
|
||||||
{
|
|
||||||
serviceType = type;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
serviceType = firstInter;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (serviceAttribute.ServiceLifetime)
|
if (serviceAttribute is not null)
|
||||||
{
|
{
|
||||||
case LifeTime.Singleton:
|
//泛型类需要单独进行处理
|
||||||
services.AddSingleton(serviceType, type);
|
//情况1:使用自定义[AppService(ServiceType = typeof(注册抽象或者接口))],手动去注册,放type即可
|
||||||
break;
|
var serviceType = serviceAttribute.ServiceType;
|
||||||
case LifeTime.Scoped:
|
//情况2 自动去找接口,如果存在就是接口,如果不存在就是本身
|
||||||
services.AddScoped(serviceType, type);
|
if (serviceType == null)
|
||||||
break;
|
{
|
||||||
case LifeTime.Transient:
|
//获取最远靠近的接口
|
||||||
services.AddTransient(serviceType, type);
|
var p = type.GetInterfaces().ToList();
|
||||||
break;
|
var firstInter = type.GetInterfaces().Where(u => u.Name == $"I{type.Name}").LastOrDefault();
|
||||||
|
if (firstInter is null)
|
||||||
|
{
|
||||||
|
serviceType = type;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
serviceType = firstInter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (serviceAttribute.ServiceLifetime)
|
||||||
|
{
|
||||||
|
case LifeTime.Singleton:
|
||||||
|
services.AddSingleton(serviceType, type);
|
||||||
|
break;
|
||||||
|
case LifeTime.Scoped:
|
||||||
|
services.AddScoped(serviceType, type);
|
||||||
|
break;
|
||||||
|
case LifeTime.Transient:
|
||||||
|
services.AddTransient(serviceType, type);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void RegIocByInterface(IServiceCollection services, Type type)
|
private static void RegIocByInterface(IServiceCollection services, Type type)
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ using Yi.Framework.Ddd.Repositories;
|
|||||||
|
|
||||||
namespace Yi.Framework.Data.DataSeeds
|
namespace Yi.Framework.Data.DataSeeds
|
||||||
{
|
{
|
||||||
public abstract class AbstractDataSeed<TEntity> : IDataSeed
|
public abstract class AbstractDataSeed<TEntity> : IDataSeed<TEntity>
|
||||||
{
|
{
|
||||||
private readonly IRepository<TEntity> _repository;
|
private readonly IRepository<TEntity> _repository;
|
||||||
public AbstractDataSeed(IRepository<TEntity> repository)
|
public AbstractDataSeed(IRepository<TEntity> repository)
|
||||||
|
|||||||
@@ -10,4 +10,8 @@ namespace Yi.Framework.Data.DataSeeds
|
|||||||
{
|
{
|
||||||
Task<bool> InvokerAsync();
|
Task<bool> InvokerAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface IDataSeed<TEntity>: IDataSeed
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ using Yi.Framework.Ddd.Services;
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Yi.BBS.Application.Contracts.GlobalSetting.Dtos.Temp;
|
using Yi.BBS.Application.Contracts.GlobalSetting.Dtos.Temp;
|
||||||
using Yi.BBS.Domain.Shared;
|
using Yi.BBS.Domain.Shared;
|
||||||
|
using Yi.Framework.Data.DataSeeds;
|
||||||
|
using Yi.RBAC.Domain.Identity.Entities;
|
||||||
|
|
||||||
namespace Yi.BBS.Application.GlobalSetting
|
namespace Yi.BBS.Application.GlobalSetting
|
||||||
{
|
{
|
||||||
@@ -14,6 +16,10 @@ namespace Yi.BBS.Application.GlobalSetting
|
|||||||
[AppService]
|
[AppService]
|
||||||
public class TempService : ApplicationService, IAutoApiService
|
public class TempService : ApplicationService, IAutoApiService
|
||||||
{
|
{
|
||||||
|
|
||||||
|
public TempService(IDataSeed<UserEntity> dataSeed) {
|
||||||
|
dataSeed.InvokerAsync().Wait();
|
||||||
|
}
|
||||||
///// <summary>
|
///// <summary>
|
||||||
///// 登录
|
///// 登录
|
||||||
///// </summary>
|
///// </summary>
|
||||||
|
|||||||
@@ -11,6 +11,10 @@ using Yi.RBAC.Domain.Shared.Identity.EnumClasses;
|
|||||||
|
|
||||||
namespace Yi.RBAC.Domain.DataSeeds
|
namespace Yi.RBAC.Domain.DataSeeds
|
||||||
{
|
{
|
||||||
|
//支持依赖注入执行
|
||||||
|
[AppService(typeof(IDataSeed<>))]
|
||||||
|
|
||||||
|
//支持启动时执行
|
||||||
[AppService(typeof(IDataSeed))]
|
[AppService(typeof(IDataSeed))]
|
||||||
public class UserDataSeed : AbstractDataSeed<UserEntity>
|
public class UserDataSeed : AbstractDataSeed<UserEntity>
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user