这是indexloc提供的服务,不要输入任何密码
Skip to content

我想咨询或探讨一个关于事务问题, 请指教 #2

@zbysir

Description

@zbysir

我拜读你写的文章: 清晰架构(Clean Architecture)的Go微服务: 事物管理, 思路很好, 不过我还有一个问题没思考明白:

不知道你有没有遇到一个事务跨几个DataService的场景?

假如 我定义了两个DataService: OrderDataInterface, ProductDataInterface, 分别处理订单数据与商品数据, 继续, 现在我定义了一个UserCase: OrderUseCase, 它依赖OrderDataInterfaceProductDataInterface来进行下单操作: 新建一个Order并将Product库存减一, 这两个操作我想做到一个事务里.

我看到此仓库的代码里只能对其中一个DataService开启事务:

import (
	"github.com/jfeng45/servicetmpl1/applicationservice/dataservice"
	"github.com/jfeng45/servicetmpl1/domain/model"
)

// RegistrationTxUseCase implements RegistrationTxUseCaseInterface.
// It has UserDataInterface, which can be used to access persistence layer
type RegistrationTxUseCase struct {
	UserDataInterface dataservice.UserDataInterface
}

// The use case of ModifyAndUnregister with transaction
func (rtuc *RegistrationTxUseCase) ModifyAndUnregisterWithTx(user *model.User) error {

	udi := rtuc.UserDataInterface
	return udi.EnableTx(func() error {
		// wrap the business function inside the TxEnd function
		return ModifyAndUnregister(udi, user)
	})
}

我想让一个事务跨几个DataService, 请问这个情况下我应该如何构建代码?

不知道你还用qq吗? 我的qq是 1019654929, 想与你实时交流更方便. 如有打扰请见谅.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions