The Active Record Pattern(1) アクティブレコードパターン

目次に戻る - (2)サンプルコード >>
http://www.devshed.com/c/a/PHP/The-Active-Record-Pattern/の日本語訳です。

アクティブレコードパターン - The Active Record Pattern

この記事の前半は、ウェブアプリケーションとデータベースのやりとりの改善を目的としたデザインパターンの使用に役立ちます。Jason E. Sweat著のphp|architect's Guide to PHPDesign Patterns(php|architect, 2005; ISBN: 0973589825)の14章から抜粋しています。

This article, the first of two parts, helps you use design patterns to better organize how your web application interacts with a database. It is excerpted from chaptr 14 of the book php|architect's Guide to PHP Design Patterns, written by Jason E. Sweat (php|architect, 2005; SBN: 0973589825).

デザインパターンによりスクリプト内の可読性とメンテナンス性を大いに改善されます。しかし、これまでのデザインパターンアーキテクチャウェブアプリケーション開発における基本的な要求であり課題である、データベース接続には取り組んできませんでした。この章および続く二つの章(テーブルデー
ゲートウェイとデータマッパー)では、アプリケーションとデータベースのやりとりを改善するための三つのデザインパターンを紹介しています。

The design patterns you've seen so far greatly improve the readability and maintainability of script internals; however none have confronted a fundamental requirement and challenge of architecting and developing web applications: connecting to a database. This chapter and the next two chapters-Table Data Gateway and Data Mapper-provide three design patterns that better organize how your application interacts with a database.

問題点 - The Problem

ほとんどのWebアプリケーションは情報をデータベース中に永続化します。データベース接続を抽象化し、テーブルへのアクセスやビジネスロジックと永続化とを統合するための方法はあるでしょうか?

Most web applications persist information in a database. Is there a way to abstract database connectivity to simplify table access and integrate persistence with business logic?

解決方法 - The Solution

アクティブレコードパターンはデータベースに関連するデザインパターンで考え方がもっとも単純です。アクティブレコードパターンはデータベースとのやりとりの仕方を、やりとり用のクラスに埋め込みます。

Conceptually, the Active Record pattern is the simplest of the database-related design patterns. The Active Record pattern embeds the knowledge of how to interact with the database directly into the class performing the interaction.

アクティブレコードによりアプリケーションコードとデータベース構造とを高いレベルで組み合わせることができるため、比較的単純な場合であれば、より複雑な解決方法に比べ、組み合わせの問題の管理がはるかに簡単です。また、アクティブレコードは初回のデータベースプロジェクトの多くで有効です。アクティブレコードパターンで簡単に解決できないほど複雑になった場合だけ、テーブルデーゲートウェイ(15章を参照)や、データマッパー(16章を参照)、その他のデザインパターンリファクタリングすればよいのです。

While Active Record leads to a high degree of coupling between application code and database structure, in relatively simple circumstances the issues inherent in coupling may be far easier to manage than adopting a more complex solution. Active Record is also sufficient for many first-time database projects. Only if complications arise that cannot be easily addressed with the Active Record pattern should you refactor to a Table Data Gateway (see Chapter 15), a Data Mapper (see Chapter 16), or another database design pattern.

エンタープライズアプリケーションアーキテクチャのパターン - Patterns of Enterprise Application Architecture

Martin Fowler著のPatterns of Enterprise Application Architectureよれば、エンタープライズアプリケーションはほかのアプリケーションと統合され、重要なビジネスロジック(やアプリケーション要求がしばしば公開している非論理的なもの)を含み、多数の並列アクセスが行われ、様々なインタフェースから永続化データにアクセスされます。興味深いことに、ウェブアプリケーションがこれらの特徴を共有しているためか、Fowlerの本はPHPプログラマの強い共感を呼んでいます。

According to Martin Fowler's book, Patterns of Enterprise Application Architecture, an enterprise application is integrated with other applications, contains significant business logic (or illogical, as application requirements often reveal), and includes lots of concurrently accessed, persistent data that's accessed from a variety of interfaces. Interestingly, web applications share many of those very characteristics, which may explain why Fowler's book resonates strongly with PHP programmers.

PHP Data Objects

PDOというプロジェクトを見てみます。PDOはパフォーマンスの高いデータベースアクセスのためのPHPの拡張です(データベースの抽象化は行いません)。PDOはネイティブドライバーのC言語によるラッパであり、非常に高速です。PDOでは、プリペアードステートメントがすべてのPDOドライバで使用できるため、このライブラリを使用することでスクリプトのセキュリティが高まります。

One project to watch is PDO. PDO is a PHP extension for high-performance database access (not database abstraction). PDO is a C-language wrapper of the native drivers and is therefore very fast. PDO provides prepared statements for all PDO drivers, enhancing the security of
scripts using the library.

目次に戻る - (2)サンプルコード >>