-I'm trying to figure out how to identify Application Services in my application. I think I can identify a Domain service by 2 things:
- It acts as a facade to the repository.
- It holds business logic that can't be encapsulated in a single entity.
So I thought about this simple use case that I'm currently working on:
An admin should be able to ban a certain user. The operation must be logged and an email must be sent to the banned user.
I have a UserRepository
which has a function getUserById()
. I have a User
entity which has a ban()
function.
I'll create a UserService
like this:
class UserService{
static void banUser(int userId){
User user= UserRepository.getUserById(userId);
user.ban();
UserRepository.update(user);
}
}
The userId
is a POST
variable that I'll be receiving in my Controller
from a web form.
Now where does the logging go? I need to log the ban process twice before and after the operation ex:
Logger.log("Attempting to ban user with id:"+userId);
//user gets banned
Logger.log("user banned successfully.");
Second, I need to send a mail to the user. where should the call go?
I thought about putting the logging and email is the UserService
class itself, but I think there are better solutions.
Third, where do Application Services fit in all of this?
ban
method on theuser
entity. Instead, I would add a new method to theadmin
entity calledBan(User useruserToBan)
. You do have an admin entity right?