Answer the question
In order to leave comments, you need to log in
How to use transactions in "clean architecture" style code?
Repository example
https://github.com/bxcodec/go-clean-arch
I looked at examples, as above, read articles but did not understand for myself. What to do with transactions. After all, repository methods do not accept anything other than data. In the end, it's not entirely clear.
For example, I have OrderUseCase {orderRepository, paymentRepository}
And the newOrder() method
How to write it correctly so that creating an order and debiting funds from the user's account would be in a transaction. Or does it all need to be pushed into 1 repository, but then there will be code duplication, right?
Answer the question
In order to leave comments, you need to log in
You need to understand that orderRepository and paymentRepository are abstractions, and in fact, data can be stored both in the same database and in completely different ones. For example, orderRepository is located on the S1 server in the Redis database, and paymentRepository is located on the S2, S3, S4 servers in the PostgreSQL database. Therefore, classical transactions are not applicable here. This can be neglected in most cases and not overcomplicated the system, or you can go the "right" way and use distributed transactions, for example, sagas https://microservices.io/patterns/data/saga.html
Everything is pretty simple here. We have a layer of business logic that has 3 interfaces:
TransactionManager - the actual transaction manager
OrderRepository - order repository
PaymentRepository - payment
repository PAID" (I'm writing an example in Kotlin, I hope it's clear)
transactionManager.inTransaction {
order.status = PAID
paymentRepository.save(payment)
orderRepository.save(order)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question