Added the possibility to register classes not implementing any interface, fixed incorrect way to get classes from namespace

This commit is contained in:
Melkus Adam
2017-09-10 19:49:08 +02:00
parent fc48d8ba83
commit 930577b5db
4 changed files with 34 additions and 3 deletions

View File

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

View File

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

View File

@@ -91,7 +91,13 @@ namespace Dependify {
var classAttributes = classType.GetCustomAttributes<Register>(true);
foreach (var classAttribute in classAttributes) {
var registrationType = classAttribute.GetType();
var interfaceTypes = classAttribute.InterfaceTypes == null || !classAttribute.InterfaceTypes.Any() ? classType.GetInterfaces() : classAttribute.InterfaceTypes;
IEnumerable<Type> 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))

View File

@@ -18,7 +18,7 @@ namespace Dependify.Utilities {
}
internal static IEnumerable<MethodInfo> GetFactoryMethodsFromNamespace(IEnumerable<Assembly> 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<Type> GetClassTypesFromNamespace(IEnumerable<Assembly> 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 + "."));
}
}