简体   繁体   English

为什么我应该将回购级别对象与控制器/服务级别对象混合?

[英]Why should i mix repo level objects with controller/service level objects?

I worked on many microservices, and saw we dont mix repo level objects(mogo document, entity) with service/controller level request/response. 我从事过许多微服务,发现我们没有将回购级别对象(mogo文档,实体)与服务/控制器级别的请求/响应混合在一起。 I wanted to know more about it ? 我想了解更多吗? 1. We dont want pojo and entity/document to behave in same way?(Could be one reason) Please help me understanding reasons behind doing this. 1.我们不希望pojo和实体/文档的行为相同吗?(可能是原因之一)请帮助我理解执行此操作的原因。

If you need to separate entities (or persistence objects in general) and public interface responses (such as REST etc.) depends a lot on your application design. 如果您需要分离实体(或通常的持久性对象)和公共接口响应(例如REST等),则在很大程度上取决于您的应用程序设计。

If your persistence objects only contain simple, non-sensitive data, it is ok to use them for public interface responses as well. 如果您的持久性对象仅包含简单的非敏感数据,则也可以将其用于公共接口响应。 But here are some scenarios that would require separate objects: 但是以下是一些需要单独对象的场景:

  1. Your persistence objects contain sensitive data (passwords, encryption keys, etc.) that is meant to be read by the outside. 您的持久性对象包含敏感数据(密码,加密密钥等),该数据应由外部读取。 Then you would convert these objects into other types and stripping away that data to hide it from the public interface. 然后,您可以将这些对象转换为其他类型,并剥离该数据以将其从公共接口隐藏。

  2. You have nested references in your persistence objects (relations between entities etc.) that reach very deep or even can form loops. 您在持久性对象(实体之间的关系等)中嵌套了引用,这些引用达到非常深的甚至可以形成循环。 If you would try to serialize these into a transport format (eg JSON), the process will fail because of the loop or your JSON object will be huge. 如果您尝试将这些序列化为传输格式(例如JSON),则该过程将因循环而失败,否则您的JSON对象将非常庞大。 Then it is easier to convert objects and removing the loops by just including IDs or whatever as your nested references. 然后,仅通过包含ID或任何嵌套引用即可轻松地转换对象并删除循环。

In some applications I developed I even went one step further and settled on three different data object layers. 在开发的某些应用程序中,我什至更进一步,并确定了三个不同的数据对象层。 The first layer is the persistence layer, could be entities, YAML or JSON representations or something like that. 第一层是持久层,可以是实体,YAML或JSON表示或类似的东西。

Since the application uses a lot of different storage backends, and supports multiple for the same data, I convert these specific objects into general domain objects, which form the second layer.The application itself only works on these domain objects, never touching the persistence objects directly. 由于应用程序使用大量不同的存储后端,并且支持多个相同的数据,因此我将这些特定对象转换为第二层的通用域对象。应用程序本身仅适用于这些域对象,而从未接触过持久性对象直。

The third layer is the decorator layer that I use to hide or simplify information for output on my REST interface. 第三层是装饰器层,我用来隐藏或简化信息以在REST接口上输出。 They are used to counter the two scenarios mentioned above. 它们用于应对上述两种情况。

Definitely you could do that, especially in a small project or demo project. 绝对可以的,特别是在小型项目或演示项目中。

Let's assume you are doing web application. 假设您正在做Web应用程序。 For complex enterprise applications, most time you would like to expose certain fields and certain format of data, with one more attraction of data object, you can avoid change entity fields and database table fields. 对于复杂的企业应用程序,大多数时候您希望公开某些字段和某种格式的数据,而又吸引了数据对象,则可以避免更改实体字段和数据库表字段。

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

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