This repository has been archived by the owner on May 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceCollectionExtensions.cs
91 lines (81 loc) · 3.87 KB
/
ServiceCollectionExtensions.cs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
namespace InjectX;
/// <summary>
/// Defines extension methods for the automatic registration of service objects to an <see cref="IServiceCollection"/>.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Registers service objects that have been defined within the assembly of the current application.
/// </summary>
/// <remarks>
/// Below is the default lifetime for service objects:
/// <list type="bullet">
/// <item><see cref="ServiceLifetime.Transient"/></item>
/// </list>
/// The lifetime for a service object may be overridden by annotating it with one of the following attributes:
/// <list type="bullet">
/// <item><see cref="SingletonAttribute"/></item>
/// <item><see cref="TransientAttribute"/></item>
/// <item><see cref="ScopedAttribute"/></item>
/// </list>
/// </remarks>
/// <param name="services">The service collection.</param>
/// <param name="strategy">The strategy to use when adding new services to the service collection.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls may be chained.</returns>
public static IServiceCollection RegisterApplicationServices(this IServiceCollection services, RegistrationStrategy? strategy = null)
{
Verify.NotNull(services);
Verify.AppDomainExists();
return services.RegisterAssemblyServices(Assembly.GetEntryAssembly(), strategy);
}
/// <summary>
/// Registers service objects that have been defined within <paramref name="assembly"/>.
/// </summary>
/// <remarks>
/// Below is the default lifetime for service objects:
/// <list type="bullet">
/// <item><see cref="ServiceLifetime.Transient"/></item>
/// </list>
/// The lifetime for a service object may be overridden by annotating it with one of the following attributes:
/// <list type="bullet">
/// <item><see cref="SingletonAttribute"/></item>
/// <item><see cref="TransientAttribute"/></item>
/// <item><see cref="ScopedAttribute"/></item>
/// </list>
/// </remarks>
/// <param name="services">The service collection.</param>
/// <param name="assembly">The assembly containing the service objects.</param>
/// <param name="strategy">The strategy to use when adding new services to the service collection.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls may be chained.</returns>
public static IServiceCollection RegisterAssemblyServices(this IServiceCollection services, Assembly assembly, RegistrationStrategy? strategy = null)
{
Verify.NotNull(services);
Verify.NotNull(assembly);
string rootNamespace = assembly.GetRootNamespace();
strategy ??= RegistrationStrategy.Append;
assembly
.GetTypes()
.Where(type =>
type.IsPublic &&
type.Namespace is not null &&
type.Namespace.Equals($"{rootNamespace}.Services", StringComparison.Ordinal))
.Fork(type =>
type.IsInterface,
out IEnumerable<Type> serviceTypes,
out IEnumerable<Type> implementationTypes);
implementationTypes
.ForEach(implementationType =>
{
Type serviceType = implementationType
.GetInterfaces()
.Where(contract => serviceTypes.Contains(contract))
.FirstOrDefault()
?? implementationType;
ServiceLifetime lifetime = implementationType.GetCustomAttribute<LifetimeAttribute>()?.Lifetime
?? ServiceLifetime.Transient;
ServiceDescriptor descriptor = ServiceDescriptor.Describe(serviceType, implementationType, lifetime);
strategy.Execute(services, descriptor);
});
return services;
}
}