8
8
namespace RoyalCode . Events . Outbox . Abstractions . Services . Defaults ;
9
9
10
10
/// <summary>
11
- /// Default implementation for <see cref="IOutboxService"/>.
11
+ /// Abstract implementation for <see cref="IOutboxService"/>.
12
12
/// </summary>
13
- public sealed class OutboxService : IOutboxService
13
+ public abstract class OutboxServiceBase : IOutboxService
14
14
{
15
15
private readonly OutboxOptions options ;
16
- private readonly ICreateMessageHandler createMessageHandler ;
17
16
private readonly IMessageDispatcher dispatcher ;
18
17
19
18
/// <summary>
20
- /// Creates a new instance of <see cref="OutboxService "/>.
19
+ /// Creates a new instance of <see cref="OutboxServiceBase "/>.
21
20
/// </summary>
22
21
/// <param name="options">The outbox options.</param>
23
- /// <param name="createMessageHandler">The handler to create new messages.</param>
24
22
/// <param name="dispatcher">The dispatcher of messagens.</param>
25
- public OutboxService (
23
+ protected OutboxServiceBase (
26
24
IOptions < OutboxOptions > options ,
27
- ICreateMessageHandler createMessageHandler ,
28
25
IMessageDispatcher dispatcher )
29
26
{
30
27
this . options = options . Value ;
31
- this . createMessageHandler = createMessageHandler ;
32
28
this . dispatcher = dispatcher ;
33
29
}
34
30
31
+ /// <summary>
32
+ /// The handler to create new messages.
33
+ /// </summary>
34
+ protected abstract ICreateMessageHandler CreateMessageHandler { get ; }
35
+
35
36
/// <inheritdoc />
36
37
public void Write ( object message )
37
38
{
@@ -40,12 +41,11 @@ public void Write(object message)
40
41
var messageType = message . GetType ( ) ;
41
42
42
43
if ( ! options . Types . TryGetValue ( messageType , out var metadata ) )
43
- throw new InvalidOperationException (
44
- $ "O Tipo { messageType . Name } não foi configurado, é necessário configurar o tipo antes de escrever uma mensagem.") ;
44
+ throw new MessateTypeNotConfiguredException ( messageType ) ;
45
45
46
- var settings = metadata . SerializerOptions ?? options . SerializerOptions ;
47
-
48
- var json = JsonSerializer . Serialize ( message , settings ) ;
46
+ var json = metadata . JsonTypeInfo is not null
47
+ ? JsonSerializer . Serialize ( metadata , metadata . JsonTypeInfo )
48
+ : JsonSerializer . Serialize ( message , metadata . SerializerOptions ?? options . SerializerOptions ) ;
49
49
50
50
var request = new CreateMessage ( )
51
51
{
@@ -54,7 +54,7 @@ public void Write(object message)
54
54
Payload = json ,
55
55
} ;
56
56
57
- createMessageHandler . Handle ( request ) . EnsureSuccess ( ) ;
57
+ CreateMessageHandler . Handle ( request ) . EnsureSuccess ( ) ;
58
58
}
59
59
60
60
/// <inheritdoc />
@@ -66,11 +66,11 @@ public async Task DispatchAsync(IEnumerable<OutboxMessage> messages, Cancellatio
66
66
. FirstOrDefault (
67
67
x => x . TypeName == message . MessageType
68
68
&& x . Version == message . VersionType )
69
- ?? throw new InvalidOperationException ( $ "O tipo { message . MessageType } não foi encontrado.") ;
70
-
71
- var settings = metadata . SerializerOptions ?? options . SerializerOptions ;
69
+ ?? throw new MessateTypeNotConfiguredException ( message . MessageType ) ;
72
70
73
- var payload = JsonSerializer . Deserialize ( message . Payload , metadata . PayloadType , settings ) ! ;
71
+ var payload = metadata . JsonTypeInfo is not null
72
+ ? JsonSerializer . Deserialize ( message . Payload , metadata . JsonTypeInfo ) !
73
+ : JsonSerializer . Deserialize ( message . Payload , metadata . PayloadType , metadata . SerializerOptions ?? options . SerializerOptions ) ! ;
74
74
75
75
await dispatcher . DispatchAsync ( payload , ct ) ;
76
76
}
0 commit comments