document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2. using System.Data;
  3. using System.Data.Objects;
  4. using System.Linq;
  5.  
  6. namespace Metavision.Infra.Data
  7. {
  8.     /// <summary>
  9.     /// Implementação de UnitOfWork para EntityFramework (ObjectContext)
  10.     /// Basicamente funciona abstraindo as operações do ObjectContext
  11.     /// Permitindo trabalhar com injeção de dependência
  12.     /// </summary>
  13.     public class ObjContextUnitOfWork : UnitOfWorkBase, IUnitOfWork
  14.     {
  15.         #region Members
  16.  
  17.         private readonly ObjectContext _objContext;
  18.         #endregion
  19.  
  20.         #region Ctor
  21.  
  22.         public ObjContextUnitOfWork(ObjectContext objectContext = null)
  23.         {
  24.             _objContext = objectContext ?? Container.GetService<ObjectContext>(false);
  25.         }
  26.  
  27.         #endregion
  28.  
  29.         #region Implementation of IUnitOfWork
  30.  
  31.         public T Find<T>(params object[] keyValues) where T : class, new()
  32.         {
  33.             var setName = GetObjectSet<T>().EntitySet.Name;
  34.             var entityKey = new EntityKey(setName, "Id", keyValues);
  35.             return (T)_objContext.GetObjectByKey(entityKey);
  36.         }
  37.  
  38.         public void Add<T>(T entity) where T : class
  39.         {
  40.             GetObjectSet<T>().AddObject(entity);
  41.         }
  42.  
  43.         public void Remove<T>(T entity) where T : class
  44.         {
  45.             GetObjectSet<T>().DeleteObject(entity);
  46.         }
  47.  
  48.         public IQueryable<T> Get<T>() where T : class
  49.         {
  50.             return GetObjectSet<T>();
  51.         }
  52.  
  53.         private ObjectSet<T> GetObjectSet<T>() where T : class
  54.         {
  55.             ObjectSet<T> set;
  56.  
  57.             if (Sets.ContainsKey(typeof(T)))
  58.                 set = (ObjectSet<T>)Sets[typeof(T)];
  59.             else
  60.             {
  61.                 set = _objContext.CreateObjectSet<T>();
  62.                 Sets[typeof(T)] = set;
  63.             }
  64.  
  65.             return set;
  66.         }
  67.  
  68.         internal override void StartTransaction()
  69.         {
  70.             if (!InTransaction)
  71.             {
  72.                 _objContext.Connection.Close();
  73.                 if (_objContext.Connection.State != ConnectionState.Open)
  74.                 {
  75.                     _objContext.Connection.Open();
  76.                     CurrentTransaction = _objContext.Connection.BeginTransaction();
  77.                 }
  78.             }
  79.         }
  80.  
  81.         public int Commit()
  82.         {
  83.             var hasChanges = StackRepositories.Any(w => w.HasChanges);
  84.  
  85.             if (!hasChanges) return 0;
  86.  
  87.             try
  88.             {
  89.                 StartTransaction();
  90.                 var count = _objContext.SaveChanges();
  91.                 CurrentTransaction.Commit();
  92.                 SetHasChangesToFalse();
  93.                 return count;
  94.             }
  95.             catch (Exception)
  96.             {
  97.                 RollBack();
  98.                 return -1;
  99.             }
  100.         }
  101.  
  102.         public void RollBack()
  103.         {
  104.             SetHasChangesToFalse();
  105.  
  106.  
  107.             if (!InTransaction) return;
  108.            
  109.             CurrentTransaction.Rollback();
  110.         }
  111.  
  112.         #endregion
  113.  
  114.         #region Implementation of IDisposable
  115.  
  116.         public override void Dispose()
  117.         {
  118.             base.Dispose();
  119.  
  120.             _objContext.Dispose();
  121.         }
  122.  
  123.         #endregion
  124.  
  125.     }
  126. }
  127.  
');