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.Tasks;
11 : using cdmdotnet.Logging;
12 : using Cqrs.Akka.Events;
13 : using Cqrs.Domain;
14 : using Cqrs.Domain.Factories;
15 : using Cqrs.Events;
16 :
17 : namespace Cqrs.Akka.Domain
18 : {
19 : /// <summary>
20 : /// A <see cref="SagaRepository{TAuthenticationToken}"/> that is safe to use within Akka.NET
21 : /// </summary>
22 : /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of authentication token.</typeparam>
23 : public class AkkaSagaRepository<TAuthenticationToken>
24 : : SagaRepository<TAuthenticationToken>
25 : , IAkkaSagaRepository<TAuthenticationToken>
26 1 : {
27 : /// <summary>
28 : /// Gets the <see cref="IAkkaEventPublisherProxy{TAuthenticationToken}"/>
29 : /// </summary>
30 : protected IAkkaEventPublisherProxy<TAuthenticationToken> EventPublisher { get; private set; }
31 :
32 : /// <summary>
33 : /// Instantiates a new instance of <see cref="AkkaSagaRepository{TAuthenticationToken}"/>
34 : /// </summary>
35 1 : public AkkaSagaRepository(IAggregateFactory aggregateFactory, IEventStore<TAuthenticationToken> eventStore, IEventPublisher<TAuthenticationToken> publisher, ICorrelationIdHelper correlationIdHelper, IAkkaEventPublisherProxy<TAuthenticationToken> eventPublisher)
36 : : base(aggregateFactory, eventStore, publisher, correlationIdHelper)
37 : {
38 : EventPublisher = eventPublisher;
39 : }
40 :
41 : #region Overrides of Repository<TAuthenticationToken>
42 :
43 : /// <summary>
44 : /// Calls <see cref="IAggregateFactory.Create"/> to get a, <typeparamref name="TSaga"/>.
45 : /// </summary>
46 : /// <typeparam name="TSaga">The <see cref="Type"/> of <see cref="ISaga{TAuthenticationToken}"/>.</typeparam>
47 : /// <param name="id">The id of the <typeparamref name="TSaga"/> to create.</param>
48 1 : protected override TSaga CreateSaga<TSaga>(Guid id)
49 : {
50 : var saga = SagaFactory.Create<TSaga>();
51 :
52 : return saga;
53 : }
54 :
55 : /// <summary>
56 : /// Publish the saved <paramref name="event"/> asynchronously on <see cref="EventPublisher"/>,
57 : /// then calls <see cref="SagaRepository{TAuthenticationToken}.PublishEvent"/>
58 : /// </summary>
59 1 : protected override void PublishEvent(ISagaEvent<TAuthenticationToken> @event)
60 : {
61 : Task.Factory.StartNewSafely(() =>
62 : {
63 : EventPublisher.Publish(@event);
64 : base.PublishEvent(@event);
65 : });
66 : }
67 :
68 : #endregion
69 : }
70 : }
|