Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing Transactions either in service layer or repository layer?

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,

like image 953
Rakesh Avatar asked Feb 15 '10 12:02

Rakesh


2 Answers

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.

like image 122
Eric Petroelje Avatar answered Jan 03 '23 00:01

Eric Petroelje


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.

like image 20
djna Avatar answered Jan 03 '23 01:01

djna