传递给方法的工作单元

Unit of work passed to methods

我正在使用 uow 和存储库模式。

我在配置从主要方法调用的方法中执行查询的正确方法时遇到问题。

明确一点: 我有一些方法:

GetSomthingFromDB(int someId)
{
       using (var uow = new UnitOfWork())
       {
         //Get the study from Db withrelated Entities
                IRepository<Study> studyRepository 
                =uow.GetRepository<Study();
         Foreach(var finding in study.Findings) 
         { 
            SomeSubMethod(finding);
         }
       }
}

SomeSubMethod(Finding finding)
{
  //Do all kind of stuff on a finding
}

好的,现在在 SomeSubMethod() 中,我需要从 Db 中获取一些东西,让我们从配置中获取一些东西 table。

但是我在subMethod中没有uow(可能是subMethods调用了另一个subMethod,只有在那里我又需要uow。

我应该将 uow 从一种方法传递到另一种方法吗?

我应该通过存储库内部和内部吗?

我应该使用 uow 作为 class 会员吗?

最佳做法是什么?

谢谢。总计

一般信息:

  • 尽可能缩短连接生命周期
  • 如果您使用的是 WPF 或 ASP 视图,那么只要视图处于活动状态,您就可以保持连接 (uow) 活动。

  • 使用 SRP 设计您的方法

Should I pass uow from one Method to the other?

是的,有时您需要将 unitOfWork 表单方法传递给其他表单方法。

  • 您可以私下传递它,没有任何问题
  • 如果将它传递给 public 方法,则必须注意方法作用域的使用以避免任何并发问题。

Should I pass the repository inner and inner?

同样这里 repostiory 属于一个连接,如果你的方法是 public 并且从不同的 layers/threads 调用,那么你可能会遇到一些并发问题。

Should I Use uow as a class member?

是的,最好的办法是将它作为参数传递给构造函数并且不带 setter。