Skip to content

Instantly share code, notes, and snippets.

@seburgi
Created January 13, 2012 17:21
Show Gist options
  • Save seburgi/1607610 to your computer and use it in GitHub Desktop.
Save seburgi/1607610 to your computer and use it in GitHub Desktop.
A wrapper for IAtomicWriter (see Lokad.CQRS.Sample) that calls a provided action after successful updates/deletes
internal class PublishingAtomicWriter<TKey, TEntity> : IAtomicWriter<TKey, TEntity> where TEntity : class
{
private readonly IClientNotificationService _clients;
private readonly IAtomicWriter<TKey, TEntity> _writer;
private readonly Action<TKey, TEntity> _publishAction;
public PublishingAtomicWriter(IAtomicWriter<TKey, TEntity> writer, Action<TKey, TEntity> publishAction)
{
if (publishAction == null) throw new ArgumentNullException("publishAction");
_writer = writer;
_publishAction = publishAction;
}
public TEntity AddOrUpdate(TKey key, Func<TEntity> addFactory, Func<TEntity, TEntity> update, AddOrUpdateHint hint = AddOrUpdateHint.ProbablyExists)
{
var entity = _writer.AddOrUpdate(key, addFactory, update, hint);
if(entity != null)
_publishAction(key, entity);
return entity;
}
public bool TryDelete(TKey key)
{
var success = _writer.TryDelete(key);
if(success)
_publishAction(key, null);
return success;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment