在最近的圍繞domain object的討論中浮現出來了三種模型,(還有一些其他的旁枝,不一一分析了),經過一番討論,各種問題逐漸清晰起來,在這里我試圖做一個總結,便于大家了解和掌握。
第一種模型:只有getter/setter方法的純數據類,所有的業務邏輯完全由business object來完成(又稱TransactionScript),這種模型下的domain object被Martin Fowler稱之為“貧血的domain object”。下面用舉一個具體的代碼來說明,代碼來自Hibernate的caveatemptor,但經過我的改寫:
一個實體類叫做Item,指的是一個拍賣項目
一個DAO接口類叫做ItemDao
一個DAO接口實現類叫做ItemDaoHibernateImpl
一個業務邏輯類叫做ItemManager(或者叫做ItemService)
java代碼
public
class
Item
implements
Serializable
{
private
Long
id
=
null
;
private
int
version
;
private
String
name
;
private
User
seller
;
private
String
description
;
private
MonetaryAmount
initialPrice
;
private
MonetaryAmount
reservePrice
;
private
Date
startDate
;
private
Date
endDate
;
private
Set
categorizedItems
=
new
HashSet
();
private
Collection
bids
=
new
ArrayList
();
private
Bid
successfulBid
;
private
ItemState
state
;
private
User
approvedBy
;
private
Date
approvalDatetime
;
private
Date
created
=
new
Date
();
/
/
getter
/
setter方法省略不寫,避免篇幅太長
}
java代碼
public
interface
ItemDao
{
public
Item
getItemById
(
Long
id
);
public
Collection
findAll
();
public
void
updateItem
(
Item
item
);
}
ItemDao定義持久化操作的接口,用于隔離持久化代碼。
java代碼
public
class
ItemDaoHibernateImpl
implements
ItemDao
extends
HibernateDaoSupport
{
public
Item
getItemById
(
Long
id
)
{
return
(
Item
)
getHibernateTemplate
().
load
(
Item
.
class
,
id
);
}
public
Collection
findAll
()
{
return
(
List
)
getHibernateTemplate
().
find
("
from Item
");
}
public
void
updateItem
(
Item
item
)
{
getHibernateTemplate
().
update
(
item
);
}
}
ItemDaoHibernateImpl完成具體的持久化工作,請注意,數據庫資源的獲取和釋放是在ItemDaoHibernateImpl里面處理的,每個DAO方法調用之前打開Session,DAO方法調用之后,關閉Session。(Session放在ThreadLocal中,保證一次調用只打開關閉一次)
java代碼
publicclass ItemManager{
privateItemDaoitemDao;
publicvoidsetItemDao(ItemDaoitemDao){this.itemDao=itemDao;}
publicBidloadItemById(Longid){
itemDao.loadItemById(id);
}
publicCollectionlistAllItems(){
returnitemDao.findAll();
}
publicBidplaceBid(Itemitem,Userbidder,MonetaryAmountbidAmount,
BidcurrentMaxBid,BidcurrentMinBid)throwsBusinessException{
if(currentMaxBid!=null&¤tMaxBid.getAmount().compareTo(bidAmount)>0){
thrownewBusinessException("Bid too low.");
}
//Auctionisactive
if(!state.equals(ItemState.ACTIVE))
thrownewBusinessException("Auction is not active yet.");
//Auctionstillvalid
if(item.getEndDate().before(newDate()))
thrownewBusinessException("Can't place new bid, auction already ended.");
//CreatenewBid
BidnewBid=newBid(bidAmount,item,bidder);
//PlacebidforthisItem
item.getBids().add(newBid);
itemDao.update(item);// 調用DAO完成持久化操作
returnnewBid;
}
}
事務的管理是在ItemManger這一層完成的,ItemManager實現具體的業務邏輯。除了常見的和CRUD有關的簡單邏輯之外,這里還有一個placeBid的邏輯,即項目的競標。
以上是一個完整的第一種模型的示例代碼。在這個示例中,placeBid,loadItemById,findAll等等業務邏輯統統放在ItemManager中實現,而Item只有getter/setter方法。
第二種模型,也就是Martin Fowler指的rich domain object是下面這樣子的:
一個帶有業務邏輯的實體類,即domain object是Item
一個DAO接口ItemDao
一個DAO實現ItemDaoHibernateImpl
一個業務邏輯對象ItemManager
java代碼
publicclass ItemimplementsSerializable{
// 所有的屬性和getter/setter方法同上,省略
publicBidplaceBid(Userbidder,MonetaryAmountbidAmount,
BidcurrentMaxBid,BidcurrentMinBid)
throwsBusinessException{
//Checkhighestbid(canalsobeadifferentStrategy(pattern))
if(currentMaxBid!=null&¤tMaxBid.getAmount().compareTo(bidAmount)>0){
thrownewBusinessException("Bid too low.");
}
//Auctionisactive
if(!state.equals(ItemState.ACTIVE))
thrownewBusinessException("Auction is not active yet.");
//Auctionstillvalid
if(this.getEndDate().before(newDate()))
thrownewBusinessException("Can't place new bid, auction already ended.");
//CreatenewBid
BidnewBid=newBid(bidAmount,this,bidder);
//PlacebidforthisItem
this.getBids.add(newBid);// 請注意這一句,透明的進行了持久化,但是不能在這里調用ItemDao,Item不能對ItemDao產生依賴!
returnnewBid;
}
}
競標這個業務邏輯被放入到Item中來。請注意this.getBids.add(newBid); 如果沒有Hibernate或者JDO這種O/R Mapping的支持,我們是無法實現這種透明的持久化行為的。但是請注意,Item里面不能去調用ItemDAO,對ItemDAO產生依賴!
ItemDao和ItemDaoHibernateImpl的代碼同上,省略。
java代碼
publicclass ItemManager{
privateItemDaoitemDao;
publicvoidsetItemDao(ItemDaoitemDao){this.itemDao=itemDao;}
publicBidloadItemById(Longid){
itemDao.loadItemById(id);
}
publicCollectionlistAllItems(){
returnitemDao.findAll();
}
publicBidplaceBid(Itemitem,Userbidder,MonetaryAmountbidAmount,
BidcurrentMaxBid,BidcurrentMinBid)throwsBusinessException{
item.placeBid(bidder,bidAmount,currentMaxBid,currentMinBid);
itemDao.update(item);// 必須顯式的調用DAO,保持持久化
}
}
在第二種模型中,placeBid業務邏輯是放在Item中實現的,而loadItemById和findAll業務邏輯是放在ItemManager中實現的。不過值得注意的是,即使placeBid業務邏輯放在Item中,你仍然需要在ItemManager中簡單的封裝一層,以保證對placeBid業務邏輯進行事務的管理和持久化的觸發。
這種模型是Martin Fowler所指的真正的domain model。在這種模型中,有三個業務邏輯方法:placeBid,loadItemById和findAll,現在的問題是哪個邏輯應該放在Item中,哪個邏輯應該放在ItemManager中。在我們這個例子中,placeBid放在Item中(但是ItemManager也需要對它進行簡單的封裝),loadItemById和findAll是放在ItemManager中的。
切分的原則是什么呢? Rod Johnson提出原則是“case by case”,可重用度高的,和domain object狀態密切關聯的放在Item中,可重用度低的,和domain object狀態沒有密切關聯的放在ItemManager中。
我提出的原則是:看業務方法是否顯式的依賴持久化。
Item的placeBid這個業務邏輯方法沒有顯式的對持久化ItemDao接口產生依賴,所以要放在Item中。請注意,如果脫離了Hibernate這個持久化框架,Item這個domain object是可以進行單元測試的,他不依賴于Hibernate的持久化機制。它是一個獨立的,可移植的,完整的,自包含的域對象。
而loadItemById和findAll這兩個業務邏輯方法是必須顯式的對持久化ItemDao接口產生依賴,否則這個業務邏輯就無法完成。如果你要把這兩個方法放在Item中,那么Item就無法脫離Hibernate框架,無法在Hibernate框架之外獨立存在。
第三種模型印象中好像是firebody或者是Archie提出的(也有可能不是,記不清楚了),簡單的來說,這種模型就是把第二種模型的domain object和business object合二為一了。所以ItemManager就不需要了,在這種模型下面,只有三個類,他們分別是:
Item:包含了實體類信息,也包含了所有的業務邏輯
ItemDao:持久化DAO接口類
ItemDaoHibernateImpl:DAO接口的實現類
由于ItemDao和ItemDaoHibernateImpl和上面完全相同,就省略了。
java代碼
publicclass ItemimplementsSerializable{
// 所有的屬性和getter/setter方法都省略
privatestaticItemDaoitemDao;
publicvoidsetItemDao(ItemDaoitemDao){this.itemDao=itemDao;}
publicstaticItemloadItemById(Longid){
return(Item)itemDao.loadItemById(id);
}
publicstaticCollectionfindAll(){
return(List)itemDao.findAll();
}publicBidplaceBid(Userbidder,MonetaryAmountbidAmount,
BidcurrentMaxBid,BidcurrentMinBid)
throwsBusinessException{
//Checkhighestbid(canalsobeadifferentStrategy(pattern))
if(currentMaxBid!=null&¤tMaxBid.getAmount().compareTo(bidAmount)>0){
thrownewBusinessException("Bid too low.");
}
//Auctionisactive
if(!state.equals(ItemState.ACTIVE))
thrownewBusinessException("Auction is not active yet.");
//Auctionstillvalid
if(this.getEndDate().before(newDate()))
thrownewBusinessException("Can't place new bid, auction already ended.");
//CreatenewBid
BidnewBid=newBid(bidAmount,this,bidder);
//PlacebidforthisItem
this.addBid(newBid);
itemDao.update(this);// 調用DAO進行顯式持久化
returnnewBid;
}
}
在這種模型中,所有的業務邏輯全部都在Item中,事務管理也在Item中實現。
posted on 2006-10-19 09:19
水煮三國 閱讀(513)
評論(2) 編輯 收藏 所屬分類:
J2EE 、
Hibernate