2

Disclaimer: The post is Spring and Hibernate specific but any generic advice would be helpful too.

I recently started on a project which contains following technology stack Hibernate, Spring MVC and Angular2.

After reading a bunch of blogs and tutorials I found clean architecture has a good set of guidelines and I learned few key concepts:

  1. Domain logic should be independent of the implementation details.
  2. Dependencies should point inwards.
  3. Data crossing boundaries should be plain data structures.

I also googles some key concepts:

  1. DAO, objects which communicates with database(not sure I got this 100%).
  2. DTO, objects which crosses application boundaries something like JSON?

In spring we have this concept of JPA annotation and the entities which are supposed to be heart of business logic serves as DAO too if I am not mistaken.

Taking example of a hypothetical application Imagica the user can upload an image, add tags and comments to it. The server side contains a controller to handle REST requests.

So, taking a couple of scenarios here:

Upload Image

User uploads an Image with some tags. The Front End does some data conversion and my controller gets the data in following format:

{
  id: null,
  caption: string,
  data: <base64 encoded>,
  tags: List<id>
}

Question 1: can I say it as DTO?

Spring does some auto conversion somehow and get a new object created from the given JSON data as method parameter (I think it uses some library called Jackson), I created a class for it named it ImageResource.

@RequestMapping(value="", method=POST)
public void add(ImageResource resource) { ... }

Question 2: ImageResource is also a DTO?

The ImageController is a simple controller which passes this ImageResource to the ImageService as it is. The ImageService depends on ImageEntity and couple Repositories for saving images and Tags, I chose to return nothing on successful save(maybe returning a new ID is a good idea).

Question 3: Is ImageEntity acting as DAO here?

Getting all image data

Skipping all irrelevant details and hoping it well understood.

Question 4: The ImageService returns ImageListResource and the conversion is done by the service itself. I have seen some examples where the service returns entities but I am not sure if that is a right strategy. Coming back to question, whose responsibility it is to convert to ImageListResource, controller or service?

1 Answer 1

1

Question 1: can I say it as DTO?

This is not an object, just serialized data that comes from the user input through a web request.

Question 2: ImageResource is also a DTO?

I'm not familiar with Spring, but from your explanation it seems to me that this is just some helper feature deserializes the input from Question 1 into a custom class defined by you. So, this ImageResource is just a reflex from the information from question 1, which are just basic input from the user; this is still not your domain models.

When you convert this into a ImageEntity and pass it to your service, then this is a DTO, crossing the boundary from the Web app and your core application.

Question 3: Is ImageEntity acting as DAO here?

I think the Repositories you mentioned act as the DAO (data access object), since they do the job of persistence and retrieving necessary info from DB.

Question 4: ... whose responsibility it is to convert to ImageListResource, controller or service?

If the service you mentioned is part of your core application, then it should not perform the conversion; it must be done by some specific part of your client. In this case, I think it's fine for the controller to convert the data in some format appropriate for the client.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.