From fe5199cd438f42dd3d3d037070bf0e496ffbabdd Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Fri, 24 May 2019 17:09:59 +0300 Subject: [PATCH] Fix ExposedServiceExplorer. --- .../ExposeServicesAttribute.cs | 36 +++++++++++++------ .../ExposedServiceExplorer.cs | 7 ++++ .../AutoRegistrationHelper_Tests.cs | 4 +-- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ExposeServicesAttribute.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ExposeServicesAttribute.cs index ed2b1b8d80..ad808e6026 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ExposeServicesAttribute.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ExposeServicesAttribute.cs @@ -7,29 +7,45 @@ namespace Volo.Abp.DependencyInjection { public class ExposeServicesAttribute : Attribute, IExposedServiceTypesProvider { - public Type[] ExposedServiceTypes { get; } - - public ExposeServicesAttribute(params Type[] exposedServiceTypes) + public Type[] ServiceTypes { get; } + + public bool? IncludeDefaults { get; set; } + + public bool? IncludeSelf { get; set; } + + public ExposeServicesAttribute(params Type[] serviceTypes) { - ExposedServiceTypes = exposedServiceTypes ?? new Type[0]; + ServiceTypes = serviceTypes ?? new Type[0]; } public Type[] GetExposedServiceTypes(Type targetType) { - if (ExposedServiceTypes.Any()) + var serviceList = ServiceTypes.ToList(); + + if (IncludeDefaults == true) + { + foreach (var type in GetDefaultServices(targetType)) + { + serviceList.AddIfNotContains(type); + } + + if (IncludeSelf != false) + { + serviceList.AddIfNotContains(targetType); + } + } + else if (IncludeSelf == true) { - return ExposedServiceTypes; + serviceList.AddIfNotContains(targetType); } - return GetDefaultExposedServices(targetType).ToArray(); + return serviceList.ToArray(); } - private static List GetDefaultExposedServices(Type type) + private static List GetDefaultServices(Type type) { var serviceTypes = new List(); - serviceTypes.Add(type); - foreach (var interfaceType in type.GetTypeInfo().GetInterfaces()) { var interfaceName = interfaceType.Name; diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ExposedServiceExplorer.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ExposedServiceExplorer.cs index 79fb77d6ff..f7324014d4 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ExposedServiceExplorer.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ExposedServiceExplorer.cs @@ -7,11 +7,18 @@ namespace Volo.Abp.DependencyInjection { public static class ExposedServiceExplorer { + private static readonly ExposeServicesAttribute DefaultExposeServicesAttribute = + new ExposeServicesAttribute + { + IncludeDefaults = true + }; + public static List GetExposedServices(Type type) { return type .GetCustomAttributes() .OfType() + .DefaultIfEmpty(DefaultExposeServicesAttribute) .SelectMany(p => p.GetExposedServiceTypes(type)) .ToList(); } diff --git a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DependencyInjection/AutoRegistrationHelper_Tests.cs b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DependencyInjection/AutoRegistrationHelper_Tests.cs index f9d2950d42..16dec3ea7d 100644 --- a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DependencyInjection/AutoRegistrationHelper_Tests.cs +++ b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DependencyInjection/AutoRegistrationHelper_Tests.cs @@ -1,6 +1,4 @@ -using Microsoft.Extensions.DependencyInjection; -using Shouldly; -using System.Linq; +using Shouldly; using Xunit; namespace Volo.Abp.DependencyInjection