简体   繁体   English

如何使用 DDD 和存储库模式处理事务?

[英]How handle Transactions with DDD and Repository Pattern?

I am refactoring a project to bring it closer to DDD.我正在重构一个项目以使其更接近 DDD。 The project is written in VB.Net and use WinForms to UI.该项目是用VB.Net编写的,并使用WinForms到UI。 I have been learning about DDD and I still don't have some things very clear.一直在学习DDD,有些东西还是不是很清楚。

I use the repository pattern and SqlKata to create and execute SQL Sentences.我使用存储库模式和 SqlKata 来创建和执行 SQL 语句。 I read about why transactions should be handle in Application Services, but I think about some cases which to handle transaction in repositories could be a good idea.我阅读了为什么应该在应用程序服务中处理事务,但我认为在某些情况下处理存储库中的事务可能是一个好主意。 An example:一个例子:

A domain model Order with a list property Lines .具有列表属性Lines 的域模型Order A service called FindOrderService .一个名为FindOrderService的服务。 The service use the OrderRepository and its function .FindById(...) .该服务使用OrderRepository及其函数.FindById(...) This repository return Order with its Lines .此存储库返回Order及其Lines In Database Order and OrderLines are two different tables.在数据库中OrderOrderLines是两个不同的表。

I. Shouldn't the repository function handle a transaction by having to use two tables to make sure the domain object is created in a consistent state? I.存储库功能是否应该通过使用两个表来确保在一致状态下创建域对象来处理事务? With a function .Add(...) Wouldn't the same thing happen?使用函数.Add(...)不会发生同样的事情吗?

II.二、 Application services may or may not be used in a transactional manner.应用服务可能会也可能不会以事务方式使用。 But they don't know how many tables are used to persist an Aggregate.但是他们不知道有多少表用于持久化聚合。 Wouldn't ensuring something like this be a matter of Infrastructure even though it can also be handled from the application services?即使它也可以从应用程序服务中处理,确保这样的事情难道不是基础设施的问题吗?

III.三、 From what I've read.从我读过的。 It seems that exist two terms: "Bussines transaction" and "Technical transaction" (in this post ).似乎存在两个术语:“业务交易”和“技术交易”(在这篇文章中)。 Are this exaples technical transactions?这是技术交易的例子吗?

Thanks in advance!提前致谢!

I read about why transactions should be handle in Application Services, but I think about some cases which to handle transaction in repositories could be a good idea.我阅读了为什么应该在应用程序服务中处理事务,但我认为在某些情况下处理存储库中的事务可能是一个好主意。

The fact that a specific scenario allows to have things done in two different ways does not mean we should choose the "rule breaker" way: having a single repository in the context of a transaction does not justify making the repository own the transaction.特定场景允许以两种不同的方式完成事情的事实并不意味着我们应该选择“规则破坏者”的方式:在事务上下文中拥有单个存储库并不能证明让存储库拥有事务是合理的。 A transaction should span over one or more repositories by design.根据设计,事务应该跨越一个或多个存储库。

I . A transaction can be abstracted away in application level, yet "contain" a single repository that queries/updates two tables in order to keep consistency.事务可以在应用程序级别被抽象出来,但“包含”一个查询/更新两个表以保持一致性的存储库。

II . Transactions, repositories, aggregates - all of them should be abstracted away in application level.事务、存储库、聚合——所有这些都应该在应用程序级别被抽象掉。 True, infrastructure level is where you create concrete implementations (technical transaction, several tables, some entities), but application does not care that a single unit-of-work (transaction) spans over four repositories which actually "hide" eight tables.确实,基础设施级别是您创建具体实现(技术事务、多个表、某些实体)的地方,但应用程序并不关心单个工作单元(事务)跨越四个实际上“隐藏”八个表的存储库。

III .. Business transaction and technical transaction can overlap, but don't have to.业务交易和技术交易可以重叠,但不是必须的。 For example, you can have a business process within which multiple technical transactions are executed (a single process may operate on different databases), composing together a single business transaction.例如,您可以有一个业务流程,其中执行多个技术事务(单个流程可能对不同的数据库进行操作),将单个业务事务组合在一起。

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

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