从我读过的所有教程来看,服务层似乎只有一两个方法,所以我不确定服务是否只应该是轻量级的/瘦的,而不是胖的,你需要多少方法就有多少
如果我有一个Post域对象和一个PostService类,如果你想删除帖子,你需要从控制器中这样做:
$postService = $this->serviceFactory->build('post');
$postService->deletePost($id);PostService中的deletePost()方法类似于:
$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);对吗?从本质上说,域对象只是值对象,所有的工作都在服务层完成吗?
任何帮助都是非常感谢的。
发布于 2013-02-17 21:37:01
看起来,问题的一部分实际上出在映射器中。IMHO,映射器不应该负责创建域对象。因此,您的示例代码实际上应该看起来更像:
$mapper = $this->dataMapperFactory->build('post');
$post = $this->domainObjectFactory->build('post');
$post->setId( $id );
$mapper->fetch($post);
// Check if the post exists
// Check if it belongs to this user
// Some other checks
$postMapper->delete($post);而且,大多数这些“其他检查”实际上是在域对象上完成的。例如:
if ( $post->belongsTo($user) )
{
...
}服务的角色是“应用程序逻辑”,这是一个用来描述域对象和映射器之间交互的术语。服务与其他服务交互也很常见。
作为附注
拥有一个PostService对我来说没有任何意义。服务应该表示模型层中的领域业务逻辑的主要部分。
Recognition服务,而不是UserService和LoginService.Content服务,而不是DocumentService、CommentService和CommentService哦..。而且,您不再需要添加..Service或..Controller后缀。PHP现在有了名称空间。
https://stackoverflow.com/questions/14921492
复制相似问题