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