dongwu5318 2012-08-13 17:19
浏览 47
已采纳

与工厂的依赖注入

I have been practicing constructor dependency injection throughout my PHP application. I didn't want to be littering my code with object creation, so factories to the rescue, or at least I thought.

I set about wiring up components with factories, then some factories started using other factories to get dependencies, great, keeps all the creation code in one place. However, once factories start using each other (or as in the code below, itself) I ran into circular dependency issues, that simply cannot be resolved. For example, my MapperFactory uses itself to inject mappers with other mappers (they need each other to build a full object graph 'eager loading'):

class MapperFactory
{   
    public function create($type)
    {
        switch (true) {
            case 'Item':
                $mapper = new ItemMapper(
                    $this->create('Field')  
                );               
                break;
            case 'Field':
                $mapper = new ItemMapper(
                    $this->create('Item')  
                );
                break;
            default:
                throw new Exception('Unknown mapper');
        }
        return $mapper;
    }

}

$mf = new MapperFactory();
$mf->create('Item');

Its a simplified example, but an increasingly common issue as the application is developing. Error back from PHP (xdebug installed) is:

Fatal error: Maximum function nesting level of '100' reached, aborting!

Fully understand why PHP is complaining (although didn't see it coming TBH).

My question is, have I completely missed the point of factories? am I using factories correctly? It would seem not, but other than the circular dependency (pretty major but), factories are an elegant solution to hiding all the construction/wiring logic away from the main application.

  • 写回答

2条回答 默认 最新

  • dooid3005 2012-08-13 17:57
    关注

    You could try using a setter for injecting the dependencies. Then you'd create both mappers like this:

    $itemMapper = new Mapper();
    $fieldMapper = new Mapper();
    $itemMapper->setRelatedMapper($fieldMapper);
    $fieldMapper->setRelatedMapper($itemMapper);
    

    And then use the switch just to return the mapper. This should get rid of the circular dependencies when creating objects.

    Having said that, if you are doing this as a sort of OR/M thing to connect to a database, you should maybe look into stuff like Doctrine2 or Propel, just to save yourself the trouble of inventing the wheel when there are already tried and tested solutions out there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退