0

我开发了一个ASP.NET MVC应用程序来管理项目,使用实体框架6.0和存储库设计模式。现在我想集成事务,以确保某些插入/更新数据库操作遵守ACID主体,尤其是原子性主体。与存储库设计模式交易

下面是我的通用仓库的一个片段:

1.通用仓库接口

public interface IGenericRepository<T> : IRepository where T : BaseEntity 
    { 
     void Create(T entity); 
     void Delete(T entity); 
     IEnumerable<T> GetAll(); 
     void Update(T entity); 
    } 

2.通用仓储类

public abstract class GenericRepository<T> : IGenericRepository<T> where T : BaseEntity 
     { 
      protected IContext _context; 
      protected IDbSet<T> _dbset; 

      public GenericRepository(IContext context) 
      { 
       _context = context; 
       _dbset = _context.Set<T>(); 
      } 


      public virtual void Create(T entity) 
      { 
       if (entity == null) 
       { 
        throw new ArgumentNullException("entity"); 
       } 

       _dbset.Add(entity); 
       _context.SaveChanges(); 
      } 


      public virtual void Update(T entity) 
      { 
       if (entity == null) throw new ArgumentNullException("entity"); 
       _context.Entry(entity).State = System.Data.Entity.EntityState.Modified; 
       _context.SaveChanges(); 
      } 

      public virtual void Delete(T entity) 
      { 
       if (entity == null) throw new ArgumentNullException("entity"); 
       _dbset.Remove(entity); 
       _context.SaveChanges(); 
      } 

      public virtual IEnumerable<T> GetAll() 
      { 
       return _dbset.AsEnumerable<T>(); 
      } 
     } 

3.我Icontext实施

public interface IContext 
    { 
     IDbSet<Projet> Projects { get; set; }  
     IDbSet<Task> Tasks{ get; set; } 
     IDbSet<Entite> Entities { get; set; } 

     DbSet<TEntity> Set<TEntity>() where TEntity : class; 
     DbEntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity : class; 

     int SaveChanges(); 
    } 

4.项目实体

public class ProjectRepository : GenericRepository<Projet>, IProjectRepository 
    { 

     IContext _context; 

     public ProjectRepository(IContext context) : base(context) 
     { 
      _context = context; 
      _dbset = _context.Set<Projet>(); 
     } 

     public Projet GetProjectById(int Id) 
     { 
      return _dbset.FirstOrDefault(x=>x.Id == Id); 
     } 

    } 

所以,我想要做的,是有交易与上面的模型工作。 例如,当用他的任务创建项目时,我想使用事务来保存项目和任务实体,所以我确信插入这些实体将是一个原子操作。

感谢您的帮助和建议。

+0

http://stackoverflow.com/questions/575136/transactions-in-the-repository-pattern可能有帮助 – Kaushal

+0

在Stack Overflow中,这个问题过于宽泛或主要是基于观点。由于它的代码本身没有问题,所以[CodeReview](http://codereview.stackexchange.com/help/how-to-ask)可能更适合这个问题。 –

+0

至于意见:'SaveChanges'不属于存储库。你离不开一个工作单位。 –

回答

2

通常,您的存储库被注入到引擎/服务类中。我将假设我们有一个ProjectEngine.cs,其中ProjectRepo和TaskRepo被注入。代码如下所示:

 public class ProjectEngine : IProjectEngine 
     { 
      IProjectRepository projectRepository; 
      ITaskRepository taskRepository; 

      public ProjectEngine(
       IProjectRepository ProjectRepository, 
       ITaskRepository TaskRepository) 
      { 
       projectRepository = ProjectRepository; 
       taskRepository = TaskRepository; 
      } 

      public void CreateProject(CreateProjectRequest createProjectRequest) 
      { 

      using (TransactionScope scope = new TransactionScope()) 
        { 

         // these operations are atomic since they below to same transactionscope 
         projectRepository.Add([project]); 
         taskRepository.Add([tasks]); 
         // data will not be affected until complete operation is called. Any database exception would rollback the transaction. 
         scope.Complete(); 
        } 

       } 

     } 

回顾一下,最好的方法是在同一个transactionscope中包含多个存储库操作。

+0

感谢Houssam,很好的解决方案! –

+0

'TransactionScope'是一个不良设计的解决方案。 –

+0

我想知道更多来自你的Gert。请与我们分享您如何管理EF存储库的交易。 –