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:
- Domain logic should be independent of the implementation details.
- Dependencies should point inwards.
- Data crossing boundaries should be plain data structures.
I also googles some key concepts:
- DAO, objects which communicates with database(not sure I got this 100%).
- 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?