Do I really need a service layer?

后端 未结 5 1137
执念已碎
执念已碎 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:22

    You don't need a service layer. However it helps you to

    • Decouple your components
    • You can enforce specific business rules in your service layer which should be agnostic to your repository
    • Let a service facade one or more repositories. Let's consider the following sample
    class Service {
      private DatabaseBarRepo barRepo;
      private DatabaseFooRepo fooRepo;
    
      @Transactional
      public void serviceRoutine() {
         barRepo.doStuff();
         fooRepo.doStuff();
      }
    }
    

    Here we let two separate repositories take part in the same transaction. This is specific for databases albeit the principles are valid for other systems as well.

提交回复
热议问题