Managing Transactions either in service layer or repository layer?

老子叫甜甜 提交于 2019-12-07 07:38:43

问题


I have a certain scenario where inserts and updates are done on multiple tables based on some constraints ..so its natural to use Transaction scope for these scenarios.Now,I have a respository layer and a service layer. service layer mediates the repository and the UI and is persistent ignorant. Now i m confused where to use the transactions either in service or in repository layers.I am not using any ORMs. I have also seen people advocating about Unit of work pattern for such scenarios. are there any examples about unit of work pattern that suits my current scenarios,all the examples i have seen are using ORMS.

Thanks,


回答1:


This is going to depend on your system of course, but typically I would do it in the service layer. Especially if your service layer methods call multiple fine-grained repository methods and expect them to either all commit or all rollback.




回答2:


Some questions that may help answer this.

Which layer understands the transactional requirements? What is the granularity of your repository interface?

In my world we tend to have fine-grained persistence operations, Insert, Update, Delete. and then compose them in the service layer. Hence in this environment it seems obvious to me that it's the service layer which understands the transactional scope.



来源:https://stackoverflow.com/questions/2265767/managing-transactions-either-in-service-layer-or-repository-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!