From 930577b5db33925fdcd4d4455d98c0df1a45220a Mon Sep 17 00:00:00 2001 From: Melkus Adam Date: Sun, 10 Sep 2017 19:49:08 +0200 Subject: [PATCH] Added the possibility to register classes not implementing any interface, fixed incorrect way to get classes from namespace --- src/Dependify.Test/AutoRegisterTestCases.cs | 15 +++++++++++++++ src/Dependify.Test/AutoRegisterTests.cs | 6 ++++++ src/Dependify/IServiceCollectionExtensions.cs | 8 +++++++- src/Dependify/Utilities/DependifyUtils.cs | 8 ++++++-- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/Dependify.Test/AutoRegisterTestCases.cs b/src/Dependify.Test/AutoRegisterTestCases.cs index ab5548c..abcb596 100644 --- a/src/Dependify.Test/AutoRegisterTestCases.cs +++ b/src/Dependify.Test/AutoRegisterTestCases.cs @@ -27,6 +27,21 @@ namespace ShouldRegisterSingleton { public class ImplementationSingleton : IInterface { } } +namespace ShouldRegisterTransientWithoutInterface { + [RegisterTransient] + public class ImplementationTransientWithoutInterface { } +} + +namespace ShouldRegisterScopedWithoutInterface { + [RegisterScoped] + public class ImplementationScopedWithoutInterface { } +} + +namespace ShouldRegisterSingletonWithoutInterface { + [RegisterSingleton] + public class ImplementationSingletonWithoutInterface { } +} + namespace ShouldRegisterOneTransient { [RegisterTransient(typeof(IInterface2))] public class ImplementationTransientOneInterface : IInterface, IInterface2 { } diff --git a/src/Dependify.Test/AutoRegisterTests.cs b/src/Dependify.Test/AutoRegisterTests.cs index 131191f..78f8100 100644 --- a/src/Dependify.Test/AutoRegisterTests.cs +++ b/src/Dependify.Test/AutoRegisterTests.cs @@ -11,8 +11,11 @@ using ShouldRegisterOneScoped; using ShouldRegisterOneSingleton; using ShouldRegisterOneTransient; using ShouldRegisterScoped; +using ShouldRegisterScopedWithoutInterface; using ShouldRegisterSingleton; +using ShouldRegisterSingletonWithoutInterface; using ShouldRegisterTransient; +using ShouldRegisterTransientWithoutInterface; namespace Dependify.Test { [TestFixture] @@ -20,6 +23,9 @@ namespace Dependify.Test { [TestCase(nameof(ShouldRegisterTransient), typeof(ImplementationTransient), typeof(IInterface), ServiceLifetime.Transient)] [TestCase(nameof(ShouldRegisterSingleton), typeof(ImplementationSingleton), typeof(IInterface), ServiceLifetime.Singleton)] [TestCase(nameof(ShouldRegisterScoped), typeof(ImplementationScoped), typeof(IInterface), ServiceLifetime.Scoped)] + [TestCase(nameof(ShouldRegisterTransientWithoutInterface), typeof(ImplementationTransientWithoutInterface), typeof(ImplementationTransientWithoutInterface), ServiceLifetime.Transient)] + [TestCase(nameof(ShouldRegisterSingletonWithoutInterface), typeof(ImplementationSingletonWithoutInterface), typeof(ImplementationSingletonWithoutInterface), ServiceLifetime.Singleton)] + [TestCase(nameof(ShouldRegisterScopedWithoutInterface), typeof(ImplementationScopedWithoutInterface), typeof(ImplementationScopedWithoutInterface), ServiceLifetime.Scoped)] [TestCase(nameof(ShouldRegisterOneTransient), typeof(ImplementationTransientOneInterface), typeof(IInterface2), ServiceLifetime.Transient)] [TestCase(nameof(ShouldRegisterOneSingleton), typeof(ImplementationSingletonOneInterface), typeof(IInterface2), ServiceLifetime.Singleton)] [TestCase(nameof(ShouldRegisterOneScoped), typeof(ImplementationScopedOneInterface), typeof(IInterface2), ServiceLifetime.Scoped)] diff --git a/src/Dependify/IServiceCollectionExtensions.cs b/src/Dependify/IServiceCollectionExtensions.cs index 9a0c0df..9370a19 100644 --- a/src/Dependify/IServiceCollectionExtensions.cs +++ b/src/Dependify/IServiceCollectionExtensions.cs @@ -91,7 +91,13 @@ namespace Dependify { var classAttributes = classType.GetCustomAttributes(true); foreach (var classAttribute in classAttributes) { var registrationType = classAttribute.GetType(); - var interfaceTypes = classAttribute.InterfaceTypes == null || !classAttribute.InterfaceTypes.Any() ? classType.GetInterfaces() : classAttribute.InterfaceTypes; + IEnumerable interfaceTypes; + if (classAttribute.InterfaceTypes?.Any() ?? false) + interfaceTypes = classAttribute.InterfaceTypes; + else if (classType.GetInterfaces().Any()) + interfaceTypes = classType.GetInterfaces(); + else + interfaceTypes = new[] { classType }; foreach (var interfaceType in interfaceTypes) { if (registrationType == typeof(RegisterTransient)) diff --git a/src/Dependify/Utilities/DependifyUtils.cs b/src/Dependify/Utilities/DependifyUtils.cs index 5eaa027..32875f8 100644 --- a/src/Dependify/Utilities/DependifyUtils.cs +++ b/src/Dependify/Utilities/DependifyUtils.cs @@ -18,7 +18,7 @@ namespace Dependify.Utilities { } internal static IEnumerable GetFactoryMethodsFromNamespace(IEnumerable assemblies, string @namespace) { - return assemblies + return assemblies .SelectMany(assembly => assembly.GetTypes()) .Where(type => type.IsClass && (type.Namespace?.StartsWith(@namespace)).GetValueOrDefault()) .SelectMany(MethodsWithAttribute); @@ -45,7 +45,11 @@ namespace Dependify.Utilities { } internal static IEnumerable GetClassTypesFromNamespace(IEnumerable assemblies, string @namespace) { - return assemblies.SelectMany(assembly => assembly.GetTypes()).Where(type => type.IsClass && (type.Namespace?.StartsWith(@namespace)).GetValueOrDefault()); + return assemblies.SelectMany(assembly => assembly.GetTypes()).Where(type => type.IsClass && BelongsToNamespace(type.Namespace, @namespace)); } + + private static bool BelongsToNamespace(string testedNamespace, string parentNamespace) + => testedNamespace != null && + (testedNamespace == parentNamespace || testedNamespace.StartsWith(parentNamespace + ".")); } } \ No newline at end of file