mirror of
https://github.com/adusak/Dependify.git
synced 2026-03-24 18:31:42 +01:00
Added the possibility to register classes not implementing any interface, fixed incorrect way to get classes from namespace
This commit is contained in:
@@ -27,6 +27,21 @@ namespace ShouldRegisterSingleton {
|
|||||||
public class ImplementationSingleton : IInterface { }
|
public class ImplementationSingleton : IInterface { }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace ShouldRegisterTransientWithoutInterface {
|
||||||
|
[RegisterTransient]
|
||||||
|
public class ImplementationTransientWithoutInterface { }
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace ShouldRegisterScopedWithoutInterface {
|
||||||
|
[RegisterScoped]
|
||||||
|
public class ImplementationScopedWithoutInterface { }
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace ShouldRegisterSingletonWithoutInterface {
|
||||||
|
[RegisterSingleton]
|
||||||
|
public class ImplementationSingletonWithoutInterface { }
|
||||||
|
}
|
||||||
|
|
||||||
namespace ShouldRegisterOneTransient {
|
namespace ShouldRegisterOneTransient {
|
||||||
[RegisterTransient(typeof(IInterface2))]
|
[RegisterTransient(typeof(IInterface2))]
|
||||||
public class ImplementationTransientOneInterface : IInterface, IInterface2 { }
|
public class ImplementationTransientOneInterface : IInterface, IInterface2 { }
|
||||||
|
|||||||
@@ -11,8 +11,11 @@ using ShouldRegisterOneScoped;
|
|||||||
using ShouldRegisterOneSingleton;
|
using ShouldRegisterOneSingleton;
|
||||||
using ShouldRegisterOneTransient;
|
using ShouldRegisterOneTransient;
|
||||||
using ShouldRegisterScoped;
|
using ShouldRegisterScoped;
|
||||||
|
using ShouldRegisterScopedWithoutInterface;
|
||||||
using ShouldRegisterSingleton;
|
using ShouldRegisterSingleton;
|
||||||
|
using ShouldRegisterSingletonWithoutInterface;
|
||||||
using ShouldRegisterTransient;
|
using ShouldRegisterTransient;
|
||||||
|
using ShouldRegisterTransientWithoutInterface;
|
||||||
|
|
||||||
namespace Dependify.Test {
|
namespace Dependify.Test {
|
||||||
[TestFixture]
|
[TestFixture]
|
||||||
@@ -20,6 +23,9 @@ namespace Dependify.Test {
|
|||||||
[TestCase(nameof(ShouldRegisterTransient), typeof(ImplementationTransient), typeof(IInterface), ServiceLifetime.Transient)]
|
[TestCase(nameof(ShouldRegisterTransient), typeof(ImplementationTransient), typeof(IInterface), ServiceLifetime.Transient)]
|
||||||
[TestCase(nameof(ShouldRegisterSingleton), typeof(ImplementationSingleton), typeof(IInterface), ServiceLifetime.Singleton)]
|
[TestCase(nameof(ShouldRegisterSingleton), typeof(ImplementationSingleton), typeof(IInterface), ServiceLifetime.Singleton)]
|
||||||
[TestCase(nameof(ShouldRegisterScoped), typeof(ImplementationScoped), typeof(IInterface), ServiceLifetime.Scoped)]
|
[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(ShouldRegisterOneTransient), typeof(ImplementationTransientOneInterface), typeof(IInterface2), ServiceLifetime.Transient)]
|
||||||
[TestCase(nameof(ShouldRegisterOneSingleton), typeof(ImplementationSingletonOneInterface), typeof(IInterface2), ServiceLifetime.Singleton)]
|
[TestCase(nameof(ShouldRegisterOneSingleton), typeof(ImplementationSingletonOneInterface), typeof(IInterface2), ServiceLifetime.Singleton)]
|
||||||
[TestCase(nameof(ShouldRegisterOneScoped), typeof(ImplementationScopedOneInterface), typeof(IInterface2), ServiceLifetime.Scoped)]
|
[TestCase(nameof(ShouldRegisterOneScoped), typeof(ImplementationScopedOneInterface), typeof(IInterface2), ServiceLifetime.Scoped)]
|
||||||
|
|||||||
@@ -91,7 +91,13 @@ namespace Dependify {
|
|||||||
var classAttributes = classType.GetCustomAttributes<Register>(true);
|
var classAttributes = classType.GetCustomAttributes<Register>(true);
|
||||||
foreach (var classAttribute in classAttributes) {
|
foreach (var classAttribute in classAttributes) {
|
||||||
var registrationType = classAttribute.GetType();
|
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) {
|
foreach (var interfaceType in interfaceTypes) {
|
||||||
if (registrationType == typeof(RegisterTransient))
|
if (registrationType == typeof(RegisterTransient))
|
||||||
|
|||||||
@@ -45,7 +45,11 @@ namespace Dependify.Utilities {
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal static IEnumerable<Type> GetClassTypesFromNamespace(IEnumerable<Assembly> assemblies, string @namespace) {
|
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 + "."));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user