I need transactions in my service to ensure data consistency. I don't know what the best layer is for transactions to be created in. The goal is to have all business logic in a single layer.
First, some example business logic:
There are the entities of
person can have multiple
hobbies. This means there will be the
hobby tables. When I create a person with its hobbies I must insert them into its respective tables. For example, 1 person with 3 hobbies will be 4 table inserts. If anything fails in this process, everything must be rolled back. Transactions are thus a requirement.
Let me describe the layers of the service:
Controllers - this is the entry point to the system. Can be a REST API for example. It handles the input and response, and calls the appropriate methods in business logic layer. Ideally, one method per handler.
Business - this is where the business logic lies. It this case, it knows that a
personmust be created alongside with its
hobbies. It will combine the CRUD operations of the lower level to perform business actions, such as person+hobbies creation.
Model - this is where all the models reside. This layer knows the has an individual CRUD for each entity, but it does not know about the database directly.
Database - this is where the data is persisted and SQL is used. There should be a CRUD for each table.
Both the database and database layers should be pure, in the sense that they don't entities do not interact with each other. So it seems like the best approach is to place transactions in the business layer. Is this the standard approach?