[Study Note] Patterns of Enterprise Application Architecture

Martin Fowler 的 Patterns of Enterprise Application Architecture 被无数的人奉为经典,我一直都没有看过原著。最近在学习 TDD 的时候经常会看到其中的一些模式,从 Martin Fowler 的网站上可以找到这一篇关于企业应用架构模式的简介,顺手学习一下。因为全部的企业应用架构模式比较多,所以我打算集腋成裘。

[A short summary of the patterns in Patterns of Enterprise Application Architecture (P of EAA)]

2010-04-16

Domain Logic Pattern - Transaction Script

Organizes business logic by procedures where each procedure handles a single request from the presentation

transactionScriptSketch

A Transaction Script organizes all this logic primarily as a single procedure, making calls directly to the database or through a thin database wrapper. Each transaction will have its own Transaction Script, although common subtasks can be broken into subsprocedures.

虽然我也见到有些程序员利用存储过程来完成业务逻辑,据说如果业务逻辑发生了变化,只需要修改存储过程就可以(这个有点让人难以置信);可是我不太喜欢这种方式,一来我自己的PL/SQL用的不怎么灵光,二来就是感觉存储过程比较难于调试,特别是相对于编程语言来说。

Domain Logic Pattern - Domain Model

An object model of the domain that incorporates both behavior and data.

domainModelSketch

A Domain Model creates a web of interconnected objects, where each object represents some meaningful individual, whether as large as a corporation or as small as a single line on an order form.

我比较推崇这样的方式,不过我认为建立 Domain Model 需要对整个系统进行很好的面向对象的分析,我现在还达不到这样的层次。

Data Source Architectural Pattern - 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.

activeRecordSketch

An object carries both data and behavior.

Active Record use the most obvious approach, putting data access logic in the domain object.

这个应该是一种比较简单的数据源架构模式,我也经常使用这样的形式,简单易懂,不过似乎还有更好的选择。

Data Source Architectural Pattern - Data Mapper

A layer of Mappers that moves data between objects and a database while keeping them indepndent of each other and the mapper itself.

databaseMapperSketch

Many parts of an object, such as collections and inheritance, aren’t present in relational database.

Doing so leads to variant schemas; that is, the object schema and the relational shema don’t match up.

transfer data between the two schemas, and this data transfer becomes a complexity in its own right.

The Data Mapper is a layer of software that separates the in-memory objects from the database. Its responsibility is to transfer data between the two and also to isolate them from each other. With Data Mapper the in-memory objects needn’t know even that there’s a database present; they need no SQL interface code, and certainly no knowledge of the database schema. (The database schema is always ignorant of objects that use it.) Since it’s a form of Mapper, Data Mapper itself is even unknown to the domain layer.

最近打算学习并试用的 NHibernate 似乎就是这个路子,不过看上去很漂亮,如果想要得心应手,还需要深入学习。

Object-Relational Behavioral Pattern: Lazy Load

An object that doesn’t contain all of the data you need but knows how to get it.

lazyLoadSketch

A Lazy Load interrupts the loading process, loading data from a database into memory, leaving a marker in the object structure so that if the data is needed it can be loaded only when it is used.

  1. Lazy Initialization uses a special marker value (usually null) to indicate a field isn’t loaded. Every access to the field checks the field for the marker value and if unloaded, loads it. 
  2. Virtual Proxy is an object with the same interface as the real object. The first time one of its methods are called it loads the real object and then delegates.
  3. Value Holder is an object with a getValue method. Client call getValue to get the real object, the first call triggers the load.
  4. Ghost is the real object without any data. The first time you call a method the ghost loads the full data into its field

Lazy Load 模式似乎和 NHibernate 的关系很是密切,印象里面似乎是采用了 Virtual Proxy 的方式,不过这个还需要进一步的确认。

Distribution Pattern – Data Transfer Object

An object that carries data between processes in order to reduce the number of method calls.

dtoSketch

create a Data Transfer Object that can hold all the data for the call. It needs to be serializable to go across the connection. Usually an assembler is used on the server side to transfer data between the DTO and any domain objects.

我有点不明白,这里为什么不直接使用 Domain Model 对象来传递?从示意图上来看,似乎传递的更像是一个中间的多对多关系表的数据——艺术家和影集的对应关系?

posted on 2010-04-17 00:32  zhaorui  阅读(239)  评论(0编辑  收藏  举报

导航