根据这篇文章here
您可以将它们 [Services] 视为“更高级别的域对象”,但服务不是业务逻辑,而是负责域对象和映射器之间的交互。这些结构最终创建了一个用于与域业务逻辑交互的“公共”接口。您可以避免它们,但代价是会将一些域逻辑泄漏到控制器中。
我一直在阅读 MVC,并将 M 部分拆分为服务、域对象和数据映射器。服务和数据映射器很容易弄清楚,但我不明白域对象的原因,你能给我一些例子吗?这是我的代码:
会员服务
class MemberService extends Service
{
public function authenticate()
{
$domainObject = $this->domainObjectFactory->getDomainObject('Member');
$dataMapper = $this->databaseFactory->getMapper('Member');
$_temp_sess_id = 0;
$_temp_sess_password = "";
$member = $dataMapper->fetch( $_temp_sess_id );
$authenticationResult = $domainObject->checkPassword( $member['password'], $_temp_sess_password );
if (!$authenticationResult)
{
$member = ['user_id' => 0];
}
return $member;
}
}
成员域对象
class MemberDomainObject extends DomainObject
{
public function checkPassword( $dataMapperPassword, $locallyStoredPassword )
{
if ( $dataMapperPassword !== $locallyStoredPassword )
return false;
return true;
}
}
更新:
这个问题是关于方法 checkPassword 以及为什么有必要创建一个单独的对象只是为了使用可以在服务内部使用的 IF 语句,从而节省 RAM 使用额外资源来创建新对象。