feat: 完善种子数据功能

This commit is contained in:
陈淳
2023-03-03 18:12:44 +08:00
parent f93cccd849
commit 834e40d6f2
6 changed files with 57 additions and 32 deletions

View File

@@ -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() { }

View File

@@ -46,7 +46,15 @@ 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 (serviceAttributes is null)
{
return;
}
//处理多个特性注入情况
foreach (var serviceAttribute in serviceAttributes)
{
if (serviceAttribute is not null) if (serviceAttribute is not null)
{ {
//泛型类需要单独进行处理 //泛型类需要单独进行处理
@@ -57,7 +65,7 @@ namespace Yi.Framework.Core.Extensions
{ {
//获取最远靠近的接口 //获取最远靠近的接口
var p = type.GetInterfaces().ToList(); var p = type.GetInterfaces().ToList();
var firstInter = type.GetInterfaces().Where(u => u.Name ==$"I{type.Name}").LastOrDefault(); var firstInter = type.GetInterfaces().Where(u => u.Name == $"I{type.Name}").LastOrDefault();
if (firstInter is null) if (firstInter is null)
{ {
serviceType = type; serviceType = type;
@@ -82,6 +90,9 @@ namespace Yi.Framework.Core.Extensions
} }
} }
}
} }
private static void RegIocByInterface(IServiceCollection services, Type type) private static void RegIocByInterface(IServiceCollection services, Type type)

View File

@@ -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)

View File

@@ -10,4 +10,8 @@ namespace Yi.Framework.Data.DataSeeds
{ {
Task<bool> InvokerAsync(); Task<bool> InvokerAsync();
} }
public interface IDataSeed<TEntity>: IDataSeed
{
}
} }

View File

@@ -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>

View File

@@ -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>
{ {