Created
November 4, 2009 16:52
-
-
Save joliver/226204 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment