Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Cqrs.Persistence
- {
- using System;
- using System.Collections.Generic;
- using Domain;
- public class DomainUnitOfWork : IUnitOfWork
- {
- private readonly ICollection<IAggregate> inserted = new HashSet<IAggregate>();
- private readonly ICollection<IAggregate> updated = new HashSet<IAggregate>();
- private readonly ICollection<IAggregate> deleted = new HashSet<IAggregate>();
- private readonly IStoreEvents eventStore;
- private bool completed;
- private bool disposed;
- public DomainUnitOfWork(IStoreEvents eventStore)
- {
- this.eventStore = eventStore;
- }
- public void RegisterNew(IAggregate aggregate)
- {
- this.inserted.Add(aggregate);
- this.updated.Remove(aggregate);
- this.deleted.Remove(aggregate);
- }
- public void RegisterDirty(IAggregate aggregate)
- {
- this.inserted.Remove(aggregate);
- this.updated.Add(aggregate);
- this.deleted.Remove(aggregate);
- }
- public void RegisterDeleted(IAggregate aggregate)
- {
- this.inserted.Remove(aggregate);
- this.updated.Remove(aggregate);
- this.deleted.Add(aggregate);
- }
- public void Complete()
- {
- if (this.deleted.Count > 0)
- throw new NotSupportedException();
- if (this.completed)
- return;
- foreach (var aggregate in this.inserted)
- {
- this.eventStore.RegisterNewAggregate(aggregate);
- this.eventStore.SaveUncommittedEvents(aggregate);
- }
- foreach (var aggregate in this.updated)
- this.eventStore.SaveUncommittedEvents(aggregate);
- this.inserted.Clear();
- this.updated.Clear();
- this.deleted.Clear();
- }
- public void Dispose()
- {
- this.Dispose(true);
- GC.SuppressFinalize(this);
- }
- protected virtual void Dispose(bool disposing)
- {
- if (!disposing || this.disposed)
- return;
- this.disposed = this.completed = true;
- this.inserted.Clear();
- this.updated.Clear();
- this.deleted.Clear();
- }
- }
- }
Add Comment
Please, Sign In to add comment