OOP design: How to incorporate DB handling into application objects

后端 未结 3 490
梦毁少年i
梦毁少年i 2020-12-29 00:27

This is a design problem I face regularly and I\'d like to find some general insights about the subject. The code provided here is just an example.

In the design pha

相关标签:
3条回答
  • 2020-12-29 00:43

    It depends on your architecture. The four common Data Source Architectural Patterns can be found in Martin Fowler's Patterns of Enterprise Application Architecture:

    • 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.

    Further patterns:

    • http://martinfowler.com/eaaCatalog/index.html
    • http://sourcemaking.com/design_patterns
    0 讨论(0)
  • 2020-12-29 00:45

    The Zend Framework quickstart has a, pretty easy to grasp, overview of models and mappers (google'able terms), together with some source code.

    0 讨论(0)
  • 2020-12-29 00:56

    Have you considered using a Object Relational Mapping library like Doctrine or Propel?

    0 讨论(0)
提交回复
热议问题