接下来我们要进入所谓的『 DataSource 』层,我相信应该有不少人提过这些名词 :
事实上还有不少,接下来我们来一个一个慢慢来的理解它们的原始定义。
首先我们先从书中的 『 Table Data Gateway 』开始说起。
根据书中的定义为 :
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 ,为什麽呢 ? 希望之後的文章可以理解。
对了,然後我还有想到一个问题
然後我在 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 所提供的东西,但如果是其它语言,到底是以什麽东为回传呢 ?
这个范例我们以下层级来分 :
然後程序码就挺简单的,就是 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"
但我好像还是不太懂呢……
BMI(身体质量指数)是用自己身高、体重的比例,来当作是否过胖的「身高体重指数」 它的计算方法也很简...
Hello, World! 我是 Peter ,在网页开发时,为了完善专案的功能与确保程序码的品质,...
大家安安,小弟弟要来接续上一篇的结尾 上一篇的进度是列出了宠物的清单 而现在我们想实现在点击清单的时...
俗语说的好:「鸟为财死,人为食亡」。(先附上新马辣的官网连结) (以上是我在Google Maps的...
昨天稍微谈到了一些有关警报的设计,然而,警报的发出与否,应是建立在我们观测到的一些系统的行为,例如说...