Skip to content

Instantly share code, notes, and snippets.

@joliver
Created November 4, 2009 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joliver/226204 to your computer and use it in GitHub Desktop.
Save joliver/226204 to your computer and use it in GitHub Desktop.
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