-
-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathPublishingExpression.cs
More file actions
191 lines (161 loc) · 5.36 KB
/
PublishingExpression.cs
File metadata and controls
191 lines (161 loc) · 5.36 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System.Reflection;
using JasperFx.Core;
using Wolverine.Runtime.Routing;
using Wolverine.Transports;
using Wolverine.Transports.Local;
namespace Wolverine.Configuration;
public class PublishingExpression : IPublishToExpression
{
private readonly List<Endpoint> _endpoints = new();
private readonly List<Subscription> _subscriptions = new();
internal PublishingExpression(WolverineOptions parent)
{
Parent = parent;
}
public WolverineOptions Parent { get; }
internal bool AutoAddSubscriptions { get; set; }
/// <summary>
/// All matching records are to be sent to the configured subscriber
/// by Uri
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
public ISubscriberConfiguration To(Uri uri)
{
var endpoint = Parent.Transports.GetOrCreateEndpoint(uri);
AddSubscriber(endpoint);
return new SubscriberConfiguration(endpoint);
}
/// <summary>
/// Send all the matching messages to the designated Uri string
/// </summary>
/// <param name="uriString"></param>
/// <returns></returns>
public ISubscriberConfiguration To(string uriString)
{
return To(uriString.ToUri());
}
/// <summary>
/// Publishes the matching messages locally to the default
/// local queue
/// </summary>
public LocalQueueConfiguration Locally()
{
var settings = Parent.Transports.GetOrCreate<LocalTransport>().QueueFor(TransportConstants.Default);
settings.Subscriptions.AddRange(_subscriptions);
return new LocalQueueConfiguration(settings);
}
/// <summary>
/// Publish the designated message types to the named
/// local queue
/// </summary>
/// <param name="queueName"></param>
/// <returns></returns>
public LocalQueueConfiguration ToLocalQueue(string queueName)
{
var settings = Parent.Transports.GetOrCreate<LocalTransport>().QueueFor(queueName);
if (AutoAddSubscriptions)
{
settings.Subscriptions.AddRange(_subscriptions);
}
_endpoints.Add(settings);
return new LocalQueueConfiguration(settings);
}
public PublishingExpression AddSubscriber(Endpoint endpoint)
{
_endpoints.Add(endpoint);
if (AutoAddSubscriptions)
{
endpoint.Subscriptions.AddRange(_subscriptions);
}
return this;
}
/// <summary>
/// Create a publishing rule for a single message type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public PublishingExpression Message<T>()
{
return Message(typeof(T));
}
/// <summary>
/// Create a publishing rule for a single message type
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public PublishingExpression Message(Type type)
{
_subscriptions.Add(Subscription.ForType(type));
return this;
}
/// <summary>
/// Create a publishing rule for all message types from within the
/// specified namespace
/// </summary>
/// <param name="namespace"></param>
/// <returns></returns>
public PublishingExpression MessagesFromNamespace(string @namespace)
{
AutoAddSubscriptions = true;
_subscriptions.Add(new Subscription
{
Match = @namespace,
Scope = RoutingScope.Namespace
});
return this;
}
/// <summary>
/// Create a publishing rule for all message types from within the
/// specified namespace holding the marker type "T"
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public PublishingExpression MessagesFromNamespaceContaining<T>()
{
return MessagesFromNamespace(typeof(T).Namespace!);
}
/// <summary>
/// Create a publishing rule for all messages from the given assembly
/// </summary>
/// <param name="assembly"></param>
/// <returns></returns>
public PublishingExpression MessagesFromAssembly(Assembly assembly)
{
_subscriptions.Add(new Subscription(assembly));
AutoAddSubscriptions = true;
return this;
}
/// <summary>
/// Create a publishing rule for all messages from the given assembly that contains the type T
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public PublishingExpression MessagesFromAssemblyContaining<T>()
{
return MessagesFromAssembly(typeof(T).Assembly);
}
internal void AttachSubscriptions()
{
if (!_endpoints.Any())
{
throw new InvalidOperationException("No subscriber endpoint(s) are specified!");
}
foreach (var endpoint in _endpoints) endpoint.Subscriptions.AddRange(_subscriptions);
}
internal void AddSubscriptionForAllMessages()
{
_subscriptions.Add(Subscription.All());
}
/// <summary>
/// Publish all messages implementing a marker interface or inheriting from a common
/// base class
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public PublishingExpression MessagesImplementing<T>()
{
_subscriptions.Add(new Subscription { BaseType = typeof(T), Scope = RoutingScope.Implements });
return this;
}
}