asp.net

asp.net,c#
Pattern of Domain Object(3)

第三种模型印象中好像是firebody或者是Archie提出的(也有可能不是,记不清楚了),简单的来说,这种模型就是把第二种模型的domain object和business object合二为一了。所以ItemManager就不需要了,在这种模型下面,只有三个类,他们分别是:

Item:包含了实体类信息,也包含了所有的业务逻辑
ItemDao:持久化DAO接口类
ItemDaoHibernateImpl:DAO接口的实现类

由于ItemDao和ItemDaoHibernateImpl和上面完全相同,就省略了。

java 代码
  1. public class Item implements Serializable {   
  2.     //  所有的属性和getter/setter方法都省略   
  3.    private static ItemDao itemDao;   
  4.     public void setItemDao(ItemDao itemDao) {this.itemDao = itemDao;}   
  5.        
  6.     public static Item loadItemById(Long id) {   
  7.         return (Item) itemDao.loadItemById(id);   
  8.     }   
  9.     public static Collection findAll() {   
  10.         return (List) itemDao.findAll();   
  11.     }   
  12.   
  13.     public Bid placeBid(User bidder, MonetaryAmount bidAmount,   
  14.                     Bid currentMaxBid, Bid currentMinBid)   
  15.     throws BusinessException {   
  16.        
  17.         // Check highest bid (can also be a different Strategy (pattern))   
  18.         if (currentMaxBid != null && currentMaxBid.getAmount().compareTo(bidAmount) > 0) {   
  19.             throw new BusinessException("Bid too low.");   
  20.         }   
  21.            
  22.         // Auction is active   
  23.         if ( !state.equals(ItemState.ACTIVE) )   
  24.             throw new BusinessException("Auction is not active yet.");   
  25.            
  26.         // Auction still valid   
  27.         if ( this.getEndDate().before( new Date() ) )   
  28.             throw new BusinessException("Can't place new bid, auction already ended.");   
  29.            
  30.         // Create new Bid   
  31.         Bid newBid = new Bid(bidAmount, this, bidder);   
  32.            
  33.         // Place bid for this Item   
  34.         this.addBid(newBid);   
  35.         itemDao.update(this);      //  调用DAO进行显式持久化   
  36.         return newBid;   
  37.     }   
  38. }  

 

在这种模型中,所有的业务逻辑全部都在Item中,事务管理也在Item中实现。

posted on 2007-03-20 21:14  灵魂边缘  阅读(155)  评论(0编辑  收藏  举报