zoukankan      html  css  js  c++  java
  • PHP – 架構設計 Data Access Layer 篇

    在前面 多层次架构设计前言 啰哩啰嗦讲了一堆,就是为了后面的架构设计的文章,作铺陈,这样才能用更宽广的角度来看其中带来的意义。

    首先,大概陈述一下架构的关联,如下所述:

    首先会先设计 标准 DAL class (STDAL),放置 getData, delete, update 等 标准常见的功能函数
    在来设计程式会用到的各种 DAL ,基本上每一个 Table 都需要有一个 DAL 来实现,后面根据 table 应用、画面呈现等需求,也可以一个 table 有多个 DAL ,这各观念类似 View 的概念。
    根据 商业逻辑的操作,制作对应的 BLL,像是insert、update 前的资料检查,这部分会根据商务应用的不同而不同,所以下面不做示范。
    在来要有一个 DAL 产生工厂(DALFactory),专门用来协助建立 DAL 的实体,因为 DAL 程式档案,可能放在另一台主机,或是 不同目录位置中等等因素,为简化开发人员的负担,所以 DAL 建立方式,统一封装在 DALFactory 里面
    接下来,依照上述建了下述程式:

    STDAL.php

     1 <?php
     2 class STDAL
     3 {
     4     public $TableName;
     5      
     6     public function __construct() {
     7         echo $this->TableName." init STDAL<br>";
     8     }
     9      
    10     public function getData()
    11     {
    12         print "select * from ".$this->TableName."<br>";
    13     }
    14      
    15     public function setDB($db)
    16     {
    17         echo $db."<br>";
    18     }
    19 }
    20 ?>

    STUser.php

    1 <?php
    2 class DAL_STUser extends STDAL
    3 {
    4     public function __construct() {
    5         $this->TableName = "STUser";
    6         parent::__construct();
    7     }
    8 }
    9 ?>

    STDoc.php

    1 <?php
    2 class DAL_STDoc extends STDAL
    3 {
    4     public function __construct() {
    5         $this->TableName = "STDoc";
    6         parent::__construct();
    7     }
    8 }
    9 ?>

    下面 DAL 产生工厂,有运用我在 PHP – 类别初探 中所讲的技巧,有兴趣可在去看下。
    DALFactory.php

     1 <?php
     2 class DALFactory
     3 {
     4     private static $db;
     5      
     6     public static function getInstance($prgName) {
     7          
     8         if(!self::$db) { 
     9             self::$db = $prgName." get DB connection"; 
    10         }
    11         $class = "DAL_$prgName";
    12         $obj = new $class();
    13         $obj->setDB(self::$db);
    14         return $obj;
    15     }
    16 }
    17 ?>

    上述就已经完成 Data Access Layer 的制作,接下来 我门测试一下,是否正常运作。

    test.php

    1 <?php
    2 $prgName = "STUser";
    3 $obj = DALFactory::getInstance($prgName);
    4 $obj->getData();
    5  
    6 $prgName = "STDoc";
    7 $obj = DALFactory::getInstance($prgName);
    8 $obj->getData();
    9 ?>

    測試結果,如下所示:
    STUser init STDAL
    STUser get DB connection
    select * from STUser
    STDoc init STDAL
    STUser get DB connection
    select * from STDoc

     一條小龍

    下一篇 PHP – EasyUI DataGrid 资料取的方式

     

    ~~~ 一条小龙 (babydragoner) ~~~

     

  • 相关阅读:
    『Delphi』字符串操作——返回子串出现的位置
    2007:远见、劲取、专注
    『转载』个人博客吸引风投关注成可盈利业务
    [和管子对话] 1 200745/对面向对象的你言我语
    『Delphi』File not found的解决办法
    Ruby学习1字符串
    聚集表(clustered table)data page中的数据行可以无序
    通过DBCC PAGE查看页信息验证聚集索引和非聚集索引节点信息
    查看SQL Server Resource Database以及修改系统表
    SQL Server的还原(2)——STOPAT
  • 原文地址:https://www.cnblogs.com/babydragoner/p/2746842.html
Copyright © 2011-2022 走看看