From all the tutorials I’ve read the service layer only seems to only have one or two methods in it so I am unsure whether services are only supposed to be lightweight/skinny as opposed to fat with as many methods as you need
If I have a Post domain object and have a PostService class is this what you are ment to do from within a controller if you wanted to delete a post:
$postService = $this->serviceFactory->build('post');
$postService->deletePost($id);
and the deletePost() method inside the PostService is something like:
$postMapper = $this->dataMapperFactory->build('post');
$post = $postMapper->fetchById($id);
// Check if the post exists
// Check if it belongs to this user
// Some other checks
$postMapper->delete($post);
Is that correct? Essentially are domain objects just value objects and all the work gets done in the service layers?
Any help would be great thanks.
It seems, that part of your problem is actually in the mappers. IMHO, mappers should not be responsible for creating the domain objects. So, your example code actually should look more like:
Also, most of those "other checks" are actually done on the domain object. For example:
The role of the service is "application logic", which is a term to describe interaction between domain objects and the mappers. It is also common that service interact with other services.
As a side-note
Having a
PostServicemakes no sense to me. Service are supposed to represent major segments of domain business logic in the model layer.Recognitionservice instead ofUserServiceandLoginService.Contentservice instead ofDocumentServiceandCommentServiceandUserServiceOh.. and also, you to not need to add
..Serviceor..Controllerpostfixes anymore. PHP has namespaces now.