-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
GatewayRegistrations.cs
158 lines (142 loc) · 8.48 KB
/
GatewayRegistrations.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (C) Ubiquitous AS. All rights reserved
// Licensed under the Apache License, Version 2.0.
// ReSharper disable CheckNamespace
using Eventuous.Gateway;
using Eventuous.Subscriptions.Registrations;
namespace Microsoft.Extensions.DependencyInjection;
using Extensions;
/// <summary>
/// Registration extensions for the gateway.
/// </summary>
[PublicAPI]
public static class GatewayRegistrations {
/// <summary>
/// Registers a gateway subscription with a producer that has options.
/// </summary>
/// <param name="services">Service collection.</param>
/// <param name="subscriptionId">Gateway subscription id. Must be unique across all subscription in the same application.</param>
/// <param name="routeAndTransform">Routing and transformation function.</param>
/// <param name="configureSubscription">A function to configure the subscription.</param>
/// <param name="configureBuilder">A function to configure the subscription builder.</param>
/// <param name="awaitProduce">An option to wait for each produce action.</param>
/// <typeparam name="TSubscription">Subscription implementation type.</typeparam>
/// <typeparam name="TSubscriptionOptions">Subscription options type.</typeparam>
/// <typeparam name="TProducer">Producer implementation type.</typeparam>
/// <typeparam name="TProduceOptions">Options for producing a message.</typeparam>
/// <returns></returns>
public static IServiceCollection AddGateway<TSubscription, TSubscriptionOptions, TProducer, TProduceOptions>(
this IServiceCollection services,
string subscriptionId,
RouteAndTransform<TProduceOptions> routeAndTransform,
Action<TSubscriptionOptions>? configureSubscription = null,
Action<SubscriptionBuilder<TSubscription, TSubscriptionOptions>>? configureBuilder = null,
bool awaitProduce = true
)
where TSubscription : EventSubscription<TSubscriptionOptions>
where TProducer : class, IEventProducer<TProduceOptions>
where TProduceOptions : class
where TSubscriptionOptions : SubscriptionOptions {
services.TryAddSingleton<TProducer>();
services.AddHostedServiceIfSupported<TProducer>();
services.AddSubscription<TSubscription, TSubscriptionOptions>(
subscriptionId,
builder => {
builder.Configure(configureSubscription);
configureBuilder?.Invoke(builder);
builder.AddEventHandler(
sp => new GatewayHandler<TProduceOptions>(
new GatewayProducer<TProduceOptions>(sp.GetRequiredService<TProducer>()),
routeAndTransform,
awaitProduce
)
);
}
);
return services;
}
/// <summary>
/// Registers a gateway subscription with a producer that has options.
/// It expects the routing and transformation function to be registered in the service collection as <see cref="RouteAndTransform{TProduceOptions}"/>.
/// </summary>
/// <param name="services">Service collection.</param>
/// <param name="subscriptionId">Gateway subscription id. Must be unique across all subscription in the same application.</param>
/// <param name="configureSubscription">A function to configure the subscription.</param>
/// <param name="configureBuilder">A function to configure the subscription builder.</param>
/// <param name="awaitProduce">An option to wait for each produce action.</param>
/// <typeparam name="TSubscription">Subscription implementation type.</typeparam>
/// <typeparam name="TSubscriptionOptions">Subscription options type.</typeparam>
/// <typeparam name="TProducer">Producer implementation type.</typeparam>
/// <typeparam name="TProduceOptions">Options for producing a message.</typeparam>
/// <returns></returns>
public static IServiceCollection AddGateway<TSubscription, TSubscriptionOptions, TProducer, TProduceOptions>(
this IServiceCollection services,
string subscriptionId,
Action<TSubscriptionOptions>? configureSubscription = null,
Action<SubscriptionBuilder<TSubscription, TSubscriptionOptions>>? configureBuilder = null,
bool awaitProduce = true
)
where TSubscription : EventSubscription<TSubscriptionOptions>
where TProducer : class, IEventProducer<TProduceOptions>
where TProduceOptions : class
where TSubscriptionOptions : SubscriptionOptions {
services.TryAddSingleton<TProducer>();
services.AddHostedServiceIfSupported<TProducer>();
services.AddSubscription<TSubscription, TSubscriptionOptions>(
subscriptionId,
builder => {
builder.Configure(configureSubscription);
configureBuilder?.Invoke(builder);
builder.AddEventHandler(GetHandler);
}
);
return services;
IEventHandler GetHandler(IServiceProvider sp) {
var transform = sp.GetRequiredService<RouteAndTransform<TProduceOptions>>();
var producer = sp.GetRequiredService<TProducer>();
return new GatewayHandler<TProduceOptions>(new GatewayProducer<TProduceOptions>(producer), transform, awaitProduce);
}
}
/// <summary>
/// Registers a gateway subscription with a producer that has options.
/// It expects the routing and transformation function to be registered in the service collection as <see cref="IGatewayTransform{TProduceOptions}"/>.
/// </summary>
/// <param name="services">Service collection.</param>
/// <param name="subscriptionId">Gateway subscription id. Must be unique across all subscription in the same application.</param>
/// <param name="configureSubscription">A function to configure the subscription.</param>
/// <param name="configureBuilder">A function to configure the subscription builder.</param>
/// <param name="awaitProduce">An option to wait for each produce action.</param>
/// <typeparam name="TSubscription">Subscription implementation type.</typeparam>
/// <typeparam name="TSubscriptionOptions">Subscription options type.</typeparam>
/// <typeparam name="TProducer">Producer implementation type.</typeparam>
/// <typeparam name="TProduceOptions">Options for producing a message.</typeparam>
/// <typeparam name="TTransform">Message router and transformer type.</typeparam>
/// <returns></returns>
public static IServiceCollection AddGateway<TSubscription, TSubscriptionOptions, TProducer, TProduceOptions, TTransform>(
this IServiceCollection services,
string subscriptionId,
Action<TSubscriptionOptions>? configureSubscription = null,
Action<SubscriptionBuilder<TSubscription, TSubscriptionOptions>>? configureBuilder = null,
bool awaitProduce = true
)
where TSubscription : EventSubscription<TSubscriptionOptions>
where TProducer : class, IEventProducer<TProduceOptions>
where TProduceOptions : class
where TTransform : class, IGatewayTransform<TProduceOptions>
where TSubscriptionOptions : SubscriptionOptions {
services.TryAddSingleton<TTransform>();
services.AddSubscription<TSubscription, TSubscriptionOptions>(
subscriptionId,
builder => {
builder.Configure(configureSubscription);
configureBuilder?.Invoke(builder);
builder.AddEventHandler(GetHandler);
}
);
return services;
IEventHandler GetHandler(IServiceProvider sp) {
var transform = sp.GetRequiredService<TTransform>();
var producer = sp.GetRequiredService<TProducer>();
return new GatewayHandler<TProduceOptions>(new GatewayProducer<TProduceOptions>(producer), transform.RouteAndTransform, awaitProduce);
}
}
}