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.Collections.Generic;
11 : using System.Linq;
12 : using Cqrs.Domain;
13 : using Cqrs.Messages;
14 :
15 : namespace Cqrs.Events
16 : {
17 : /// <summary>
18 : /// An <see cref="IEventStore{TAuthenticationToken}"/> that uses a local (non-static) <see cref="IDictionary{TKey,TValue}"/>.
19 : /// This does not manage memory in any way and will continue to grow. Mostly suitable for running tests or short lived processes.
20 : /// </summary>
21 : /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
22 : public class InProcessEventStore<TAuthenticationToken> : IEventStore<TAuthenticationToken>
23 1 : {
24 : /// <summary>
25 : /// Gets or sets the in-memory storage <see cref="IDictionary{TKey,TValue}"/>.
26 : /// </summary>
27 : protected IDictionary<Guid, IList<IEvent<TAuthenticationToken>>> InMemoryDb { get; private set; }
28 :
29 : /// <summary>
30 : /// Instantiate a new instance of the <see cref="InProcessEventStore{TAuthenticationToken}"/> class.
31 : /// </summary>
32 1 : public InProcessEventStore()
33 : {
34 : InMemoryDb = new Dictionary<Guid, IList<IEvent<TAuthenticationToken>>>();
35 : }
36 :
37 : /// <summary>
38 : /// Saves the provided <paramref name="event"/>.
39 : /// </summary>
40 : /// <param name="aggregateRootType"> <see cref="Type"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/> the <see cref="IEvent{TAuthenticationToken}"/> was raised in.</param>
41 : /// <param name="event">The <see cref="IEvent{TAuthenticationToken}"/> to be saved.</param>
42 1 : public void Save(Type aggregateRootType, IEvent<TAuthenticationToken> @event)
43 : {
44 : IList<IEvent<TAuthenticationToken>> list;
45 : InMemoryDb.TryGetValue(@event.Id, out list);
46 : if (list == null)
47 : {
48 : list = new List<IEvent<TAuthenticationToken>>();
49 : InMemoryDb.Add(@event.Id, list);
50 : }
51 : list.Add(@event);
52 : }
53 :
54 : /// <summary>
55 : /// Gets a collection of <see cref="IEvent{TAuthenticationToken}"/> for the <typeparamref name="T">aggregate root</typeparamref> with the ID matching the provided <paramref name="aggregateId"/>.
56 : /// </summary>
57 : /// <typeparam name="T">The <see cref="Type"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/> the <see cref="IEvent{TAuthenticationToken}"/> was raised in.</typeparam>
58 : /// <param name="aggregateId">The <see cref="IAggregateRoot{TAuthenticationToken}.Id"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/>.</param>
59 : /// <param name="useLastEventOnly">Loads only the last event<see cref="IEvent{TAuthenticationToken}"/>.</param>
60 : /// <param name="fromVersion">Load events starting from this version</param>
61 1 : public IEnumerable<IEvent<TAuthenticationToken>> Get<T>(Guid aggregateId, bool useLastEventOnly = false, int fromVersion = -1)
62 : {
63 : return Get(typeof(T), aggregateId, useLastEventOnly, fromVersion);
64 : }
65 :
66 : /// <summary>
67 : /// Gets a collection of <see cref="IEvent{TAuthenticationToken}"/> for the <see cref="IAggregateRoot{TAuthenticationToken}"/> of type <paramref name="aggregateType"/> with the ID matching the provided <paramref name="aggregateId"/>.
68 : /// </summary>
69 : /// <param name="aggregateType"> <see cref="Type"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/> the <see cref="IEvent{TAuthenticationToken}"/> was raised in.</param>
70 : /// <param name="aggregateId">The <see cref="IAggregateRoot{TAuthenticationToken}.Id"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/>.</param>
71 : /// <param name="useLastEventOnly">Loads only the last event<see cref="IEvent{TAuthenticationToken}"/>.</param>
72 : /// <param name="fromVersion">Load events starting from this version</param>
73 1 : public IEnumerable<IEvent<TAuthenticationToken>> Get(Type aggregateType, Guid aggregateId, bool useLastEventOnly = false, int fromVersion = -1)
74 : {
75 : IList<IEvent<TAuthenticationToken>> events;
76 : InMemoryDb.TryGetValue(aggregateId, out events);
77 : return events != null
78 : ? events.Where(x => x.Version > fromVersion)
79 : : new List<IEvent<TAuthenticationToken>>();
80 : }
81 :
82 : /// <summary>
83 : /// Get all <see cref="IEvent{TAuthenticationToken}"/> instances for the given <paramref name="correlationId"/>.
84 : /// </summary>
85 : /// <param name="correlationId">The <see cref="IMessage.CorrelationId"/> of the <see cref="IEvent{TAuthenticationToken}"/> instances to retrieve.</param>
86 1 : public IEnumerable<EventData> Get(Guid correlationId)
87 : {
88 : return Enumerable.Empty<EventData>();
89 : }
90 :
91 : /// <summary>
92 : /// Saves the provided <paramref name="event"/>.
93 : /// </summary>
94 : /// <typeparam name="T">The <see cref="Type"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/> the <see cref="IEvent{TAuthenticationToken}"/> was raised in.</typeparam>
95 : /// <param name="event">The <see cref="IEvent{TAuthenticationToken}"/> to be saved.</param>
96 1 : public void Save<T>(IEvent<TAuthenticationToken> @event)
97 : {
98 : Save(typeof(T), @event);
99 : }
100 : }
101 : }
|