简体   繁体   English

可以访问存储库中的UnitOfWork是不好的设计吗?

[英]Is it bad design to have access to the UnitOfWork in the Repository?

Background: Entity framework4.1 and MVC4 背景:实体framework4.1和MVC4

My setup has model entities, and then a generic repository and then model specific repositories like UserRepository, ProductRepository that inherit from the generic repository. 我的设置包含模型实体,然后是通用存储库,然后对从通用存储库继承的特定存储库(如UserRepository,ProductRepository)进行建模。

I then have a service layer that use these repositories along with any business logic, and the UnitOfWork object is accessible here to call Commit. 然后,我有了一个使用这些存储库以及任何业务逻辑的服务层,在这里可以访问UnitOfWork对象来调用Commit。

With the Unit of work pattern, and my quesiton is: 使用工作单位模式,我的问题是:

Is it poor design to let the repository layer have access to the UnitOfWork object? 让存储库层可以访问UnitOfWork对象的设计不当吗?

To me this seems to be a 'leak' because now things may be commit when you don't even realize it. 对我来说,这似乎是一个“泄漏”,因为当您什至没有意识到时,事情可能会发生。

is this correct? 这个对吗?

eg 例如

public class ProductService
{


   public void SaveProduct(Product product)
   {
      try
      {
        productRepository.Save(product);
        statsRepository.Update(product);
        this.UnitOfWork.Commit();
      } catch(..) 
      {
            //
      }
      finally()
      {
        //
      }


   }

}

Now if any one of those calls fails, commit won't be called. 现在,如果这些调用中的任何一个失败,将不会调用commit。

But if in the abcRepository layer you had access to the UofW object and called commit, it would behave in a inconsistant manner. 但是,如果在abcRepository层中可以访问UofW对象并称为commit,则它的行为将不一致。

I think that is the right way of implementing repository and UnitofWork 我认为这是实现存储库和UnitofWork的正确方法

http://code.cmsstores.com/implementing-unit-of-work-pattern-dot-net/ http://code.cmsstores.com/implementing-unit-of-work-pattern-dot-net/

I think the repository should not access the uow, as Matthew already commented. 正如马修(Matthew)所说,我认为存储库不应该访问uow。 Your question pretty much answers itself. 您的问题几乎可以自己回答。

Re: Now if any one of those calls fails, commit won't be called. 回复: 现在,如果这些调用中的任何一个失败,将不会调用commit。 - --

This is, generally, good, right? 一般来说,这很好,对吗? You don't want to commit your changes partially. 您不想部分提交更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM