Guest User

Untitled

a guest
Apr 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. namespace Cqrs.Persistence
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using Domain;
  6.  
  7. public class DomainUnitOfWork : IUnitOfWork
  8. {
  9. private readonly ICollection<IAggregate> inserted = new HashSet<IAggregate>();
  10. private readonly ICollection<IAggregate> updated = new HashSet<IAggregate>();
  11. private readonly ICollection<IAggregate> deleted = new HashSet<IAggregate>();
  12. private readonly IStoreEvents eventStore;
  13. private bool completed;
  14. private bool disposed;
  15.  
  16. public DomainUnitOfWork(IStoreEvents eventStore)
  17. {
  18. this.eventStore = eventStore;
  19. }
  20.  
  21. public void RegisterNew(IAggregate aggregate)
  22. {
  23. this.inserted.Add(aggregate);
  24. this.updated.Remove(aggregate);
  25. this.deleted.Remove(aggregate);
  26. }
  27. public void RegisterDirty(IAggregate aggregate)
  28. {
  29. this.inserted.Remove(aggregate);
  30. this.updated.Add(aggregate);
  31. this.deleted.Remove(aggregate);
  32. }
  33. public void RegisterDeleted(IAggregate aggregate)
  34. {
  35. this.inserted.Remove(aggregate);
  36. this.updated.Remove(aggregate);
  37. this.deleted.Add(aggregate);
  38. }
  39.  
  40. public void Complete()
  41. {
  42. if (this.deleted.Count > 0)
  43. throw new NotSupportedException();
  44.  
  45. if (this.completed)
  46. return;
  47.  
  48. foreach (var aggregate in this.inserted)
  49. {
  50. this.eventStore.RegisterNewAggregate(aggregate);
  51. this.eventStore.SaveUncommittedEvents(aggregate);
  52. }
  53.  
  54. foreach (var aggregate in this.updated)
  55. this.eventStore.SaveUncommittedEvents(aggregate);
  56.  
  57. this.inserted.Clear();
  58. this.updated.Clear();
  59. this.deleted.Clear();
  60. }
  61.  
  62. public void Dispose()
  63. {
  64. this.Dispose(true);
  65. GC.SuppressFinalize(this);
  66. }
  67. protected virtual void Dispose(bool disposing)
  68. {
  69. if (!disposing || this.disposed)
  70. return;
  71.  
  72. this.disposed = this.completed = true;
  73.  
  74. this.inserted.Clear();
  75. this.updated.Clear();
  76. this.deleted.Clear();
  77. }
  78. }
  79. }
Add Comment
Please, Sign In to add comment