1

I have a problem that bothers me for quite some time, and I also came up with a solution, the question here is rather on how to best implement it, so I'm seeking advice from you if you've ever dealt with that situation before (it's hard to find anything useful on this topic on the web).

Situation

3-tier architecture (rich client such as Swing or Eclipse RCP or Android, web application with implementations of the service layer, relational database). My models are POJOs (plain old Java objects, pure data containers with getters and setters), which are persisted (technical ID on all my models).

I'm often dealing with large models that are aggregatively used, but need to be efficiently read and transported. Let's say I've got following models:

  • User, with login name, password hash, salt, first/last name, e-mail address, authorization credentials, profile picture (Image)
  • Image, with name, content type and (usually large) data
  • Article, with text, and author (User)

Problem

Now when I'm listing or loading an article, I don't want to load the entire author (User), as it exposes too much details (password hash and salt) and carries too much data (credentials, image) for what I actually need in the context of an article (first/last name and e-mail).

Generally speaking: sometimes I need the full details of my models (when creating/editing them, or in very specific situations), but when I use them aggregatively in other models, I'd rather have a simplified form (if I need details, I could load them with a separate request).

Solution

For each model, I could create two variants: a full detail variant with full CRUD (create, read, update, delete) and a simplified, read-only variant, which can be used as a surrogate in aggregative relationships. The simplified model version also contains the technical ID of the detail version, so I could fetch that by demand.

  • For the User: the simplified model just has the first/last name and e-mail.
  • For the Image: the simplified model comes without the image data.
  • The article's author is the simplified version of the user, and the User's profile picture is the simplified version of the image.

Question

  • Is this an existing pattern? It somewhat relates to DTO (Data Transfer Object), but it's not the same. Has anybody seen this before?
  • Have you used something like this before? Any advice or tips on the naming, OO-relationship between the two representations?
Peter Walser
  • 15,208
  • 4
  • 51
  • 78

1 Answers1

1

I cannot correlate your solution option to a pattern that i am aware of. But your requirement(s) can be fulfilled by introducing a very thin API (Web API) on top of your service later.

There are two parts to it,

All in all, what this gives you is a very flexible API with a very clean domain model under your API. And this is a widely accepted approach these days.

Hope that helps.

Community
  • 1
  • 1
Yakaas
  • 191
  • 1
  • 14