本文介绍了“将业务逻辑代码迁移到我们的域模型中”是一个好主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Hibernate in Action,作者建议将业务逻辑移入我们的领域模型(p。306)。例如,在本书提供的示例中,我们有三个实体,名称分别为 Item Bid User 并且作者建议将 placeBid(User bidder,BigDecimal amount)方法添加到 Item class。

考虑到通常我们有一个独特的业务逻辑层(例如 Manager 服务在Spring中的类),除其他外控制交易等,这真的是一个很好的建议?是不是更好的不添加商业逻辑方法给我们的实体?



预先感谢。

解决方案

正如我们所说的那样,我们有一个独特的业务逻辑层(通常称为Service层)

>

Domain-Driven-Design(DDD)声明您应该将业务逻辑放入您的域模型中。而且,相信我,这真的很好。正如POJO在Action中关于服务层所述的那样


  • 它是用例驱动的

  • 它可以定义交易范围



之前

  @Service 
public class BidServiceImpl实现BidService {

@Autowired
private ItemRepository itemRepository;

public void placeBid(Integer itemId,User Bidder,BigDecimal amount){

Item item = itemRepository.getById(itemId);

if(amount.compareTo(new BigDecimal(0.00))抛出新的IllegalStateException(金额必须大于零);

if(!bidder.isEnabled())
抛出新的IllegalStateException(Disabled bidder);

item.getBidList()。add(new Bid(bidder,amount));
}

}

After

  @Service 
公共类BidServiceImpl实现BidService {

@Autowired
private ItemRepository itemRepository;

public void placeBid(Integer itemId,User Bidder,BigDecimal amount){
// itemRepository将检索托管Item实例
Item item = itemRepository.getById(itemId);

item.placeBid(bidder,amount);
}

}

您的网域逻辑显示如下

  @Entity 
public class Item实现Serializable {

private List< Bid> bidList = new ArrayList< Bid>();

@OneToMany(cascade = CascadeType.ALL)
public List< Bid> getBidList(){
返回this.bidList;

$ b $ public void placeBid(User bidder,BigDecimal amount){

if(amount.compareTo(new BigDecimal(0.00))抛出新的IllegalStateException(金额必须大于零);

if(!bidder.isEnabled())
抛出新的IllegalStateException(Disabled bidder);
$ b $ / **
*通过使用自动脏检查
*
* Hibernate将保存我们的出价
* /
item.getBidList( ).add(新投标(投标人,金额));
}

}

当使用Domain-Driven-Design ,你的商业逻辑就生活在正确的地方。但是,有时,在服务层中定义业务逻辑可能是一个好主意。请参阅此处为什么


I am reading Hibernate in Action and the author suggests to move business logic into our domain models (p. 306). For instance, in the example presented by the book, we have three entities named Item, Bid, and User and the author suggests to add a placeBid(User bidder, BigDecimal amount) method to the Item class.

Considering that usually we have a distinct layer for business logic (e.g. Manager or Service classes in Spring) that among other things control transactions, etc. is this really a good advice? Isn't it better not to add business logic methods to our entities?

Thanks in advance.

解决方案

As said

Domain-Driven-Design (DDD) states you should put business logic inside your domain model. And, believe me, it is really good. As said by POJO in Action book about Service layer

  • It is Use Case driven
  • It can define Transaction boundaries

Before

@Service
public class BidServiceImpl implements BidService {

    @Autowired
    private ItemRepository itemRepository;

    public void placeBid(Integer itemId, User bidder, BigDecimal amount) {

        Item item = itemRepository.getById(itemId);

        if(amount.compareTo(new BigDecimal("0.00")) <= 0)
            throw new IllegalStateException("Amount must be greater than zero");

        if(!bidder.isEnabled())
            throw new IllegalStateException("Disabled bidder");

        item.getBidList().add(new Bid(bidder, amount));
    }

}

After

@Service
public class BidServiceImpl implements BidService {

    @Autowired
    private ItemRepository itemRepository;

    public void placeBid(Integer itemId, User bidder, BigDecimal amount) {
        // itemRepository will retrieve a managed Item instance
        Item item = itemRepository.getById(itemId);

        item.placeBid(bidder, amount);
    }

}

Your domain logic is show as follows

@Entity
public class Item implements Serializable {

    private List<Bid> bidList = new ArrayList<Bid>();

    @OneToMany(cascade=CascadeType.ALL)
    public List<Bid> getBidList() {
        return this.bidList;
    }

    public void placeBid(User bidder, BigDecimal amount) {

        if(amount.compareTo(new BigDecimal("0.00")) <= 0)
            throw new IllegalStateException("Amount must be greater than zero");

        if(!bidder.isEnabled())
            throw new IllegalStateException("Disabled bidder");

        /** 
          * By using Automatic Dirty Checking
          * 
          * Hibernate will save our Bid
          */
        item.getBidList().add(new Bid(bidder, amount));
     }

}

When using Domain-Driven-Design, your business logic lives in the right place. But, sometimes, it could be a good idea to define your business logic inside your Service layer. See here why

这篇关于“将业务逻辑代码迁移到我们的域模型中”是一个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 07:46