【发布时间】:2016-04-09 18:48:23
【问题描述】:
据本文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 使用额外资源来创建新对象。
【问题讨论】:
-
好吧,它说:“域对象是数据,域服务是数据部分。”。所以我必须在服务部分执行所有 if,同时将 $member 存储在 DO 中?
-
另外,根据这里的答案:stackoverflow.com/questions/5863870/…,“您可以将它们 [Services] 视为“更高级别的域对象”,但服务负责域之间的交互而不是业务逻辑对象和映射器。”
标签: php service domain-object