PHP OOP: Business Logic Level - DB Level

What can be a good design for partitioning between business logic objects and a database using OOP?

+6
source share
2 answers

There are several approaches you can take with this, but I would like to recommend the DataMapper template in conjunction with domain models. See this page for more details.

Thus, you can easily and easily separate data access from your domain models (business logic). If you are a little familiar with OOP, the UML model on the page above should clarify the approach and how it separates database logic from business logic.

+2
source

Any of these will work ( from Fowler POEAA ):

Data Source Architectural Patterns:

  • Gateway data table . An object that acts as a gateway to the database table. One instance processes all the rows in the table.
  • Gateway data line . An object that acts as a gateway to a single record in a data source. There is one instance per line.
  • Active record . An object that wraps a string in a database table or view encapsulates access to the database and adds domain logic to this data.
  • Data Mapper : a Mappers layer that moves data between objects and the database, saving them independently of each other and the translator itself.

What to choose depends on which one you have chosen (same source):

Domain logic:

  • Transaction Script: Organizes business logic using procedures in which each procedure processes one request from a presentation
  • Domain Model: A domain object model that includes both behavior and data
  • Table: One instance that processes business logic for all rows in a database table or view.
  • Service Level: Defines the boundary of the application with the service layer, which sets the set of available operations and coordinates the response of the application in each operation.

In general, the closer your business objects resemble the database schema and are oriented around CRUD operations, the easier your Data Source Architectural and Doman Logic can be (this is not necessary). If you are faced with a large number of impedance mismatches or with a large number of business logic that is not directly related to database data, then you can select a domain model / data card (and also enable ORM).

+9
source

Source: https://habr.com/ru/post/885966/


All Articles