简体   繁体   English

如何在“干净架构”中实现“SQL 事务”?

[英]How to implement “SQL Transactions” in “Clean Architecture”?

I am working on an Express-based (Nodejs) API that uses MySQL for data persistence.我正在研究基于 Express(Nodejs)的 API,它使用 MySQL 进行数据持久性。 I have tried to follow the CLEAN ARCHITECTURE proposed by Sir R.C.我试图遵循 R.C 爵士提出的清洁架构。 Martin.马丁。

Long story short:-长话短说:-

There are some crop vendors and some users.有一些作物供应商和一些用户。 A user can request an order of some crops with a defined quantity from a vendor.用户可以向供应商请求一些具有定义数量的作物的订单。 This puts the order in PENDING state.这会将订单置于 PENDING state 中。 Then the vendor will confirm the orders he/she gets from the user.然后供应商将确认他/她从用户那里获得的订单。

Domain/Entity -> CROP, Use-case -> add, remove, edit, find, updateQty域/实体 -> CROP、用例 -> 添加、删除、编辑、查找、更新数量

Domain/Entity -> ORDER, Use-case -> request, confirm, cancel域/实体 -> 订单、用例 -> 请求、确认、取消

I have to implement a confirm order functionality我必须实现确认订单功能

  • I have an already recorded order with ordered item list in my DB (order in the pending state)我的数据库中有一个已记录的订单,其中包含已订购的商品列表(订单处于待处理状态)
  • Now on confirming order action I need to subtract each item quantity from respective crop present in the DB record, with a check that no value turns negative (ie no ordered qty is more than present qty)现在在确认订单操作时,我需要从数据库记录中存在的相应作物中减去每个项目数量,并检查没有值变为负数(即没有订购数量超过当前数量)
  • If it is done for all the items under a "transaction cover" then I have to commit the transaction如果对“交易覆盖”下的所有项目都完成了,那么我必须提交交易
  • Else revert back to the previous state (ie rollback)否则恢复到之前的 state(即回滚)

I know how to run Mysql specific transactions using "Sequelize", but with a lot of coupling and poor source code architecture.我知道如何使用“Sequelize”运行 Mysql 特定事务,但有很多耦合和糟糕的源代码架构。 (If I do it that way, then DB won't be like plugin anymore) (如果我这样做,那么 DB 将不再像插件一样)

I am not able to understand how to do this while maintaining the architecture and at what layer to implement this transaction thing, use-case/data-access, or what?我无法理解如何在维护架构的同时执行此操作,以及在哪一层实现此事务、用例/数据访问或什么?

Thanks in advance提前致谢

I recommend keeping the transaction in the "adapters layer" by using "unit of work" pattern.我建议使用“工作单元”模式将事务保存在“适配器层”中。 This way the database remains a plug-in to the business logic.这样,数据库仍然是业务逻辑的插件。

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

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