为什么要初始化一个分配了所有属性的域对象?
相反,只需创建一个空的Domain Object。您可以在工厂检查它是否有prepare()
执行方法。哦.. 如果您使用的是DAO,而不是直接与Mappers交互,您可能希望在您的域对象中构造和注入适当的DAO。
值的分配应该只发生在Service中。通过使用普通的二传手。
一些例子:
检索现有文章
public function retrieveArticle( $id )
{
$mapper = $this->mapperFactory->create('Article');
$article = $this->domainFactory->create('Article');
$article->setId( $id );
$mapper->fetch( $article );
$this->currentArticle = $article;
}
发表新评论
public function addComment( $id, $content )
{
$mapper = $this->mapperFactory->create('article');
$article = $this->domainFactory->create('Article');
$comment = $this->domainFactory->create('Comment');
$comment->setContent( $content );
$comment->setAuthor( /* user object that you retrieved from Recognition service */ );
$article->setId( $id );
$article->addComment( $comment );
// or you might retrieve the ID of currently view article
// and assign it .. depends how you build it all
$mapper->store( $article ); // or
}
传递用户输入
public function getArticle( $request )
{
$library = $this->serviceFactory->build('Library');
$library->retrieveArticle( $request->getParameter('articleId'));
}