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 // ReSharper disable once InconsistentNaming
public static class IServiceCollectionExtensions { public static class IServiceCollectionExtensions {
/// <summary> /// <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> /// </summary>
/// <param name="services">Service collection.</param> /// <param name="services">Service collection.</param>
/// <returns>Service collection.</returns> /// <returns>Service collection.</returns>
public static IServiceCollection AutoRegister(this IServiceCollection services) { public static IServiceCollection AutoRegister(this IServiceCollection services) {
var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var assemblies = AppDomain.CurrentDomain.GetAssemblies();
services.AddFactories(DependifyUtils.GetFactoryMethods(assemblies)); return services.AutoRegister(assemblies);
services.AddClasses(DependifyUtils.GetClassTypes(assemblies));
return services;
} }
/// <summary> /// <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> /// </summary>
/// <param name="services">Service collection.</param> /// <param name="services">Service collection.</param>
/// <param name="assemblies">Assemblies to scan</param> /// <param name="assemblies">Assemblies to scan</param>
@@ -38,10 +42,13 @@ namespace Dependify {
} }
/// <summary> /// <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> /// </summary>
/// <param name="services"></param> /// <param name="services">Service collection.</param>
/// <param name="namespace"></param> /// <param name="namespace">Scanned namespace.</param>
/// <returns></returns> /// <returns></returns>
public static IServiceCollection AutoRegister(this IServiceCollection services, string @namespace) { public static IServiceCollection AutoRegister(this IServiceCollection services, string @namespace) {
var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var assemblies = AppDomain.CurrentDomain.GetAssemblies();
@@ -50,6 +57,19 @@ namespace Dependify {
return services; 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) { internal static IServiceCollection AddFactories(this IServiceCollection services, IEnumerable<MethodInfo> methodInfos) {
foreach (var methodInfo in methodInfos) { foreach (var methodInfo in methodInfos) {
var factoryAttribute = methodInfo.GetCustomAttributes<RegisterFactory>(true).First(); var factoryAttribute = methodInfo.GetCustomAttributes<RegisterFactory>(true).First();