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