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