PageRenderTime 5ms CodeModel.GetById 1ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Bifrost/Events/EventStore.cs

#
C# | 61 lines | 46 code | 5 blank | 10 comment | 0 complexity | d8d00f70763ea9d66cebb02c9d361a42 MD5 | raw file
 1using System;
 2using Bifrost.Execution;
 3using Bifrost.Globalization;
 4
 5namespace Bifrost.Events
 6{
 7	/// <summary>
 8	/// Represents an <see cref="IEventStore"/>
 9	/// </summary>
10    public class EventStore : IEventStore
11    {
12        readonly IEventRepository _repository;
13        readonly IEventStoreChangeManager _eventStoreChangeManager;
14        readonly IEventSubscriptionManager _eventSubscriptionManager;
15	    readonly ILocalizer _localizer;
16
17	    /// <summary>
18	    /// Initializes a new instance of <see cref="EventStore"/>
19	    /// </summary>
20	    /// <param name="repository"><see cref="IEventRepository"/> that persists events</param>
21        /// <param name="eventStoreChangeManager">A <see cref="IEventStoreChangeManager"/> for managing changes to the event store</param>
22        /// <param name="eventSubscriptionManager">A <see cref="IEventSubscriptionManager"/> for managing event subscriptions</param>
23	    /// <param name="localizer"><see cref="ILocalizer" /> that ensures thread has the correct culture.</param>
24	    public EventStore(
25            IEventRepository repository, 
26            IEventStoreChangeManager eventStoreChangeManager, 
27            IEventSubscriptionManager eventSubscriptionManager,
28            ILocalizer localizer)
29        {
30            _repository = repository;
31            _eventStoreChangeManager = eventStoreChangeManager;
32            _eventSubscriptionManager = eventSubscriptionManager;
33		    _localizer = localizer;
34        }
35
36#pragma warning disable 1591 // Xml Comments
37		public CommittedEventStream Load(Type aggregatedRootType, Guid aggregateId)
38        {
39            var events = _repository.GetForAggregatedRoot(aggregatedRootType, aggregateId);
40            var stream = new CommittedEventStream(aggregateId);
41            stream.Append(events);
42            return stream;
43        }
44
45        public void Save(UncommittedEventStream eventsToSave)
46        {
47            using (_localizer.BeginScope())
48            {
49                _repository.Insert(eventsToSave);
50                _eventSubscriptionManager.Process(eventsToSave);
51                _eventStoreChangeManager.NotifyChanges(this);
52            }
53		}
54
55	    public EventSourceVersion GetLastCommittedVersion(Type aggregatedRootType, Guid aggregateId)
56	    {
57	        return _repository.GetLastCommittedVersion(aggregatedRootType, aggregateId);
58	    }
59#pragma warning restore 1591 // Xml Comments
60	}
61}