Home > Net >  Is it a bad practice to use service layer in domain layer? [closed]
Is it a bad practice to use service layer in domain layer? [closed]

Time:09-17

I'm using Spring, JPA and AWS S3 services.

In my project...
There is an S3Service class in the service layer.
There is an image class in the domain layer which has an attribute of the name of the image file on S3.

The related S3 files should be deleted whenever the tuples of the image table are deleted from the database.
So, I want to use @PreRemove annotation in image so that the image can use the delete method of the S3Service before being removed.

Is it a bad practice to access service layer in domain layer?
If so, how could I delete S3 files when the tuples are deleted?

CodePudding user response:

I would say that the Service that removes the data from the entities (or call the respective methods on a Repository) should also call S3Service to remove such files from S3.

In my opinion, you should strive to keep the Services talking to each other whenever possible so that you keep your business logic in such a layer. Otherwise, you might end up with spaghetti code that is hard to read, maintain and change.


Another approach could be let the S3 deletion be event-driven. In this case, whenever you delete a Post, you would publish an event with the IDs of the images to delete or with the ID of the deleted Post (if with that you are able to find the images to delete on S3). This would decouple the deletion of the entities in the DB from the deletion of the images in S3. This would be my preferred approach.

  • Related