OOP design: How to incorporate DB handling into application objects

后端 未结 3 508
梦毁少年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

提交回复
热议问题