域驱动设计 - 数据库事务管理

时间:2022-10-01 21:01:50

I'm trying to implement Domain Driven Design with Unit Of Pattern in my app using ASP.Net WebApi.

我正在尝试使用ASP.Net WebApi在我的应用程序中使用Pattern of Pattern实现域驱动设计。

1) Where should I start/commit transactions (API controller, application layer/service, DAL, domain layer/service)?

1)我应该在哪里开始/提交事务(API控制器,应用程序层/服务,DAL,域层/服务)?

2) What is the best way to manage transactions in complex application with a lot of bussines rules (AOP, explicit call or somehow else)?

2)使用大量商务规则(AOP,显式调用或其他方式)管理复杂应用程序中的事务的最佳方法是什么?

3) What is the best way to reuse one piece of code with transaction in more bigger transaction? i.g. I have independent use case - close invoice - which contains the transaction already. Also this code contains some non domain code like logging, statistics counting and etc. And I want to reuse this code in more complex use case - paying invoice, deduction of commissions and closing invoice.

3)在更大的事务中重用一段代码与事务的最佳方法是什么?胃内我有独立的用例 - 关闭发票 - 其中已包含交易。此代码还包含一些非域代码,如日志记录,统计计数等。我想在更复杂的用例中重复使用此代码 - 支付发票,扣除佣金和结算发票。

3.1) How you deal with an inner transaction problem (popular databases don't support it)?

3.1)如何处理内部事务问题(流行的数据库不支持它)?

3.2) What should layer be responsible for this?

3.2)层应该对此负责什么?

I know answers may depend on a particular project. But it would be great to consider any workable in real projects techniques

我知道答案可能取决于一个特定的项目。但考虑实际项目技术中的任何可行性都会很棒

2 个解决方案

#1


2  

1) The Application layer starts a Unit of Work which turns to the Infrastructure side to initiate whichever sort of technical transaction supports the operation. Same for committing.

1)应用层启动一个工作单元,转向基础设施方面以启动支持该操作的任何类型的技术事务。提交相同。

2) Exactly the same way as smaller applications. Transactions usually take place around Aggregates no matter the number of business rules. For very long running, very complex multi part use cases, you could use Sagas.

2)与较小的应用程序完全相同。无论业务规则的数量如何,事务通常都在聚合周围进行。对于运行时间很长,非常复杂的多部分用例,您可以使用Sagas。

3) A few options, in order of preference : 1. Just reuse the domain and subscribe to domain events to manage things such as stats or logging. 2. In the Application layer, share code between the bigger and smaller use cases, but not transaction / UoW code. Keep it all inside a single transaction. 3. Use a process manager to coordinate calls to smaller use cases as part of a bigger use case (Saga).

3)按优先顺序排列的几个选项:1。只需重用域并订阅域事件即可管理统计信息或日志记录等内容。 2.在应用程序层中,在较大和较小的用例之间共享代码,但不在事务/ UoW代码之间共享代码。将它全部保存在单个事务中。 3.使用流程管理器协调对较小用例的调用,作为更大用例(Saga)的一部分。

#2


1  

1) Where should I start/commit transactions (API controller, application layer/service, DAL, domain layer/service)?

1)我应该在哪里开始/提交事务(API控制器,应用程序层/服务,DAL,域层/服务)?

Transactions is a concern of the application layer.

事务是应用程序层的关注点。

2) What is the best way to manage transactions in complex application with a lot of bussines rules (AOP, explicit call or somehow else)?

2)使用大量商务规则(AOP,显式调用或其他方式)管理复杂应用程序中的事务的最佳方法是什么?

It kinda depends on the persistence/ORM framework that you are using. Entity Framework and NHibernate supports change tracking can make unit of work (commit and rollbacks) easy for you. To ensure changes are made within the same transaction, the repositories must use same unit of work instance (eg. DbContext, session)

它有点取决于您正在使用的持久性/ ORM框架。实体框架和NHibernate支持变更跟踪可以使您的工作单元(提交和回滚)变得简单。为确保在同一事务中进行更改,存储库必须使用相同的工作单元实例(例如,DbContext,session)

3) What is the best way to reuse one piece of code with transaction in more bigger transaction? i.g. I have independent use case - close invoice - which contains the transaction already. Also this code contains some non domain code like logging, statistics counting and etc. And I want to reuse this code in more complex use case - paying invoice, deduction of commissions and closing invoice.

3)在更大的事务中重用一段代码与事务的最佳方法是什么?胃内我有独立的用例 - 关闭发票 - 其中已包含交易。此代码还包含一些非域代码,如日志记录,统计计数等。我想在更复杂的用例中重复使用此代码 - 支付发票,扣除佣金和结算发票。

Logging are cross-cutting concerns and should not affect the use case. These kind of dependencies are injected via the constructor or property. Use an inversion of control container like unity.

记录是跨领域的问题,不应影响用例。这些依赖项是通过构造函数或属性注入的。使用像统一的控制容器的反转。

#1


2  

1) The Application layer starts a Unit of Work which turns to the Infrastructure side to initiate whichever sort of technical transaction supports the operation. Same for committing.

1)应用层启动一个工作单元,转向基础设施方面以启动支持该操作的任何类型的技术事务。提交相同。

2) Exactly the same way as smaller applications. Transactions usually take place around Aggregates no matter the number of business rules. For very long running, very complex multi part use cases, you could use Sagas.

2)与较小的应用程序完全相同。无论业务规则的数量如何,事务通常都在聚合周围进行。对于运行时间很长,非常复杂的多部分用例,您可以使用Sagas。

3) A few options, in order of preference : 1. Just reuse the domain and subscribe to domain events to manage things such as stats or logging. 2. In the Application layer, share code between the bigger and smaller use cases, but not transaction / UoW code. Keep it all inside a single transaction. 3. Use a process manager to coordinate calls to smaller use cases as part of a bigger use case (Saga).

3)按优先顺序排列的几个选项:1。只需重用域并订阅域事件即可管理统计信息或日志记录等内容。 2.在应用程序层中,在较大和较小的用例之间共享代码,但不在事务/ UoW代码之间共享代码。将它全部保存在单个事务中。 3.使用流程管理器协调对较小用例的调用,作为更大用例(Saga)的一部分。

#2


1  

1) Where should I start/commit transactions (API controller, application layer/service, DAL, domain layer/service)?

1)我应该在哪里开始/提交事务(API控制器,应用程序层/服务,DAL,域层/服务)?

Transactions is a concern of the application layer.

事务是应用程序层的关注点。

2) What is the best way to manage transactions in complex application with a lot of bussines rules (AOP, explicit call or somehow else)?

2)使用大量商务规则(AOP,显式调用或其他方式)管理复杂应用程序中的事务的最佳方法是什么?

It kinda depends on the persistence/ORM framework that you are using. Entity Framework and NHibernate supports change tracking can make unit of work (commit and rollbacks) easy for you. To ensure changes are made within the same transaction, the repositories must use same unit of work instance (eg. DbContext, session)

它有点取决于您正在使用的持久性/ ORM框架。实体框架和NHibernate支持变更跟踪可以使您的工作单元(提交和回滚)变得简单。为确保在同一事务中进行更改,存储库必须使用相同的工作单元实例(例如,DbContext,session)

3) What is the best way to reuse one piece of code with transaction in more bigger transaction? i.g. I have independent use case - close invoice - which contains the transaction already. Also this code contains some non domain code like logging, statistics counting and etc. And I want to reuse this code in more complex use case - paying invoice, deduction of commissions and closing invoice.

3)在更大的事务中重用一段代码与事务的最佳方法是什么?胃内我有独立的用例 - 关闭发票 - 其中已包含交易。此代码还包含一些非域代码,如日志记录,统计计数等。我想在更复杂的用例中重复使用此代码 - 支付发票,扣除佣金和结算发票。

Logging are cross-cutting concerns and should not affect the use case. These kind of dependencies are injected via the constructor or property. Use an inversion of control container like unity.

记录是跨领域的问题,不应影响用例。这些依赖项是通过构造函数或属性注入的。使用像统一的控制容器的反转。