Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding db transaction in repository layer #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

chud-lori
Copy link

@chud-lori chud-lori commented Feb 18, 2025

Solve issue: #8

Tests result:
image

@chud-lori chud-lori force-pushed the feature/db-transaction branch from c791faf to f352316 Compare February 18, 2025 07:00
@syahidfrd
Copy link
Owner

syahidfrd commented Feb 18, 2025

Hi @chud-lori thanks for your contribution! However, I have some concerns about this approach.

Right now, the transaction is handled entirely within the repository layer. This works fine if a use case only interacts with a single repository. But what if a use case needs to perform operations across multiple repositories and ensure they are all committed or rolled back together?

For example, imagine a scenario where a single business process involves inserting data into both the todos repository and another repository. If each repository manages its own transaction separately, we lose the ability to handle them as an atomic unit, which could lead to data inconsistencies.

A better approach might be to manage transactions at the use case (service) layer, where the transaction context can be shared across multiple repositories. That way, if something goes wrong in one repository, we can roll back the entire operation instead of leaving the system in a partial state.

Would love to hear your thoughts on this! 🚀

@chud-lori
Copy link
Author

I was thinking to keep it simple, but as boilerplate that's concern is valid, I have pushed the update to put the transaction on use case layer, but I haven't managed to write the tests due to the mockery on my local seems not working to generate a mock code, will get back once the test code works, cheers @syahidfrd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants