iT邦幫忙

2021 iThome 鐵人賽

DAY 12
1
自我挑戰組

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

30-12 之 Domain Layer - Domain Model ( 未完成版 )

  • 分享至 

  • twitterImage
  •  

這篇文章接下來我們要談談《 企業應用架構模式- Martin Fowler 》這本書中所提 domain 的三種 patterns 的第二種『 Domain Model 』。

什麼是 Domain Model 呢 ?

這是我從書中,自已提出的想法,我認為 domain model 為 :

將每個業務領域都定義成個實體,並且創建一個這些實體的關係網,然後每個實體中都有所謂的資料與行為。

我個人是為了 OO 算是是一種 domain model 的開發方式,然後 domain model class 基本上要包含 :

  • identity
  • state
  • behaviour

然後在書中有提到兩種風格 :

  • A simple Domain Model : 每一個資料庫都與一個 domain object 所對應,然後書中有提到這個風格適合用 Active Record。
  • A rich Domain Model : 使用繼承、策略和其它設計模式,是由一堆東西所組成,而書中也有提到這個風格適合用 Data Mapper。

以上兩種風格的就在於,一個適合簡單,一個適合複雜…… 就是這樣。其中『 Active Record 』與『 Data Mapper 』在之後的文章會提到,它是在 3-tier 的資料層。

Anemic domain model

範例 ( 未完成 )

// Transaction Script
class CartService{
	async checkout(){
     // 取出購物車要結帳的商品
		 const products = this.cart.items
     
     // 建立訂單
		 const order = await orderDao.create(orderBody)
     // 建立付款記錄
     const payment = await paymentDao.create(paymentBody)
     // 建立發票 
		 const invoice = await invoiceDao.create(invoiceBody)
	}
}
// Domain Model
class Cart{
  id: String
  items: Product[]

  async addItem(product: Product){
      this.items.push(product)
  }
  async chekcout(){
      
  }
}   

class Order{
   id: string
   products: Product[]
   construct(productDao:ProductDao){
      this.productDao = productDao
   }
   
   async chekcout(){
        this.productDao.save({
           products
        })
        
        return this.id
   }
}

class Payment{
   async checkout(){

   }
}

參考資料


上一篇
30-11 之Domain Layer - Transaction Script
下一篇
30-13 之 Domain Layer - Table Module
系列文
馬克的軟體架構小筆記29
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言