-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModuleExtensions.cs
More file actions
68 lines (60 loc) · 3.21 KB
/
ModuleExtensions.cs
File metadata and controls
68 lines (60 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
namespace A11d.Module;
public static class ModuleExtensions
{
/// <summary>
/// Installs all <see cref="Module"/>s in the assembly of the specified marker type.
/// </summary>
/// <param name="builder">The <see cref="WebApplicationBuilder"/> to install modules to.</param>
/// <typeparam name="TMarker">Any type in your assembly. This type is solely used to find your assembly.</typeparam>
/// <returns>Returns the <see cref="WebApplicationBuilder"/> for chaining.</returns>
public static WebApplicationBuilder Install<TMarker>(this WebApplicationBuilder builder)
{
return Install(builder, typeof(TMarker).Assembly);
}
/// <summary>
/// Installs all <see cref="Module"/>s in the specified assembly.
/// </summary>
/// <param name="builder">The <see cref="WebApplicationBuilder"/> to install modules to.</param>
/// <param name="assembly">The assembly to install modules from.</param>
/// <returns>Returns the <see cref="WebApplicationBuilder"/> for chaining.</returns>
public static WebApplicationBuilder Install(this WebApplicationBuilder builder, Assembly assembly)
{
builder.Services.ConfigureModulesServices(assembly);
return builder;
}
/// <summary>
/// Configures all <see cref="Module"/>s in the assembly of the specified marker type.
/// </summary>
/// <param name="application">The <see cref="WebApplication"/> to operate configurations on.</param>
/// <typeparam name="TMarker">Any type in your assembly. This type is solely used to find your assembly.</typeparam>
/// <returns>Returns the <see cref="WebApplication"/> for chaining.</returns>
public static WebApplication Configure<TMarker>(this WebApplication application)
{
return Configure(application, typeof(TMarker).Assembly);
}
/// <summary>
/// Configures all <see cref="Module"/>s in the specified assembly.
/// </summary>
/// <param name="application">The <see cref="WebApplication"/> to operate configurations on.</param>
/// <param name="assembly">The assembly to configure modules from.</param>
/// <returns>Returns the <see cref="WebApplication"/> for chaining.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "ASP0014", Justification = "This is a wrapper around all the modules.")]
public static WebApplication Configure(this WebApplication application, Assembly assembly)
{
application.ConfigureModulesApplication(assembly);
application.UseEndpoints(endpoints => endpoints.ConfigureModulesEndpoints(assembly));
return application;
}
private static void ConfigureModulesServices(this IServiceCollection services, Assembly assembly)
=> GetModules(assembly).ForEach(module => module.ConfigureServices(services));
private static void ConfigureModulesApplication(this WebApplication application, Assembly assembly)
=> GetModules(assembly).ForEach(module => module.ConfigureApplication(application));
private static void ConfigureModulesEndpoints(this IEndpointRouteBuilder endpoints, Assembly assembly)
=> GetModules(assembly).ForEach(module => module.ConfigureEndpoints(endpoints));
private static List<Module> GetModules(Assembly assembly) => assembly
.GetTypes()
.Where(p => p.IsClass && p.IsAssignableTo(typeof(Module)) && p.IsAbstract == false)
.Select(Activator.CreateInstance)
.Cast<Module>()
.ToList();
}