Skip to content

Instantly share code, notes, and snippets.

@glikoz
Created October 23, 2013 14:16
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 glikoz/7119628 to your computer and use it in GitHub Desktop.
Save glikoz/7119628 to your computer and use it in GitHub Desktop.
/// <summary>
/// Represents a context class for db operations
/// </summary>
public class DbOperationContext : IDisposable
{
#region Members
private object syncRoot = new object();
private int _TransactionCount;
private Dictionary<Type, object> _TypedClients = new Dictionary<Type, object>();
private IRedisTransaction _Transaction;
#endregion
#region Properties
/// <summary>
/// Gets or sets the repository
/// </summary>
public RedisRepository Repository { get; internal set; }
/// <summary>
/// Gets or sets the client
/// </summary>
public IRedisClient Client { get; internal set; }
public IRedisClient ReadOnlyClient { get; internal set; }
/// <summary>
/// Gets or sets the transaction
/// </summary>
public IRedisTransaction Transaction
{
get
{
return _Transaction;
}
}
#endregion
#region Initialization
/// <summary>
/// Initialize a new instance of this class
/// </summary>
public DbOperationContext(RedisRepository repository)
{
Repository = repository;
Client = repository.GetClient();
ReadOnlyClient = repository.GetReadOnlyClient();
}
#endregion
#region Public Methods
/// <summary>
/// Gets associated typed client
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public IRedisTypedClient<T> As<T>()
{
Type type = typeof(T);
if (!_TypedClients.ContainsKey(type))
{
_TypedClients.Add(type, Client.As<T>());
}
return _TypedClients[type]as IRedisTypedClient<T>;
}
/// <summary>
/// Begins transaction if not exists otherwise increase the counter
/// </summary>
/// <returns></returns>
public IRedisTransaction BeginTransaction()
{
lock (syncRoot)
{
if (_TransactionCount == 0)
{
_Transaction = Client.CreateTransaction();
}
_TransactionCount++;
return _Transaction;
}
}
/// <summary>
/// Commits transaction
/// </summary>
public void CommitTransaction()
{
lock (syncRoot)
{
_TransactionCount--;
if (_TransactionCount == 0)
{
_Transaction.Commit();
_Transaction.Dispose();
_Transaction = null;
}
}
}
/// <summary>
/// Rollbacks transaction
/// </summary>
public void RollbackTransaction()
{
lock (syncRoot)
{
if (_Transaction != null)
{
_Transaction.Rollback();
_Transaction.Dispose();
_Transaction = null;
}
_TransactionCount = 0;
}
}
/// <summary>
/// Releases all unmanaged resources
/// </summary>
public void Dispose()
{
Dispose(true);
}
#endregion
#region Protected Methods
/// <summary>
/// Releases all unmanaged resources
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if (_Transaction != null)
{
RollbackTransaction();
}
foreach (var item in _TypedClients)
{
IDisposable value = item.Value as IDisposable;
if (value != null)
{
value.Dispose();
}
}
if (Client != null)
{
Client.Dispose();
ReadOnlyClient.Dispose();
}
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment