多个 UnitOfWorks、ISession 和存储库
你知道某个地方有一个很好的例子吗?
我想要的是以下内容:
using(IUnitOfWork uow = UnitOfWork.Start())
{
repositoryA.Add(insanceOfA);
repositoryB.Add(instanceOfB);
uow.Commit();
}
但是这种情况太理想了,因为我还想要多个UnitOfWorks(大多数情况下每个演示者,这对于网络应用程序来说不)。在这种情况下,如果我不明确地向存储库提供信息,那么存储库将如何知道要使用哪个 UnitOfWork。
repository.UnitOfWork = uow;
我的目标是让每个 UnitOfWork 后面都包含一个 ISession,因此每个演示者一个 UOW,每个演示者一个 ISession。注意:我知道 ISession 本质上是 UOW,但我必须以这种方式进行...
任何建议都表示赞赏
Do you know a good example somewhere for this?
What I would like is the following:
using(IUnitOfWork uow = UnitOfWork.Start())
{
repositoryA.Add(insanceOfA);
repositoryB.Add(instanceOfB);
uow.Commit();
}
But this case is too ideal, because what I also want is multiple UnitOfWorks (per presenter in most cases, and this is not for a web app). In that case, how will repositories know which UnitOfWork to use if I don't give them that explicitly like
repository.UnitOfWork = uow;
My goal is to have every UnitOfWork contain a ISession behind, so one UOW per presenter, one ISession per presenter. Note: I know that ISession is esentially UOW, but I must proceed this way...
Any advices are appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我所做的是将应该使用的工作单元传递给存储库。
为此,您可以确保必须在存储库的构造函数中传递存储库必须使用的 UnitOfWork 实例。
为了使其更加用户友好,您可以在您的 UnitOfWork 上创建特定于您的项目的扩展方法(假设您的 UnitOfWork 是通用的并在多个项目中使用),如下所示:
然后,它使您能够为此:
What I do, is pass the unitOfWork that should be used to the Repository.
To achieve this, you can make sure that you have to pass the UnitOfWork instance that must be used by the repository in the repository's constructor.
In order to make it all a bit more user-friendly, you can create extension methods on your UnitOfWork that are specific for your project (supposing that your UnitOfWork is generic and used in multiple projects) that look like this:
Then, it enables you to do this: