Do I really need a service layer?

后端 未结 5 1145
执念已碎
执念已碎 2020-12-08 07:51

My web application is written using Spring MVC + Hibernate.

  • My model is \"Customer\" entity POJO.
  • I have got a DAO object \"CustomerDAO\", its method
5条回答
  •  情书的邮戳
    2020-12-08 08:23

    The key is the transactional behavior. If you don't have a service layer, where will you demarcate transactions?

    • in the presentation layer: that's not where transaction demarcation belongs, and you wouldn't be able to use declarative transaction demarcation
    • in the DAO layer: you would not be able to create a customer and its contact (for example) in a single transaction, because it would use two different DAOs

    Moreover, you want your UI layer as simple as possible, and the business code (which sometimes is much more complex than sinply calling a DAO method) isolated in specific components. This allows

    • unit testing the UI layer by mocking the service layer
    • unit testing the service layer (the business code) by mocking the DAO layer

提交回复
热议问题