简体   繁体   English

我们在构建 Jhipster 应用程序时如何选择业务逻辑?

[英]How do we choose the business logic when building a Jhipster application?

When creating a entity with the Jhipster helper, it asks使用 Jhipster 助手创建实体时,它会询问

? Do you want to use separate service class for your business logic? (Use arrow keys)
> No, the REST controller should use the repository directly
  Yes, generate a separate service class
  Yes, generate a separate service interface and implementation

In which case should I use which option?在什么情况下我应该使用哪个选项?

What are the benefits and flaws of each solution?每种解决方案的优点和缺点是什么?

Is it possible to change easily the architecture once everything is set?一切就绪后是否可以轻松更改架构?

IMHO it depends on how complex your application is going to be and how long you plan on having to maintain it.恕我直言,这取决于您的应用程序的复杂程度以及您计划维护它的时间。

If your domain model is quite simple and your REST controllers are straightforward CRUD operations without complex mapping, you can get away without using a separate service layer.如果您的域 model 非常简单,并且您的 REST 控制器是简单的 CRUD 操作而无需复杂的映射,那么您无需使用单独的服务层即可摆脱困境。

If your domain model or interactions get more complex, you might need a 'Separation of Concerns': your Controller classes should just map REST calls from/to the correct DTO's for the REST API, and business logic and coordination between different entities should go in a service class that does not have anything to do with the REST API. In the long term, that makes it easier to make changes in the REST API separate from changes in the business logic. If your domain model or interactions get more complex, you might need a 'Separation of Concerns': your Controller classes should just map REST calls from/to the correct DTO's for the REST API, and business logic and coordination between different entities should go in服务 class 与 REST API 没有任何关系。从长远来看,这使得 REST API 的更改与业务逻辑的更改分开变得更容易。

Some blog posts to read:一些博客文章阅读:

https://www.petrikainulainen.net/software-development/design/understanding-spring-web-application-architecture-the-classic-way/ https://www.petrikainulainen.net/software-development/design/understanding-spring-web-application-architecture-the-classic-way/

https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html

Then about the decision to use interfaces or not.然后关于是否使用接口的决定。 The main advantages of using interfaces used to be that it allowed better testing and avoided coupling modules too close.使用接口的主要优点过去是它允许更好的测试并避免将模块耦合得太近。 But since 2010, there has been a lot of discussion whether it's worth the overhead.但自 2010 年以来,人们一直在讨论它是否值得开销。 Maybe start reading the discussion underneath Adam Bien's original post:也许开始阅读 Adam Bien 原帖下面的讨论:

https://www.adam-bien.com/roller/abien/entry/service_s_new_serviceimpl_why https://www.adam-bien.com/roller/abien/entry/service_s_new_serviceimpl_why

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM