简体   繁体   English

Laravel控制器调用数据库事务

[英]Laravel controller calling database transaction

Does calling a database transaction in the Controller a good practice? 在Controller中调用数据库事务是否是一个好习惯? if no where should i proper call a database transaction? 如果没有,我应该在哪里正确地调用数据库事务?

Depends upon the size of project and personal preference also. 还取决于项目的大小和个人喜好。 But calling a database transaction outside of controller is a better practice as its makes your code clean and understandable. 但是在控制器外部调用数据库事务是一种更好的做法,因为它使您的代码清晰易懂。 Also you can use the same function for multiple controllers reducing the pain of redundant coding. 您也可以对多个控制器使用相同的功能,从而减少了冗余编码的麻烦。 You can search for SOA (Service Oriented Architecture) and use it for better practice. 您可以搜索SOA(面向服务的体系结构)并将其用于更好的实践。

Or you can search for Command Bus pattern 或者您可以搜索命令总线模式

https://github.com/tillkruss/laravel-tactician https://github.com/tillkruss/laravel-tactician

This package implements the Command Bus pattern easily with Laravel. 该软件包使用Laravel轻松实现了Command Bus模式。 It comes with a database transaction middleware and Command Bus is good for a scalable application moving the logic from controllers to Command Handlers. 它带有数据库事务中间件,并且Command Bus对于将逻辑从控制器转移到Command Handlers的可伸缩应用程序非常有用。

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

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