-
Notifications
You must be signed in to change notification settings - Fork 2
/
PersonUpdatedConsumer.cs
88 lines (72 loc) · 3.31 KB
/
PersonUpdatedConsumer.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
#region Usings
using MassTransit;
using Records.Persons.Projection.Persons.Projectors;
using Records.Shared.Infra.Idempotence.Abstractions;
using Records.Shared.Infra.MessageBroker.MassTransit;
using Records.Shared.IntegrationEvents.Persons;
using Records.Shared.Messaging;
using Records.Shared.Projection.Abstractions;
using Serilog;
#endregion
namespace Records.Persons.BackgroundTasks.Consumers;
/// <summary>
/// Represents a MassTransit consumer of the message <see cref="Message{PersonCreatedIntegrationEvent}"/>
/// (TContent:<see cref="PersonCreatedIntegrationEvent"/>).
/// </summary>
public sealed class PersonUpdatedConsumer : ConsumerMetadataSupport<Message<PersonUpdatedIntegrationEvent>>
{
#region Declarations
/// <summary>Contains a session to encapsulate a business transaction which can affect the database.</summary>
private readonly IUnitOfWork _unitOfWork;
/// <summary>Manages proyection operations from "source" database to "projection" (read) database.</summary>
private readonly IPersonUpdatedProjector _projector;
#endregion
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="Message{PersonUpdatedConsumer}"/> class.
/// </summary>
/// <param name="idempotentMessageService">Service to manage idempotent messages.</param>
/// <param name="obsoleteMessageService">Service to prevent obsoletes messages.</param>
/// <param name="unitOfWork">Manage a <see cref="IDbSession"/> to encapsulate a business transaction which can affect the database.</param>
/// <param name="projector">Manages proyection operations from "source" database to "projection" (read) database.</param>
/// <exception cref="ArgumentNullException">When some argument for the constructor parameters is null.</exception>
public PersonUpdatedConsumer(
IIdempotentMessageService idempotentMessageService,
IObsoleteMessageService obsoleteMessageService,
IUnitOfWork unitOfWork,
IPersonUpdatedProjector projector)
: base(idempotentMessageService, obsoleteMessageService)
{
_unitOfWork = unitOfWork;
_projector = projector;
}
#endregion
#region Public methods
/// <summary>
/// Consumes the message.
/// </summary>
/// <param name="context">Context that allow access to details surrounding the inbound message and its metadata, including headers.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public override async Task Consume(ConsumeContext<Message<PersonUpdatedIntegrationEvent>> context)
{
ArgumentNullException.ThrowIfNull(context);
try
{
MessageMetadata messageMetadata = context.Message.Metadata;
if (await NotYetConsumed(messageMetadata) && await NotObsolete(messageMetadata))
{
Log.Information($"[PersonUpdatedConsumer << PersonUpdatedIntegrationEvent] Name => {context.Message.Content.Name}");
// Proyects the Update.
_unitOfWork.BeginTransaction();
await _projector.ProjectAsync(context.Message.Content.Id);
_unitOfWork.Commit();
}
}
catch
{
// TODO: Handle the exception (log, revert, etc.).
throw;
}
}
#endregion
}