From 2a01e5b2a362e302f48a9a9da9a081841956fdaa Mon Sep 17 00:00:00 2001 From: David Kaya Date: Sun, 10 Sep 2017 14:14:04 +0200 Subject: [PATCH] Added IAssemblyResolver and new overload --- src/Dependify/IAssemblyResolver.cs | 17 +++++++++ src/Dependify/IServiceCollectionExtensions.cs | 38 ++++++++++++++----- src/Dependify/Utilities/DependifyUtils.cs | 2 +- 3 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 src/Dependify/IAssemblyResolver.cs diff --git a/src/Dependify/IAssemblyResolver.cs b/src/Dependify/IAssemblyResolver.cs new file mode 100644 index 0000000..21d59ae --- /dev/null +++ b/src/Dependify/IAssemblyResolver.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; +using System.Reflection; +using Dependify.Attributes; +using Microsoft.Extensions.DependencyInjection; + +namespace Dependify { + /// + /// Specifies a resolver that knows how to locate assemblies that will be scanned for registration in . + /// + public interface IAssemblyResolver { + /// + /// Returns assemblies that will be scanned for and attributes. + /// + /// Collection of assemblies. + IEnumerable Resolve(); + } +} \ No newline at end of file diff --git a/src/Dependify/IServiceCollectionExtensions.cs b/src/Dependify/IServiceCollectionExtensions.cs index 10b979a..9a0c0df 100644 --- a/src/Dependify/IServiceCollectionExtensions.cs +++ b/src/Dependify/IServiceCollectionExtensions.cs @@ -14,19 +14,23 @@ namespace Dependify { // ReSharper disable once InconsistentNaming public static class IServiceCollectionExtensions { /// - /// Adds all classes with , or attribute to . + /// Adds all classes and factory methods with , + /// or attribute for classes + /// and , or for + /// factory methods to . /// /// Service collection. /// Service collection. public static IServiceCollection AutoRegister(this IServiceCollection services) { var assemblies = AppDomain.CurrentDomain.GetAssemblies(); - services.AddFactories(DependifyUtils.GetFactoryMethods(assemblies)); - services.AddClasses(DependifyUtils.GetClassTypes(assemblies)); - return services; + return services.AutoRegister(assemblies); } /// - /// Adds all classes from specified with , or attribute to . + /// Adds all classes and factory methods from specified with , + /// or attribute for classes + /// and , or for + /// factory methods to . /// /// Service collection. /// Assemblies to scan @@ -36,12 +40,15 @@ namespace Dependify { services.AddClasses(DependifyUtils.GetClassTypes(assemblies)); return services; } - + /// - /// Adds all classes, from namespaces that start with , with , or attribute to . + /// Adds all classes and factory methods, from namespaces that start with , with , + /// or attribute for classes + /// and , or for + /// factory methods to . /// - /// - /// + /// Service collection. + /// Scanned namespace. /// public static IServiceCollection AutoRegister(this IServiceCollection services, string @namespace) { var assemblies = AppDomain.CurrentDomain.GetAssemblies(); @@ -50,6 +57,19 @@ namespace Dependify { return services; } + /// + /// Adds all classes and factory methods resolved from with , + /// or attribute for classes + /// and , or for + /// factory methods to . + /// + /// Service collection. + /// Assembly resolver. + /// + public static IServiceCollection AutoRegister(this IServiceCollection services, IAssemblyResolver assemblyResolver) { + return services.AutoRegister(assemblyResolver.Resolve().ToArray()); + } + internal static IServiceCollection AddFactories(this IServiceCollection services, IEnumerable methodInfos) { foreach (var methodInfo in methodInfos) { var factoryAttribute = methodInfo.GetCustomAttributes(true).First(); diff --git a/src/Dependify/Utilities/DependifyUtils.cs b/src/Dependify/Utilities/DependifyUtils.cs index bc99eae..5eaa027 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);