Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP OOP: business logic layer - DB layer

what could be a good design for layering between the business logic objects and the Database using OOP?

like image 515
Pablo Avatar asked Apr 15 '11 08:04

Pablo


1 Answers

Any of these would work (from Fowler's POEAA):

Data Source Architectural Patterns:

  • Table Data Gateway: An object that acts as a Gateway to a database table. One instance handles all the rows in the table.
  • Row Data Gateway: An object that acts as a Gateway to a single record in a data source. There is one instance per row.
  • Active Record: An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.
  • Data Mapper: A layer of Mappers that moves data between objects and a database while keeping them independent of each other and the mapper itself.

Which to choose depends on which of these you picked (same source):

Domain Logic Patterns:

  • Transaction Script: Organizes business logic by procedures where each procedure handles a single request from the presentation
  • Domain Model: An object model of the domain that incorporates both behavior and data
  • Table Module: A single instance that handles the business logic for all rows in a database table or view.
  • Service Layer: Defines an application's boundary with a layer of services that establishes a set of available operations and coordinates the application's response in each operation.

In general, the more closely your business objects resemble the DB schema and is centric around CRUD operations, the simpler your Data Source Architectural and Doman Logic pattern can be (it doesnt have to though). If you find yourself with a lot of Impedance Mismatch or with lots of Business Logic not directly related to the DB data, then you might opt for Domain Model / Data Mapper (and might also include an ORM).

like image 122
Gordon Avatar answered Sep 28 '22 18:09

Gordon