iT邦幫忙

2021 iThome 鐵人賽

DAY 15
1
自我挑戰組

馬克的軟體架構小筆記系列 第 15

30-15 之 DataSource Layer - TableDataGateway

  • 分享至 

  • xImage
  •  

接下來我們要進入所謂的『 DataSource 』層,我相信應該有不少人提過這些名詞 :

  • DataMapper
  • DAO
  • DataTableGateway
  • Repository
  • Model

事實上還有不少,接下來我們來一個一個慢慢來的理解它們的原始定義。

首先我們先從書中的 『 Table Data Gateway 』開始說起。

什麼是 TableDataGateway ?

根據書中的定義為 :

A Table Data Gateway holds all the SQL for accessing a single table or view: selects, inserts, updates, and deletes. Other code calls its methods for all interaction with the database.

這裡我個人理解的意思為 :

就是一個專門用來操作資料庫表與視圖的地方,然後他只會做簡單對資料庫的操作,例如 Select, Update 之類。

然後書中有提到幾個重點 :

TableDataGateway 適合與 table module 與 transaction script

事實上我現在還不能說很理解為什麼 domain model 就不適合,就我的理解幾乎可以說是一行資料對應一個 domain model 實體,但應該還是適用於 tableDataGateway ,為什麼呢 ? 希望之後的文章可以理解。

對了,然後我還有想到一個問題

  • 如個 class 處理一張表,那如果是依賴另一張表呢? ( 例如需要 join 或啥的 )

然後我在 stackoverflow 找到一個回答,我覺得可以參考看看。

If you imagine yourself ever wanting to JOIN this table with another, don't use this pattern. Since the entire point of a relational model is to preserve the relationships between data, in practice it is usually avoided.

然後事實上我還有想到一個問題 :

TableDataGateway 回傳的東西是什麼呢 ?

以書中範例他是以 c# 來撰寫,然後他 select 部份回傳的為 IDateReader,但那應該是屬於 ADO.NET 所提供的東西,但如果是其它語言,到底是以什麼東為回傳呢 ?

範例

這個範例我們以下層級來分 :

  • Domain : Table Module
  • DataSource : TableDataGateway

然後程式碼就挺簡單的,就是 PersonTableModule 處理業務 register,然後透過 PersonDataGateway 來操作資料庫。

class PersonDataGateway{
    update(): void{
        console.log('UPDATE SQL OPERATION')
    }
    insert(username: string, email: string): void{
        console.log('INSERT SQL OPERATION')
    }
    selectById(id: string){
        console.log('SELECT * FROM Person WHERE id=xx')
    }
    selectByName(name: string){
        console.log('SELECT * FROM Person WHERE name=xx')
    }
}

class PersonTableModule{

    personDataGateway: PersonDataGateway
    constructor(personDataGateway: PersonDataGateway){
        this.personDataGateway = personDataGateway
    }

    register(username: string, email: string){
        const user = this.personDataGateway.selectByName(username)
        if(user.email === email) throw Error('The email has registered')

        this.personDataGateway.insert(username, email)
    }
}

小總結

這個知識點可以用來解釋什麼現象

想不到呢……

這個知識點可以和以前的什麼知識連結呢 ?

這個模式與我第一份工作時所看到的分層中的 dao 層很像,就是專門處理 sql 操作,會將所有 sql 不帶業務邏輯的東西放在此處。

但這樣的話 Dao 和 TableDataGateway 又有什麼差別呢 ?

我有上網找了一下,然後看到以下兩部份的說明

A TableDataGateway is "a Gateway (object that encapsulates access to an external system or resource) to a database table. One instance handles all the rows in the table"

A DAO "separates a data resource's client interface from its data access mechanisms / adapts a specific data resource's access API to a generic client interface" allowing "data access mechanisms to change independently of the code that uses the data"

但我好像還是不太懂呢……

我要如何運用這個知識點 ?

  • 在實務時,要分的清楚什麼是 Domain 與 DataSource 要處理的事情。
  • TableDataGateway 有了這一層後,就可以不用管後面是用 table 還是 view,我覺得這思路不錯呢

參考資料


上一篇
30-14 之 Domain Layer - Service
下一篇
30-16 之 DataSource Layer - RowDataGateway
系列文
馬克的軟體架構小筆記29
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
kaikaitaiwan
iT邦新手 5 級 ‧ 2021-10-12 15:07:57

請問 "就是一個專門用來操作資料庫表與視圖的地方,然後他只會做簡單對資料庫的操作,而不進行所謂的商業邏輯,例如 Select, Update 之類。" 這句的結論是怎麼得到的呢?

按照您quote下來的意思,A Table Data Gateway holds all the SQL for accessing...比較像是所有對資料庫的操作都要經過TableDataGateway。

馬克 iT邦研究生 3 級 ‧ 2021-10-12 23:52:13 檢舉

A Table Data Gateway has a simple interface, usually consisting of several find methods to get data from the database and update, insert, and delete methods. Each method maps the input parameters into a SQL call and executes the SQL against a database connection.The Table Data Gateway is usually stateless, as its role is to push data back and forth.

我當初主要是根據,原文中的這一段來判斷,裡面的方法只能代 sql 要用的參數,但我覺得你說的也有理……

因為假設他的 sql 是有業務邏輯,那這裡就有問題了…

我修改一下我上面文字的說法好了 ~ 剛蝦

我要留言

立即登入留言