admin管理员组文章数量:1357112
I'm developing a backend where controllers call services, and each service is responsible for handling an entity(table) in SQL. Now, what if I want to use a transaction, basically roll back previous service calls if any of them fail? What would be the right approach? The problem is that sql code is down below in each service. If you have ever written Onion architecture for a backend, sure thing you know what I mean.
I use Adonisjs + lucid form. But it does not matter for a question.
I'm developing a backend where controllers call services, and each service is responsible for handling an entity(table) in SQL. Now, what if I want to use a transaction, basically roll back previous service calls if any of them fail? What would be the right approach? The problem is that sql code is down below in each service. If you have ever written Onion architecture for a backend, sure thing you know what I mean.
I use Adonisjs + lucid form. But it does not matter for a question.
Share Improve this question asked Mar 27 at 18:28 Eldar TailovEldar Tailov 3746 silver badges18 bronze badges1 Answer
Reset to default 1A controller is calling many services. This means that no single service is responsible for managing a transaction.
Rather, a transaction is an application layer (or a use case) concern. It is started before calling any of the services and then passed to them.
You have two ways to pass transaction to a service. Either in a constructor, or as method argument. I believe, passing as a method argument is fine, since a transaction is a perfect business concept. Not a technical SQL transaction, of course, but a business transaction. Which may encapsulate SQL transaction.
To sum it up:
A controller parses and validates request, instantiates app layer (or a use case) and calls it. Gets back response and renders HTTP response.
App layer starts and commits (or rolls back) a transaction and passes it around to services. Services use the transaction to call repositories.
In general this approach is language-agnostic, although different languages may bring their specifics to the implementation.
本文标签:
版权声明:本文标题:controller - How to make a transactional Services when developing a backend with Onion or similar architectures? - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744073548a2586328.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论