在最近的圍繞domain object的討論中浮現出來了三種模型,(還有一些其他的旁枝,不一一分析了),經過一番討論,各種問題逐漸清晰起來,在這里我試圖做一個總結,便于大家了解和掌握。
第一種模型:只有getter/setter方法的純數據類,所有的業務邏輯完全由business object來完成(又稱TransactionScript),這種模型下的domain object被Martin Fowler稱之為“貧血的domain object”。下面用舉一個具體的代碼來說明,代碼來自Hibernate的caveatemptor,但經過我的改寫:
一個實體類叫做Item,指的是一個拍賣項目
一個DAO接口類叫做ItemDao
一個DAO接口實現類叫做ItemDaoHibernateImpl
一個業務邏輯類叫做ItemManager(或者叫做ItemService)
ItemDao定義持久化操作的接口,用于隔離持久化代碼。
ItemDaoHibernateImpl完成具體的持久化工作,請注意,數據庫資源的獲取和釋放是在ItemDaoHibernateImpl里面處理的,每個DAO方法調用之前打開Session,DAO方法調用之后,關閉Session。(Session放在ThreadLocal中,保證一次調用只打開關閉一次)
事務的管理是在ItemManger這一層完成的,ItemManager實現具體的業務邏輯。除了常見的和CRUD有關的簡單邏輯之外,這里還有一個placeBid的邏輯,即項目的競標。
以上是一個完整的第一種模型的示例代碼。在這個示例中,placeBid,loadItemById,findAll等等業務邏輯統統放在ItemManager中實現,而Item只有getter/setter方法。
第一種模型:只有getter/setter方法的純數據類,所有的業務邏輯完全由business object來完成(又稱TransactionScript),這種模型下的domain object被Martin Fowler稱之為“貧血的domain object”。下面用舉一個具體的代碼來說明,代碼來自Hibernate的caveatemptor,但經過我的改寫:
一個實體類叫做Item,指的是一個拍賣項目
一個DAO接口類叫做ItemDao
一個DAO接口實現類叫做ItemDaoHibernateImpl
一個業務邏輯類叫做ItemManager(或者叫做ItemService)
- 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方法省略不寫,避免篇幅太長
- }
- public interface ItemDao {
- public Item getItemById(Long id);
- public Collection findAll();
- public void updateItem(Item item);
- }
ItemDao定義持久化操作的接口,用于隔離持久化代碼。
- 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中,保證一次調用只打開關閉一次)
- public class ItemManager {
- private ItemDao itemDao;
- public void setItemDao(ItemDao itemDao) { this.itemDao = itemDao;}
- public Bid loadItemById(Long id) {
- itemDao.loadItemById(id);
- }
- public Collection listAllItems() {
- return itemDao.findAll();
- }
- public Bid placeBid(Item item, User bidder, MonetaryAmount bidAmount,
- Bid currentMaxBid, Bid currentMinBid) throws BusinessException {
- if (currentMaxBid != null && currentMaxBid.getAmount().compareTo(bidAmount) > 0) {
- throw new BusinessException("Bid too low.");
- }
- // Auction is active
- if ( !state.equals(ItemState.ACTIVE) )
- throw new BusinessException("Auction is not active yet.");
- // Auction still valid
- if ( item.getEndDate().before( new Date() ) )
- throw new BusinessException("Can't place new bid, auction already ended.");
- // Create new Bid
- Bid newBid = new Bid(bidAmount, item, bidder);
- // Place bid for this Item
- item.getBids().add(newBid);
- itemDao.update(item); // 調用DAO完成持久化操作
- return newBid;
- }
- }
事務的管理是在ItemManger這一層完成的,ItemManager實現具體的業務邏輯。除了常見的和CRUD有關的簡單邏輯之外,這里還有一個placeBid的邏輯,即項目的競標。
以上是一個完整的第一種模型的示例代碼。在這個示例中,placeBid,loadItemById,findAll等等業務邏輯統統放在ItemManager中實現,而Item只有getter/setter方法。
安徽新華電腦學校專業職業規劃師為你提供更多幫助【在線咨詢】