【活动记录的意图】
一个对象,它包装数据表或视图中某一行,封装数据库访问,并在这些数据上增加了领域逻辑。
【活动记录的适用场景】
适用于不太复杂的领域逻辑,如CRUD操作等。
【活动记录的运行机制】
对象既有数据又有行为。其使用最直接的方法,将数据访问逻辑置于领域对象中。
活动记录的本质是一个领域模型,这个领域模型中的类和基数据库中的记录结构应该完全匹配,类的每个域对应表的每一列。
一般来说,活动记录包括如下一些方法:
1、由数据行构造一个活动记录实例;
2、为将来对表的插入构造一个新的实例;
3、用静态查找方法来包装常用的SQL查询和返回活动记录;
4、更新数据库并将活动记录中的数据插入数据库;
5、获取或设置域;
6、实现部分业务逻辑。
【活动记录的优点和缺点】
优点:
1、简单,容易创建并且容易理解。
2、在使用事务脚本时,减少代码复制。
3、可以在改变数据库结构时不改变领域逻辑。
4、基于单个活动记录的派生和测试验证会很有效。
缺点:
1、没有隐藏关系数据库的存在。
2、仅当活动记录对象和数据库中表直接对应时,活动记录才会有效。
3、要求对象的设计和数据库的设计紧耦合,这使得项目中的进一步重构很困难
【活动记录与其它模式】
数据源架构模式之行数据入口:活动记录与行数据入口十分类似。二者的主要差别是行数据入口 仅有数据库访问而活动记录既有数据源逻辑又有领域逻辑。
【活动记录的PHP示例】
<ol class="dp-c"> <li class="alt"><span><span><?php </span></span></span></li> <li><span> </span></li> <li class="alt"> <span class="comment">/** </span> </li> <li><span><span class="comment"> * 企业应用架构 数据源架构模式之活动记录 2010-10-17 sz </span> </span></li> <li class="alt"><span><span class="comment"> * @author phppan.p#gmail.com http://www.phppan.com </span> </span></li> <li><span><span class="comment"> * 哥学社成员(http://www.blog-brother.com/) </span> </span></li> <li class="alt"><span><span class="comment"> * @package architecture </span> </span></li> <li><span><span class="comment"> */</span><span> </span></span></li> <li class="alt"><span> </span></li> <li> <span class="comment">/** </span> </li> <li class="alt"><span><span class="comment"> * 定单类 </span> </span></li> <li><span><span class="comment"> */</span><span> </span></span></li> <li class="alt"> <span class="keyword">class</span><span> Order { </span> </li> <li><span> </span></li> <li class="alt"> <span> </span><span class="comment">/** </span> </li> <li><span><span class="comment"> * 定单ID </span> </span></li> <li class="alt"><span><span class="comment"> * @var <type> </type></span> </span></li> <li><span><span class="comment"> */</span><span> </span></span></li> <li class="alt"> <span> </span><span class="keyword">private</span><span> </span><span class="vars">$_order_id</span><span>; </span> </li> <li><span> </span></li> <li class="alt"> <span> </span><span class="comment">/** </span> </li> <li><span><span class="comment"> * 客户ID </span> </span></li> <li class="alt"><span><span class="comment"> * @var <type> </type></span> </span></li> <li><span><span class="comment"> */</span><span> </span></span></li> <li class="alt"> <span> </span><span class="keyword">private</span><span> </span><span class="vars">$_customer_id</span><span>; </span> </li> <li><span> </span></li> <li class="alt"> <span> </span><span class="comment">/** </span> </li> <li><span><span class="comment"> * 定单金额 </span> </span></li> <li class="alt"><span><span class="comment"> * @var <type> </type></span> </span></li> <li><span><span class="comment"> */</span><span> </span></span></li> <li class="alt"> <span> </span><span class="keyword">private</span><span> </span><span class="vars">$_amount</span><span>; </span> </li> <li><span> </span></li> <li class="alt"> <span> </span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> __construct(</span><span class="vars">$order_id</span><span>, </span><span class="vars">$customer_id</span><span>, </span><span class="vars">$amount</span><span>) { </span> </li> <li> <span> </span><span class="vars">$this</span><span>->_order_id = </span><span class="vars">$order_id</span><span>; </span> </li> <li class="alt"> <span> </span><span class="vars">$this</span><span>->_customer_id = </span><span class="vars">$customer_id</span><span>; </span> </li> <li> <span> </span><span class="vars">$this</span><span>->_amount = </span><span class="vars">$amount</span><span>; </span> </li> <li class="alt"><span> } </span></li> <li><span> </span></li> <li class="alt"> <span> </span><span class="comment">/** </span> </li> <li><span><span class="comment"> * 实例的删除操作 </span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li> <span> </span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> </span><span class="func">delete</span><span>() { </span> </li> <li class="alt"> <span> </span><span class="vars">$sql</span><span> = </span><span class="string">"DELETE FROM Order SET WHERE order_id = "</span><span> . </span><span class="vars">$this</span><span>->_order_id . </span><span class="string">" AND customer_id = "</span><span> . </span><span class="vars">$this</span><span>->_customer_id; </span> </li> <li> <span> </span><span class="keyword">return</span><span> DB::query(</span><span class="vars">$sql</span><span>); </span> </li> <li class="alt"><span> } </span></li> <li><span> </span></li> <li class="alt"> <span> </span><span class="comment">/** </span> </li> <li><span><span class="comment"> * 实例的更新操作 </span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li> <span> </span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> update() { </span> </li> <li class="alt"><span> } </span></li> <li><span> </span></li> <li class="alt"> <span> </span><span class="comment">/** </span> </li> <li><span><span class="comment"> * 插入操作 </span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li> <span> </span><span class="keyword">public</span><span> </span><span class="keywo </p> <p align=" left><div style="display:none;"> <span id="url" itemprop="url">http://www.bkjia.com/PHPjc/486087.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/486087.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">【活动记录的意图】 一个对象,它包装数据表或视图中某一行,封装数据库访问,并在这些数据上增加了领域逻辑。 【活动记录的适用场...</span> </div> <div class="art_confoot"></div> </span> </li> </ol>