I'm refactoring an application into service objects to keep the models smaller and the actual code files more organized.
To that end, I'm utilizing service objects that perform actions on a given object. However, one of the dilemmas I'm facing is whether I pass in the entire object to the service or just an ID.
A classic example is updating a user's email address. The reason I would use a service object instead of just a method in the model, is that this email also has to sync with external 3rd-party systems that do not belong in the user model.
class User::UpdateEmail
# Passing entire user object
def self.update_for_user(user, new_email)
if user.update_attributes(email: new_email)
# do some API calls to update email in external services
true
else
false
end
end
# Just passing user ID
def self.update_for_user_id(user_id, new_email)
user = User.find(user_id)
if user.update_attributes(email: new_email)
# do some API calls to update email in external services
true
else
false
end
end
end
Is there any benefit to one vs the other? They both "seem" to do the same thing and it just looks like personal preference to me, but I'm curious if I would run into a situation where I pass in an entire user object and somehow it gets stale while the service object works on it.
If I pass in the entire user, then the class that calls the service object would need to do the checking to ensure the user exists and so forth, whereas if I just pass a user_id then the service object now has to ensure a valid object, etc.
is there an agreed upon or expected standard or pattern for this type of service-oriented operation? I want to make sure I use a consistent approach throughout the entire application and also don't force myself into a hole later on.