Skip to content

Commit

Permalink
chore: minimal support specify Assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenlei520 committed Nov 12, 2021
1 parent e4428fc commit 1bbf336
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Microsoft.Extensions.DependencyInjection;
public static class ServiceCollectionExtensions
{
Expand All @@ -12,29 +7,46 @@ public static class ServiceCollectionExtensions
/// <param name="services"></param>
/// <param name="suffix">default is Service</param>
public static IServiceCollection AddServices(this IServiceCollection services, string suffix, bool autoFire)
{
return Assembly
.GetEntryAssembly()!
.GetTypes()
.Where(t => t.Name.EndsWith(suffix))
.AddScoped(services, autoFire);
}
=> services.AddServices(suffix, autoFire, Assembly.GetEntryAssembly()!);

/// <summary>
/// Auto add all service to IoC, lifecycle is scoped
/// </summary>
/// <param name="services"></param>
/// <param name="suffix">default is Service</param>
/// <param name="autoFire"></param>
/// <param name="assemblies"></param>
/// <returns></returns>
public static IServiceCollection AddServices(this IServiceCollection services, string suffix, bool autoFire, params Assembly[] assemblies)
=> (from type in assemblies.SelectMany(assembly => assembly.GetTypes())
where type.Name.EndsWith(suffix)
select type).AddScoped(services, autoFire);

/// <summary>
/// Auto add all service to IoC, lifecycle is scoped
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <param name="services"></param>
/// <param name="autoFire"></param>
/// <returns></returns>
public static IServiceCollection AddServices<TService>(this IServiceCollection services, bool autoFire)
{
var serviceType = typeof(TService);
=> services.AddServices<TService>(autoFire, new Assembly[1]
{
Assembly.GetEntryAssembly()!
});

return Assembly
.GetEntryAssembly()!
.GetTypes()
.Where(t => t.BaseType == serviceType)
.AddScoped(services, autoFire);
}
/// <summary>
/// Auto add all service to IoC, lifecycle is scoped
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <param name="services"></param>
/// <param name="autoFire"></param>
/// <param name="assemblies"></param>
/// <returns></returns>
public static IServiceCollection AddServices<TService>(this IServiceCollection services, bool autoFire, params Assembly[] assemblies)
=> (from type in assemblies.SelectMany(assembly => assembly.GetTypes())
where type.BaseType == typeof(TService)
select type).AddScoped(services, autoFire);

private static IServiceCollection AddScoped(this IEnumerable<Type> serviceTypes, IServiceCollection services, bool autoFire)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using System.Reflection;

0 comments on commit 1bbf336

Please sign in to comment.