There are many different entities in a web application that interact with each other. An example of such an entity would be "user".

In different parts of the application, a different set of user fields is required. For example, on the user edit page, all fields are required; on the page with the user’s login history, only the identifier, name, and last login date are needed.

If you create one class for a user in an application, then there will be an extra consumption of resources for storing extra fields, filling them, etc. This is especially true if you need to serialize an object in json for transmission to the browser (this is done automatically and the extra fields generate significant traffic).

If you create different classes for different parts of the application, you get a lot of duplication of code; many classes for one entity that are hard to maintain; you have to write additional dao - everything is very inconvenient and somehow wrong.

What approaches exist to support in the application different representations of the same entity in different parts of the application?

  • The application now uses Struts and self-signed DAO classes. Struts serializes JSON with the entire hierarchy, which creates many problems. Plus DAO by default unloads from the base the maximum necessary information to fill the object, which is also not ice. - slava
  • I'm not sure, but it seems you are doing premature optimization. Do you really have such a crazy number of fields for a user that this greatly affects resources? - Victor
  • Try to approach this in the standard way — configure the cache, add lazy initsialization for user-related entities. You can use DTO to transfer certain fields. - Victor
  • The user is just an example. There are other entities in which there are many fields plus nested entities. Already, basic server requests return a response of 100–200 kilobytes at the expense of extra fields, and the serialization time is significant. I do not want to immediately attach a pig. - slava
  • Then try to limit the amount of information using DTO'shek and lazy initialization of connections between entities, so as not to pull extra info from the database during serialization. Not sure what is being serialized in json, but for example at Jackson you can filter the serializable fields for a particular controller. - Victor

0