Skip to content

Instantly share code, notes, and snippets.

@jpolvora
Created November 8, 2011 21:16
Show Gist options
  • Save jpolvora/1349260 to your computer and use it in GitHub Desktop.
Save jpolvora/1349260 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Data;
namespace Metavision.Infra.Data
{
public abstract class UnitOfWorkBase : IDisposable
{
protected readonly Stack<IRepositorio> StackRepositories
= new Stack<IRepositorio>();
protected IDbTransaction CurrentTransaction;
protected readonly Dictionary<Type, object> Sets
= new Dictionary<Type, object>();
/// <summary>
/// Deve anexar um repositório
/// </summary>
/// <param name="repositorio"></param>
protected internal void AttachRepository(IRepositorio repositorio)
{
StackRepositories.Push(repositorio);
}
protected void SetHasChangesToFalse()
{
foreach (var rep in StackRepositories)
{
rep.HasChanges = false;
}
}
public int AttachedRepositories
{
get { return StackRepositories.Count; }
}
/// <summary>
/// Deve indicar se há uma transação aberta com o banco de dados
/// </summary>
public bool InTransaction
{
get
{
return
CurrentTransaction != null &&
CurrentTransaction.Connection != null &&
CurrentTransaction.Connection.State == ConnectionState.Open;
}
}
/// <summary>
/// Deve iniciar uma transação
/// </summary>
internal abstract void StartTransaction();
#region Implementation of IDisposable
public virtual void Dispose()
{
while (StackRepositories.Count > 0)
{
StackRepositories.Pop().Dispose();
}
if (InTransaction) CurrentTransaction.Dispose();
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment