简体   繁体   English

模块化应用程序的设计模式(如何重用实体)

[英]Design pattern for modular application (how to reuse entities)

I have the following scenario: 我有以下情况:

  1. A JAX-RS Webservice that is responsable for the business logic and database interactions. 一个JAX-RS Web服务,负责业务逻辑和数据库的交互。
  2. A webapp that will be used by the end users. 最终用户将使用的Web应用程序。
  3. A webapp that will be used by administrators. 管理员将使用的Web应用程序。

My problem is that I want to reuse the entities from the webservice on the other apps, but it is highly wrapped with frameworks like JPA, JAX-RS, CDI, among others... So I am having a hard time to isolate them. 我的问题是我想在其他应用程序上重用Web服务中的实体,但是它被诸如JPA,JAX-RS,CDI等框架高度封装了……所以我很难隔离它们。 What I want is to know the best workaround and why should I use it instead of others. 我要知道的是最佳的解决方法,为什么我应该使用它来代替其他方法。

Maybe DTO is the way to go (with support from some object mapper library like Dozer ) 也许DTO可行的方式(在某些对象映射器库(如Dozer )的支持下)

Please take a look at following article for more details: http://zezutom.blogspot.com/2012/02/thoughts-on-data-transfer-objects.html 请查看以下文章以了解更多详细信息:http: //zezutom.blogspot.com/2012/02/thoughts-on-data-transfer-objects.html

Write you entity objects as Plain Old Java Objects (POJOs), with proper constructors, setters, etc. Apply the annotations that allow the JPA to persist them and do the object to relational mapping in such a way that, if those annotations were all stripped away you could still create and manipulate those objects fully, using the public methods of the class. 使用适当的构造函数,setter等将您的实体对象编写为Plain Old Java Objects(POJO)。应用允许JPA保留它们的注释,并以一种方式将对象进行关系映射,如果所有注释都被剥离您仍然可以使用类的公共方法完全创建和操纵这些对象。 It can be helpful if you create the POJO first, then add the annotations afterwards. 如果先创建POJO,然后再添加注释,则可能会有所帮助。

As the POJOs stand alone they are not at all part of your repository layer. 由于POJO独立存在,因此它们并不是您存储库层的全部。 You can use them without using the JPA at all. 您可以完全不使用JPA来使用它们。

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

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