在最近的圍繞domain object的討論中浮現(xiàn)出來了三種模型,(還有一些其他的旁枝,不一一分析了),經(jīng)過一番討論,各種問題逐漸清晰起來,在這里我試圖做一個總結(jié),便于大家了解和掌握。
第一種模型:只有g(shù)etter/setter方法的純數(shù)據(jù)類,所有的業(yè)務(wù)邏輯完全由business object來完成(又稱TransactionScript),這種模型下的domain object被Martin Fowler稱之為“貧血的domain object”。下面用舉一個具體的代碼來說明,代碼來自Hibernate的caveatemptor,但經(jīng)過我的改寫:
一個實(shí)體類叫做Item,指的是一個拍賣項(xiàng)目
一個DAO接口類叫做ItemDao
一個DAO接口實(shí)現(xiàn)類叫做ItemDaoHibernateImpl
一個業(yè)務(wù)邏輯類叫做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完成具體的持久化工作,請注意,數(shù)據(jù)庫資源的獲取和釋放是在ItemDaoHibernateImpl里面處理的,每個DAO方法調(diào)用之前打開Session,DAO方法調(diào)用之后,關(guān)閉Session。(Session放在ThreadLocal中,保證一次調(diào)用只打開關(guān)閉一次)
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);// 調(diào)用DAO完成持久化操作
returnnewBid;
}
}
事務(wù)的管理是在ItemManger這一層完成的,ItemManager實(shí)現(xiàn)具體的業(yè)務(wù)邏輯。除了常見的和CRUD有關(guān)的簡單邏輯之外,這里還有一個placeBid的邏輯,即項(xiàng)目的競標(biāo)。
以上是一個完整的第一種模型的示例代碼。在這個示例中,placeBid,loadItemById,findAll等等業(yè)務(wù)邏輯統(tǒng)統(tǒng)放在ItemManager中實(shí)現(xiàn),而Item只有g(shù)etter/setter方法。
第二種模型,也就是Martin Fowler指的rich domain object是下面這樣子的:
一個帶有業(yè)務(wù)邏輯的實(shí)體類,即domain object是Item
一個DAO接口ItemDao
一個DAO實(shí)現(xiàn)ItemDaoHibernateImpl
一個業(yè)務(wù)邏輯對象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);// 請注意這一句,透明的進(jìn)行了持久化,但是不能在這里調(diào)用ItemDao,Item不能對ItemDao產(chǎn)生依賴!
returnnewBid;
}
}
競標(biāo)這個業(yè)務(wù)邏輯被放入到Item中來。請注意this.getBids.add(newBid); 如果沒有Hibernate或者JDO這種O/R Mapping的支持,我們是無法實(shí)現(xiàn)這種透明的持久化行為的。但是請注意,Item里面不能去調(diào)用ItemDAO,對ItemDAO產(chǎn)生依賴!
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);// 必須顯式的調(diào)用DAO,保持持久化
}
}
在第二種模型中,placeBid業(yè)務(wù)邏輯是放在Item中實(shí)現(xiàn)的,而loadItemById和findAll業(yè)務(wù)邏輯是放在ItemManager中實(shí)現(xiàn)的。不過值得注意的是,即使placeBid業(yè)務(wù)邏輯放在Item中,你仍然需要在ItemManager中簡單的封裝一層,以保證對placeBid業(yè)務(wù)邏輯進(jìn)行事務(wù)的管理和持久化的觸發(fā)。
這種模型是Martin Fowler所指的真正的domain model。在這種模型中,有三個業(yè)務(wù)邏輯方法:placeBid,loadItemById和findAll,現(xiàn)在的問題是哪個邏輯應(yīng)該放在Item中,哪個邏輯應(yīng)該放在ItemManager中。在我們這個例子中,placeBid放在Item中(但是ItemManager也需要對它進(jìn)行簡單的封裝),loadItemById和findAll是放在ItemManager中的。
切分的原則是什么呢? Rod Johnson提出原則是“case by case”,可重用度高的,和domain object狀態(tài)密切關(guān)聯(lián)的放在Item中,可重用度低的,和domain object狀態(tài)沒有密切關(guān)聯(lián)的放在ItemManager中。
我提出的原則是:看業(yè)務(wù)方法是否顯式的依賴持久化。
Item的placeBid這個業(yè)務(wù)邏輯方法沒有顯式的對持久化ItemDao接口產(chǎn)生依賴,所以要放在Item中。請注意,如果脫離了Hibernate這個持久化框架,Item這個domain object是可以進(jìn)行單元測試的,他不依賴于Hibernate的持久化機(jī)制。它是一個獨(dú)立的,可移植的,完整的,自包含的域?qū)ο?/span>。
而loadItemById和findAll這兩個業(yè)務(wù)邏輯方法是必須顯式的對持久化ItemDao接口產(chǎn)生依賴,否則這個業(yè)務(wù)邏輯就無法完成。如果你要把這兩個方法放在Item中,那么Item就無法脫離Hibernate框架,無法在Hibernate框架之外獨(dú)立存在。
第三種模型印象中好像是firebody或者是Archie提出的(也有可能不是,記不清楚了),簡單的來說,這種模型就是把第二種模型的domain object和business object合二為一了。所以ItemManager就不需要了,在這種模型下面,只有三個類,他們分別是:
Item:包含了實(shí)體類信息,也包含了所有的業(yè)務(wù)邏輯
ItemDao:持久化DAO接口類
ItemDaoHibernateImpl:DAO接口的實(shí)現(xiàn)類
由于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);// 調(diào)用DAO進(jìn)行顯式持久化
returnnewBid;
}
}
在這種模型中,所有的業(yè)務(wù)邏輯全部都在Item中,事務(wù)管理也在Item中實(shí)現(xiàn)。
posted on 2006-10-19 09:19
水煮三國 閱讀(513)
評論(2) 編輯 收藏 所屬分類:
J2EE 、
Hibernate