3

I have seen it done both ways, but I have never seen any discussion around which is the more "right" way.

Say you have a entity that has children relationships to other entities. The children can't exist without the parent, so does it make more sense to make on service that handles the CRUD of the parent AND the children, or should you keep the services aligned to a single entity (so one for the parent, one for [each of] the children)?

2 Answers 2

2

If the children are useful on their own (if the parent is optional or can be ignored for some operations), then they should get their own service.

If the separation is only to provide a collection in the parent, probably not.

0

You need only write some sample code.

Blog blog = new Blog()

blog.addPost(new Blog post())

blogService.save(blog)

// Do you expect the posts to exist in the DB at this line?

If you answer "yes" then save parent and children in a single service. Otherwise split them.

Thinking about how the code will be used and how other programmers will interpret the code will give you clarity.

The Principal of Least Surprise is your friend here.

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.