At first I had the following logic of user creation:
class UserService {
constructor(userRepository, postRepository) {}
createUser({user}) {
this.userRepository.create(user);
}
}
Then I also got another use case when while creating a user we also have to create a default post for him.
The new use case should be identical to the previous one, just additionally create a post. And I see two different ways of how it can be implemented:
- Create a separate method for new use case and inside of it call the user-creation use case.
class UserService {
constructor(userRepository, postRepository) {}
createUser({user}) {
this.userRepository.create(user);
}
createUserWithDefaultPost({user, post}) {
this.createUser({user});
this.postRepository.create(post);
}
}
- Just add a new condition for a situation when post data is passed:
class UserService {
constructor(userRepository, postRepository) {}
createUser({user, post}) {
this.userRepository.create(user);
if (post) {
this.postRepository.create(post);
}
}
}
In my opinion, an approach with separate method creation looks better, and cleaner in some way, but that's just kind of my feelings and I can't give some clear explanation why it should be done.
Somehow it's definitely related to separation of concerns, but approach with "if" condition still looks pretty normal.
createPost(user, post)and call it.