Added IAssemblyResolver and new overload

This commit is contained in:
David Kaya
2017-09-10 14:14:04 +02:00
parent f45e305172
commit 2a01e5b2a3
3 changed files with 47 additions and 10 deletions

View File

@@ -0,0 +1,17 @@
using System.Collections.Generic;
using System.Reflection;
using Dependify.Attributes;
using Microsoft.Extensions.DependencyInjection;
namespace Dependify {
/// <summary>
/// Specifies a resolver that knows how to locate assemblies that will be scanned for registration in <see cref="IServiceCollection"/>.
/// </summary>
public interface IAssemblyResolver {
/// <summary>
/// Returns assemblies that will be scanned for <see cref="Register"/> and <see cref="RegisterFactory"/> attributes.
/// </summary>
/// <returns>Collection of assemblies.</returns>
IEnumerable<Assembly> Resolve();
}
}

View File

@@ -14,19 +14,23 @@ namespace Dependify {
// ReSharper disable once InconsistentNaming
public static class IServiceCollectionExtensions {
/// <summary>
/// Adds all classes with <see cref="RegisterScoped"/>, <see cref="RegisterSingleton"/> or <see cref="RegisterTransient"/> attribute to <see cref="IServiceCollection"/>.
/// Adds all classes and factory methods with <see cref="RegisterScoped"/>,
/// <see cref="RegisterSingleton"/> or <see cref="RegisterTransient"/> attribute for classes
/// and <see cref="RegisterScopedFactory"/>, <see cref="RegisterSingletonFactory"/> or <see cref="RegisterTransientFactory"/> for
/// factory methods to <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">Service collection.</param>
/// <returns>Service collection.</returns>
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);
}
/// <summary>
/// Adds all classes from specified <paramref name="assemblies"/> with <see cref="RegisterScoped"/>, <see cref="RegisterSingleton"/> or <see cref="RegisterTransient"/> attribute to <see cref="IServiceCollection"/>.
/// Adds all classes and factory methods from specified <paramref name="assemblies"/> with <see cref="RegisterScoped"/>,
/// <see cref="RegisterSingleton"/> or <see cref="RegisterTransient"/> attribute for classes
/// and <see cref="RegisterScopedFactory"/>, <see cref="RegisterSingletonFactory"/> or <see cref="RegisterTransientFactory"/> for
/// factory methods to <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">Service collection.</param>
/// <param name="assemblies">Assemblies to scan</param>
@@ -36,12 +40,15 @@ namespace Dependify {
services.AddClasses(DependifyUtils.GetClassTypes(assemblies));
return services;
}
/// <summary>
/// Adds all classes, from namespaces that start with <paramref name="namespace"/>, with <see cref="RegisterScoped"/>, <see cref="RegisterSingleton"/> or <see cref="RegisterTransient"/> attribute to <see cref="IServiceCollection"/>.
/// Adds all classes and factory methods, from namespaces that start with <paramref name="namespace"/>, with <see cref="RegisterScoped"/>,
/// <see cref="RegisterSingleton"/> or <see cref="RegisterTransient"/> attribute for classes
/// and <see cref="RegisterScopedFactory"/>, <see cref="RegisterSingletonFactory"/> or <see cref="RegisterTransientFactory"/> for
/// factory methods to <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services"></param>
/// <param name="namespace"></param>
/// <param name="services">Service collection.</param>
/// <param name="namespace">Scanned namespace.</param>
/// <returns></returns>
public static IServiceCollection AutoRegister(this IServiceCollection services, string @namespace) {
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
@@ -50,6 +57,19 @@ namespace Dependify {
return services;
}
/// <summary>
/// Adds all classes and factory methods resolved from <paramref name="assemblyResolver"/> with <see cref="RegisterScoped"/>,
/// <see cref="RegisterSingleton"/> or <see cref="RegisterTransient"/> attribute for classes
/// and <see cref="RegisterScopedFactory"/>, <see cref="RegisterSingletonFactory"/> or <see cref="RegisterTransientFactory"/> for
/// factory methods to <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">Service collection.</param>
/// <param name="assemblyResolver">Assembly resolver.</param>
/// <returns></returns>
public static IServiceCollection AutoRegister(this IServiceCollection services, IAssemblyResolver assemblyResolver) {
return services.AutoRegister(assemblyResolver.Resolve().ToArray());
}
internal static IServiceCollection AddFactories(this IServiceCollection services, IEnumerable<MethodInfo> methodInfos) {
foreach (var methodInfo in methodInfos) {
var factoryAttribute = methodInfo.GetCustomAttributes<RegisterFactory>(true).First();

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);