Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="cdmdotnet Limited">
4 : // // Copyright cdmdotnet Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using System.Collections.Generic;
11 : using System.Linq;
12 : using cdmdotnet.Logging;
13 :
14 : namespace Cqrs.Events
15 : {
16 : public abstract class EventStore<TAuthenticationToken> : IEventStore<TAuthenticationToken>
17 0 : {
18 : protected const string CqrsEventStoreStreamNamePattern = "{0}/{1}";
19 :
20 : protected IEventBuilder<TAuthenticationToken> EventBuilder { get; set; }
21 :
22 : protected IEventDeserialiser<TAuthenticationToken> EventDeserialiser { get; set; }
23 :
24 : protected ITelemetryHelper TelemetryHelper { get; set; }
25 :
26 : protected ILogger Logger { get; private set; }
27 :
28 0 : protected EventStore(IEventBuilder<TAuthenticationToken> eventBuilder, IEventDeserialiser<TAuthenticationToken> eventDeserialiser, ILogger logger)
29 : {
30 : EventBuilder = eventBuilder;
31 : EventDeserialiser = eventDeserialiser;
32 : Logger = logger;
33 : TelemetryHelper = new NullTelemetryHelper();
34 : }
35 :
36 0 : public virtual void Save<T>(IEvent<TAuthenticationToken> @event)
37 : {
38 : Save(typeof(T), @event);
39 : }
40 :
41 0 : protected virtual string GenerateStreamName(Type aggregateRootType, IEvent<TAuthenticationToken> @event)
42 : {
43 : return GenerateStreamName(aggregateRootType, @event.Id);
44 : }
45 :
46 0 : protected virtual string GenerateStreamName(Type aggregateRootType, Guid aggregateId)
47 : {
48 : return string.Format(CqrsEventStoreStreamNamePattern, aggregateRootType.FullName, aggregateId);
49 : }
50 :
51 0 : public virtual void Save(Type aggregateRootType, IEvent<TAuthenticationToken> @event)
52 : {
53 : Logger.LogDebug(string.Format("Saving aggregate root event type '{0}'", @event.GetType().FullName), string.Format("{0}\\Save", GetType().Name));
54 : EventData eventData = EventBuilder.CreateFrameworkEvent(@event);
55 : string streamName = GenerateStreamName(aggregateRootType, @event);
56 : eventData.AggregateId = streamName;
57 : eventData.AggregateRsn = @event.Id;
58 : eventData.Version = @event.Version;
59 : eventData.CorrelationId = @event.CorrelationId;
60 : PersistEvent(eventData);
61 : Logger.LogInfo(string.Format("Saving aggregate root event type '{0}'... done", @event.GetType().FullName), string.Format("{0}\\Save", GetType().Name));
62 : TelemetryHelper.TrackMetric(string.Format("Cqrs/EventStore/Save/{0}", streamName), 1);
63 : }
64 :
65 0 : public virtual IEnumerable<IEvent<TAuthenticationToken>> Get<T>(Guid aggregateId, bool useLastEventOnly = false, int fromVersion = -1)
66 : {
67 : IEnumerable<IEvent<TAuthenticationToken>> results = Get(typeof (T), aggregateId, useLastEventOnly, fromVersion);
68 : TelemetryHelper.TrackMetric(string.Format("Cqrs/EventStore/Get/{0}", GenerateStreamName(typeof(T), aggregateId)), results.Count());
69 :
70 : return results;
71 : }
72 :
73 0 : public abstract IEnumerable<IEvent<TAuthenticationToken>> Get(Type aggregateRootType, Guid aggregateId, bool useLastEventOnly = false, int fromVersion = -1);
74 :
75 0 : public abstract IEnumerable<EventData> Get(Guid correlationId);
76 :
77 0 : protected abstract void PersistEvent(EventData eventData);
78 : }
79 : }
|