From a975f28c95a45d4d9b16108fc8d93270c2a098a4 Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Fri, 24 May 2019 16:39:15 +0300 Subject: [PATCH] Refactor exposed services. --- .../AutoRegistrationHelper.cs | 53 ------------------- .../DefaultConventionalRegistrar.cs | 2 +- .../ExposeServicesAttribute.cs | 36 ++++++++++++- .../ExposedServiceExplorer.cs | 19 +++++++ .../AutoRegistrationHelper_Tests.cs | 4 +- 5 files changed, 56 insertions(+), 58 deletions(-) delete mode 100644 framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/AutoRegistrationHelper.cs create mode 100644 framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ExposedServiceExplorer.cs diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/AutoRegistrationHelper.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/AutoRegistrationHelper.cs deleted file mode 100644 index 7d0ce0a39e..0000000000 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/AutoRegistrationHelper.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using Microsoft.Extensions.DependencyInjection; - -namespace Volo.Abp.DependencyInjection -{ - public static class AutoRegistrationHelper - { - public static List GetExposedServices(IServiceCollection services, Type type) - { - var typeInfo = type.GetTypeInfo(); - - var customExposedServices = typeInfo - .GetCustomAttributes() - .OfType() - .SelectMany(p => p.GetExposedServiceTypes(type)) - .ToList(); - - if (customExposedServices.Any()) - { - return customExposedServices; - } - - return GetDefaultExposedServices(type); - } - - private static List GetDefaultExposedServices(Type type) - { - var serviceTypes = new List(); - - serviceTypes.Add(type); - - foreach (var interfaceType in type.GetTypeInfo().GetInterfaces()) - { - var interfaceName = interfaceType.Name; - - if (interfaceName.StartsWith("I")) - { - interfaceName = interfaceName.Right(interfaceName.Length - 1); - } - - if (type.Name.EndsWith(interfaceName)) - { - serviceTypes.Add(interfaceType); - } - } - - return serviceTypes; - } - } -} diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/DefaultConventionalRegistrar.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/DefaultConventionalRegistrar.cs index b76c37682d..9c5ee06d81 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/DefaultConventionalRegistrar.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/DefaultConventionalRegistrar.cs @@ -26,7 +26,7 @@ namespace Volo.Abp.DependencyInjection return; } - var serviceTypes = AutoRegistrationHelper.GetExposedServices(services, type); + var serviceTypes = ExposedServiceExplorer.GetExposedServices(type); TriggerServiceExposing(services, type, serviceTypes); 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 f46be345af..ed2b1b8d80 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ExposeServicesAttribute.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ExposeServicesAttribute.cs @@ -1,11 +1,14 @@ using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; namespace Volo.Abp.DependencyInjection { public class ExposeServicesAttribute : Attribute, IExposedServiceTypesProvider { public Type[] ExposedServiceTypes { get; } - + public ExposeServicesAttribute(params Type[] exposedServiceTypes) { ExposedServiceTypes = exposedServiceTypes ?? new Type[0]; @@ -13,7 +16,36 @@ namespace Volo.Abp.DependencyInjection public Type[] GetExposedServiceTypes(Type targetType) { - return ExposedServiceTypes; + if (ExposedServiceTypes.Any()) + { + return ExposedServiceTypes; + } + + return GetDefaultExposedServices(targetType).ToArray(); + } + + private static List GetDefaultExposedServices(Type type) + { + var serviceTypes = new List(); + + serviceTypes.Add(type); + + foreach (var interfaceType in type.GetTypeInfo().GetInterfaces()) + { + var interfaceName = interfaceType.Name; + + if (interfaceName.StartsWith("I")) + { + interfaceName = interfaceName.Right(interfaceName.Length - 1); + } + + if (type.Name.EndsWith(interfaceName)) + { + serviceTypes.Add(interfaceType); + } + } + + return serviceTypes; } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ExposedServiceExplorer.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ExposedServiceExplorer.cs new file mode 100644 index 0000000000..79fb77d6ff --- /dev/null +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ExposedServiceExplorer.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +namespace Volo.Abp.DependencyInjection +{ + public static class ExposedServiceExplorer + { + public static List GetExposedServices(Type type) + { + return type + .GetCustomAttributes() + .OfType() + .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 529290d0a1..f9d2950d42 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 @@ -12,7 +12,7 @@ namespace Volo.Abp.DependencyInjection { //Act - var exposedServices = AutoRegistrationHelper.GetExposedServices(new ServiceCollection(), typeof(DefaultDerivedService)).ToList(); + var exposedServices = ExposedServiceExplorer.GetExposedServices(typeof(DefaultDerivedService)); //Assert @@ -27,7 +27,7 @@ namespace Volo.Abp.DependencyInjection { //Act - var exposedServices = AutoRegistrationHelper.GetExposedServices(new ServiceCollection(), typeof(ExplicitDerivedService)).ToList(); + var exposedServices = ExposedServiceExplorer.GetExposedServices(typeof(ExplicitDerivedService)); //Assert