-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
89 lines (75 loc) · 3.92 KB
/
ServiceCollectionExtensions.cs
File metadata and controls
89 lines (75 loc) · 3.92 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and ReflectionEventing Contributors.
// All Rights Reserved.
using ReflectionEventing.DependencyInjection.Configuration;
using ReflectionEventing.Queues;
namespace ReflectionEventing.DependencyInjection;
/// <summary>
/// Provides extension methods for the <see cref="IServiceCollection"/> interface.
/// </summary>
public static class ServiceCollectionExtensions
{
#if NET8_0_OR_GREATER
/// <summary>
/// Adds the event bus and its related services to the specified services collection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the event bus to.</param>
/// <param name="configure">A delegate that configures the <see cref="EventBusBuilder"/>.</param>
/// <param name="serviceKey">The <see cref="ServiceDescriptor.ServiceKey"/> of the service.</param>
/// <returns>The same service collection so that multiple calls can be chained.</returns>
/// <remarks>
/// This method adds a singleton service of type <see cref="IConsumerTypesProvider"/> that uses a <see cref="HashedConsumerTypesProvider"/> with the consumers from the event bus builder.
/// It also adds a scoped service of type <see cref="IEventBus"/> that uses the <see cref="EventBus"/> class.
/// </remarks>
public static IServiceCollection AddEventBus(
this IServiceCollection services,
Action<EventBusBuilder> configure,
object? serviceKey
)
{
DependencyInjectionEventBusBuilder builder = new(services);
configure(builder);
_ = services.AddKeyedSingleton(serviceKey, builder.BuildTypesProvider());
_ = services.AddKeyedSingleton<IEventsQueue, EventsQueue>(serviceKey);
_ = services.AddKeyedScoped<IConsumerProvider, DependencyInjectionConsumerProvider>(
serviceKey
);
_ = services.AddKeyedScoped<IEventBus, DependencyInjectionEventBus>(serviceKey);
_ = services.AddSingleton(new QueueProcessorOptionsProvider(builder.Options, serviceKey));
if (builder.Options.UseEventsQueue)
{
_ = services.AddSingleton(typeof(IHostedService), builder.QueueBackgroundService);
}
return services;
}
#endif
/// <summary>
/// Adds the event bus and its related services to the specified services collection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the event bus to.</param>
/// <param name="configure">A delegate that configures the <see cref="EventBusBuilder"/>.</param>
/// <returns>The same service collection so that multiple calls can be chained.</returns>
/// <remarks>
/// This method adds a singleton service of type <see cref="IConsumerTypesProvider"/> that uses a <see cref="HashedConsumerTypesProvider"/> with the consumers from the event bus builder.
/// It also adds a scoped service of type <see cref="IEventBus"/> that uses the <see cref="EventBus"/> class.
/// </remarks>
public static IServiceCollection AddEventBus(
this IServiceCollection services,
Action<EventBusBuilder> configure
)
{
DependencyInjectionEventBusBuilder builder = new(services);
configure(builder);
_ = services.AddSingleton(builder.BuildTypesProvider());
_ = services.AddSingleton<IEventsQueue, EventsQueue>();
_ = services.AddScoped<IConsumerProvider, DependencyInjectionConsumerProvider>();
_ = services.AddScoped<IEventBus, DependencyInjectionEventBus>();
_ = services.AddSingleton(new QueueProcessorOptionsProvider(builder.Options));
if (builder.Options.UseEventsQueue)
{
_ = services.AddSingleton(typeof(IHostedService), builder.QueueBackgroundService);
}
return services;
}
}