如何在PHP中建立成功的域对象工厂 [英] How can I Make a Successful Domain Object Factory in PHP

查看:156
本文介绍了如何在PHP中建立成功的域对象工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搞一个MVC框架,我偶然发现一个问题,我不知道如何解决。

I'm fiddling with an MVC framework, and I stumbled upon a problem I'm not sure how to solve.

我想要一个 DomainObjectFactory 为我的应用程序的Model层,但是每个Domain对象将有一组不同的参数,例如:

I want to make a DomainObjectFactory for the Model layer of my application, however, each Domain object would have a different set of arguments, for instance:


  • 个人 - $ id,$ name,$ age。

  • 发布 - $ id,$ author,$ title,$ content,$ comments

  • 评论 - $ id,$ author,$ content

等等。我如何能够轻易地告诉我工厂需要什么样的对象?

And so on. How can I easily tell my factory what kinds of object do I require?

我想出了几个选项:


  • 传递数组 - 我不喜欢这个数组,因为你不能依赖构造函数的合同来告诉他的对象需要什么工作。

  • DomainObjectFactory 一个接口,并制作具体的类 - 有问题,因为这是很多工厂制作的!

  • 使用反射 - 服务定位器多?我不知道,这似乎是对我的。

  • Pass an array - I dislike this one, because you can't rely on the constructor's contract to tell what the object needs for his work.
  • Make the DomainObjectFactory an interface, and make concrete classes - Problematic, because that's an awful lot of factories to make!
  • Use Reflection - Service locator much? I don't know, it just seems that way to me.

这里有一个有用的设计模式吗?或者其他一些聪明的解决方案?

Is there a useful deign pattern I can employ here? Or some other clever solution?

推荐答案

为什么要初始化一个 域对象 分配了所有属性?

Why do you want to initialize a Domain Object with all the properties assigned?

相反,只需创建一个空的域对象。你可能在工厂检查,如果它有 prepare()方法执行。哦,如果您使用的是 DAO ,而不是直接与 Mappers 进行交互,您可能需要构建并在您的域对象中注入适当的 DAO

Instead just create an empty Domain Object. You might in the factory check, if it has prepare() method to execute. Oh .. and if you are using DAO, instead of directly interacting with Mappers, you might want to construct and inject the appropriate DAO in your Domain Object.

值的赋值应该发生在 服务

The assignment of values should just happen in the Service. By the use of ordinary setters.


检索现有文章

Retrieving existing article



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'));
}

这篇关于如何在PHP中建立成功的域对象工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆