Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using System.Threading;
11 : using cdmdotnet.Logging;
12 : using Cqrs.Bus;
13 : using Cqrs.Commands;
14 : using Cqrs.Configuration;
15 : using Cqrs.Events;
16 : using Cqrs.Messages;
17 : using Microsoft.ServiceBus.Messaging;
18 :
19 : namespace Cqrs.Azure.ServiceBus
20 : {
21 : /// <summary>
22 : /// A helper for Azure Service Bus and Event Hub.
23 : /// </summary>
24 : /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
25 : public interface IAzureBusHelper<TAuthenticationToken>
26 : {
27 : /// <summary>
28 : /// Prepares a <see cref="ICommand{TAuthenticationToken}"/> to be sent specifying the framework it is sent via.
29 : /// </summary>
30 : /// <typeparam name="TCommand">The <see cref="Type"/> of<see cref="ICommand{TAuthenticationToken}"/> being sent.</typeparam>
31 : /// <param name="command">The <see cref="ICommand{TAuthenticationToken}"/> to send.</param>
32 : /// <param name="framework">The framework the <paramref name="command"/> is being sent from.</param>
33 1 : void PrepareCommand<TCommand>(TCommand command, string framework)
34 : where TCommand : ICommand<TAuthenticationToken>;
35 :
36 : /// <summary>
37 : /// Prepares and validates a <see cref="ICommand{TAuthenticationToken}"/> to be sent specifying the framework it is sent via.
38 : /// </summary>
39 : /// <typeparam name="TCommand">The <see cref="Type"/> of<see cref="ICommand{TAuthenticationToken}"/> being sent.</typeparam>
40 : /// <param name="command">The <see cref="ICommand{TAuthenticationToken}"/> to send.</param>
41 : /// <param name="framework">The framework the <paramref name="command"/> is being sent from.</param>
42 1 : bool PrepareAndValidateCommand<TCommand>(TCommand command, string framework)
43 : where TCommand : ICommand<TAuthenticationToken>;
44 :
45 : /// <summary>
46 : /// Deserialises and processes the <paramref name="messageBody"/> received from the network through the provided <paramref name="receiveCommandHandler"/>.
47 : /// </summary>
48 : /// <param name="messageBody">A serialised <see cref="IMessage"/>.</param>
49 : /// <param name="receiveCommandHandler">The handler method that will process the <see cref="ICommand{TAuthenticationToken}"/>.</param>
50 : /// <param name="messageId">The network id of the <see cref="IMessage"/>.</param>
51 : /// <param name="signature">The signature of the <see cref="IMessage"/>.</param>
52 : /// <param name="signingTokenConfigurationKey">The configuration key for the signing token as used by <see cref="IConfigurationManager"/>.</param>
53 : /// <param name="skippedAction">The <see cref="Action"/> to call when the <see cref="ICommand{TAuthenticationToken}"/> is being skipped.</param>
54 : /// <param name="lockRefreshAction">The <see cref="Action"/> to call to refresh the network lock.</param>
55 : /// <returns>The <see cref="ICommand{TAuthenticationToken}"/> that was processed.</returns>
56 1 : ICommand<TAuthenticationToken> ReceiveCommand(string messageBody, Func<ICommand<TAuthenticationToken>, bool?> receiveCommandHandler, string messageId, string signature, string signingTokenConfigurationKey, Action skippedAction = null, Action lockRefreshAction = null);
57 :
58 : /// <summary>
59 : /// The default command handler that
60 : /// check if the <see cref="ICommand{TAuthenticationToken}"/> has already been processed by this framework,
61 : /// checks if the <see cref="ICommand{TAuthenticationToken}"/> is required,
62 : /// finds the handler from the provided <paramref name="routeManager"/>.
63 : /// </summary>
64 : /// <param name="command">The <see cref="ICommand{TAuthenticationToken}"/> to process.</param>
65 : /// <param name="routeManager">The <see cref="RouteManager"/> to get the <see cref="ICommandHandler{TAuthenticationToken,TCommand}"/> from.</param>
66 : /// <param name="framework">The current framework.</param>
67 : /// <returns>
68 : /// True indicates the <paramref name="command"/> was successfully handled by a handler.
69 : /// False indicates the <paramref name="command"/> wasn't handled, but didn't throw an error, so by convention, that means it was skipped.
70 : /// Null indicates the command<paramref name="command"/> wasn't handled as it was already handled.
71 : /// </returns>
72 1 : bool? DefaultReceiveCommand(ICommand<TAuthenticationToken> command, RouteManager routeManager, string framework);
73 :
74 : /// <summary>
75 : /// Prepares an <see cref="IEvent{TAuthenticationToken}"/> to be sent specifying the framework it is sent via.
76 : /// </summary>
77 : /// <typeparam name="TEvent">The <see cref="Type"/> of<see cref="IEvent{TAuthenticationToken}"/> being sent.</typeparam>
78 : /// <param name="event">The <see cref="IEvent{TAuthenticationToken}"/> to send.</param>
79 : /// <param name="framework">The framework the <paramref name="event"/> is being sent from.</param>
80 1 : void PrepareEvent<TEvent>(TEvent @event, string framework)
81 : where TEvent : IEvent<TAuthenticationToken>;
82 :
83 : /// <summary>
84 : /// Prepares and validates an <see cref="IEvent{TAuthenticationToken}"/> to be sent specifying the framework it is sent via.
85 : /// </summary>
86 : /// <typeparam name="TEvent">The <see cref="Type"/> of<see cref="IEvent{TAuthenticationToken}"/> being sent.</typeparam>
87 : /// <param name="event">The <see cref="IEvent{TAuthenticationToken}"/> to send.</param>
88 : /// <param name="framework">The framework the <paramref name="event"/> is being sent from.</param>
89 1 : bool PrepareAndValidateEvent<TEvent>(TEvent @event, string framework)
90 : where TEvent : IEvent<TAuthenticationToken>;
91 :
92 : /// <summary>
93 : /// Deserialises and processes the <paramref name="messageBody"/> received from the network through the provided <paramref name="receiveEventHandler"/>.
94 : /// </summary>
95 : /// <param name="messageBody">A serialised <see cref="IMessage"/>.</param>
96 : /// <param name="receiveEventHandler">The handler method that will process the <see cref="IEvent{TAuthenticationToken}"/>.</param>
97 : /// <param name="messageId">The network id of the <see cref="IMessage"/>.</param>
98 : /// <param name="signature">The signature of the <see cref="IMessage"/>.</param>
99 : /// <param name="signingTokenConfigurationKey">The configuration key for the signing token as used by <see cref="IConfigurationManager"/>.</param>
100 : /// <param name="skippedAction">The <see cref="Action"/> to call when the <see cref="IEvent{TAuthenticationToken}"/> is being skipped.</param>
101 : /// <param name="lockRefreshAction">The <see cref="Action"/> to call to refresh the network lock.</param>
102 : /// <returns>The <see cref="IEvent{TAuthenticationToken}"/> that was processed.</returns>
103 1 : IEvent<TAuthenticationToken> ReceiveEvent(string messageBody, Func<IEvent<TAuthenticationToken>, bool?> receiveEventHandler, string messageId, string signature, string signingTokenConfigurationKey, Action skippedAction = null, Action lockRefreshAction = null);
104 :
105 : /// <summary>
106 : /// Refreshes the network lock.
107 : /// </summary>
108 1 : void RefreshLock(CancellationTokenSource brokeredMessageRenewCancellationTokenSource, BrokeredMessage message, string type = "message");
109 :
110 : /// <summary>
111 : /// The default event handler that
112 : /// check if the <see cref="IEvent{TAuthenticationToken}"/> has already been processed by this framework,
113 : /// checks if the <see cref="IEvent{TAuthenticationToken}"/> is required,
114 : /// finds the handler from the provided <paramref name="routeManager"/>.
115 : /// </summary>
116 : /// <param name="event">The <see cref="IEvent{TAuthenticationToken}"/> to process.</param>
117 : /// <param name="routeManager">The <see cref="RouteManager"/> to get the <see cref="IEventHandler{TAuthenticationToken,TCommand}"/> from.</param>
118 : /// <param name="framework">The current framework.</param>
119 : /// <returns>
120 : /// True indicates the <paramref name="event"/> was successfully handled by a handler.
121 : /// False indicates the <paramref name="event"/> wasn't handled, but didn't throw an error, so by convention, that means it was skipped.
122 : /// Null indicates the <paramref name="event"/> wasn't handled as it was already handled.
123 : /// </returns>
124 1 : bool? DefaultReceiveEvent(IEvent<TAuthenticationToken> @event, RouteManager routeManager, string framework);
125 :
126 : /// <summary>
127 : /// Manually registers the provided <paramref name="handler"/>
128 : /// on the provided <paramref name="routeManger"/>
129 : /// </summary>
130 : /// <typeparam name="TMessage">The <see cref="Type"/> of <see cref="IMessage"/> the <paramref name="handler"/> can handle.</typeparam>
131 1 : void RegisterHandler<TMessage>(ITelemetryHelper telemetryHelper, RouteManager routeManger, Action<TMessage> handler, Type targetedType, bool holdMessageLock = true)
132 : where TMessage : IMessage;
133 :
134 : /// <summary>
135 : /// Register an event handler that will listen and respond to all events.
136 : /// </summary>
137 1 : void RegisterGlobalEventHandler<TMessage>(ITelemetryHelper telemetryHelper, RouteManager routeManger, Action<TMessage> handler, bool holdMessageLock = true)
138 : where TMessage : IMessage;
139 : }
140 : }
|