Created
October 23, 2013 14:16
Revisions
-
glikoz created this gist
Oct 23, 2013 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,163 @@ /// <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 }