mirror of
https://github.com/adusak/Dependify.git
synced 2026-03-19 16:21:28 +01:00
Added IAssemblyResolver and new overload
This commit is contained in:
17
src/Dependify/IAssemblyResolver.cs
Normal file
17
src/Dependify/IAssemblyResolver.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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>
|
||||||
@@ -36,12 +40,15 @@ namespace Dependify {
|
|||||||
services.AddClasses(DependifyUtils.GetClassTypes(assemblies));
|
services.AddClasses(DependifyUtils.GetClassTypes(assemblies));
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <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();
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace Dependify.Utilities {
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal static IEnumerable<MethodInfo> GetFactoryMethodsFromNamespace(IEnumerable<Assembly> assemblies, string @namespace) {
|
internal static IEnumerable<MethodInfo> GetFactoryMethodsFromNamespace(IEnumerable<Assembly> assemblies, string @namespace) {
|
||||||
return assemblies
|
return assemblies
|
||||||
.SelectMany(assembly => assembly.GetTypes())
|
.SelectMany(assembly => assembly.GetTypes())
|
||||||
.Where(type => type.IsClass && (type.Namespace?.StartsWith(@namespace)).GetValueOrDefault())
|
.Where(type => type.IsClass && (type.Namespace?.StartsWith(@namespace)).GetValueOrDefault())
|
||||||
.SelectMany(MethodsWithAttribute);
|
.SelectMany(MethodsWithAttribute);
|
||||||
|
|||||||
Reference in New Issue
Block a user