Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As said</p> <blockquote> <p>We have a distinct layer for business logic (usually called Service layer)</p> </blockquote> <p>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</p> <ul> <li>It is Use Case driven</li> <li>It can define Transaction boundaries</li> </ul> <p><strong>Before</strong> </p> <pre><code>@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")) &lt;= 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)); } } </code></pre> <p><strong>After</strong></p> <pre><code>@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); } } </code></pre> <p>Your domain logic is show as follows</p> <pre><code>@Entity public class Item implements Serializable { private List&lt;Bid&gt; bidList = new ArrayList&lt;Bid&gt;(); @OneToMany(cascade=CascadeType.ALL) public List&lt;Bid&gt; getBidList() { return this.bidList; } public void placeBid(User bidder, BigDecimal amount) { if(amount.compareTo(new BigDecimal("0.00")) &lt;= 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)); } } </code></pre> <p>When using Domain-Driven-Design, your business logic lives in the right place. But, <strong>sometimes</strong>, it could be a good idea to define your business logic inside your Service layer. See <a href="http://docs.spring.io/autorepo/docs/spring-roo/1.2.5.RELEASE/reference/html/architecture.html#architecture-services" rel="nofollow noreferrer">here</a> why</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload