diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Attributes/AppServiceAttribute.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Attributes/AppServiceAttribute.cs index a398b51a..59beb884 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Attributes/AppServiceAttribute.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Attributes/AppServiceAttribute.cs @@ -9,7 +9,7 @@ namespace Yi.Framework.Core.Attributes /// 1、[AppService]:自动去找接口,如果存在就是接口,如果不存在就是本身 /// 2、[AppService(ServiceType = typeof(注册抽象或者接口或者本身))],手动去注册,放type即可 /// - [AttributeUsage(AttributeTargets.Class, Inherited = false)] + [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)] public class AppServiceAttribute : Attribute { public AppServiceAttribute() { } diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Extensions/AutoIocAddExtensions.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Extensions/AutoIocAddExtensions.cs index e303196e..e2f8fe63 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Extensions/AutoIocAddExtensions.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Extensions/AutoIocAddExtensions.cs @@ -46,42 +46,53 @@ namespace Yi.Framework.Core.Extensions private static void RegIocByAttribute(IServiceCollection services, Type type) { - var serviceAttribute = type.GetCustomAttribute(); - if (serviceAttribute is not null) + var serviceAttributes = type.GetCustomAttributes(); + 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) diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Data/DataSeeds/AbstractDataSeed.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Data/DataSeeds/AbstractDataSeed.cs index 02fe3342..1992122f 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Data/DataSeeds/AbstractDataSeed.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Data/DataSeeds/AbstractDataSeed.cs @@ -7,7 +7,7 @@ using Yi.Framework.Ddd.Repositories; namespace Yi.Framework.Data.DataSeeds { - public abstract class AbstractDataSeed : IDataSeed + public abstract class AbstractDataSeed : IDataSeed { private readonly IRepository _repository; public AbstractDataSeed(IRepository repository) diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Data/DataSeeds/IDataSeed.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Data/DataSeeds/IDataSeed.cs index fdf12f75..22dc95ff 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Data/DataSeeds/IDataSeed.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Data/DataSeeds/IDataSeed.cs @@ -10,4 +10,8 @@ namespace Yi.Framework.Data.DataSeeds { Task InvokerAsync(); } + + public interface IDataSeed: IDataSeed + { + } } diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application/GlobalSetting/TempService.cs b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application/GlobalSetting/TempService.cs index e2e04b84..83d4f30f 100644 --- a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application/GlobalSetting/TempService.cs +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application/GlobalSetting/TempService.cs @@ -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 dataSeed) { + dataSeed.InvokerAsync().Wait(); + } ///// ///// 登录 ///// diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/DataSeeds/UserDataSeed.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/DataSeeds/UserDataSeed.cs index b16193da..5228412c 100644 --- a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/DataSeeds/UserDataSeed.cs +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/DataSeeds/UserDataSeed.cs @@ -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 {