lundi 19 août 2019

Business logic wrapped inside a db transaction

In my app i have an api layer that defines routes, services layer to perform business logic, and a repository layer that queries the database. Services will call into the repository layer to retrieve data, perform various business logic and then return a response back to the api layer.

Now, I have some business logic that needs to be completed inside a database transaction. For example, let's assume my app allows you to buy tickets to a given event:

  1. Service layer calls repository to atomically reserve tickets in the db by decrementing number of available tickets.

  2. Service layer then makes an api request to process the payment for the ticket.

  3. If the payment succeeds, service layer calls repository to create an "order" for the user and the given event.

If the payment fails, however, i want to rollback the changes made to the ticket availability. (or if for some reason creating the order for the user fails, same thing).

The problem is that it seems like a leaky abstraction if the services layer is aware of database transactions. Is there a better paradigm for this?

Aucun commentaire:

Enregistrer un commentaire