首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >最佳实践Symfony2 (工厂)服务

最佳实践Symfony2 (工厂)服务
EN

Stack Overflow用户
提问于 2015-05-16 09:13:52
回答 1查看 792关注 0票数 5

昨天,我在IRC Symfony2频道上讨论了最佳实践方法和教程(书,sf2-网站上的食谱)。

  1. 有人说服务总是无状态的。他的意思是,不应该连接到服务类中的实体。但是,如果我需要数据库预先格式化的内容,我应该如何处理用户生成的内容?
  2. 虽然有人说该服务应该是无状态的,但我基于本教程创建了一个服务和一个工厂服务:http://brentertainment.com/2012/02/28/contextualizing-your-symfony2-application-with-the-service-container/

我的配置服务的命名目前有点糟糕,但目前我没有任何策略或良好的命名概念。

目前,我在控制器中调用$this->get('test.conf')->getCategory('data-browser')。这样做“感觉很好”,因为我不需要在控制器中重复这个函数。然后,服务test.conf调用工厂服务test.conf_factory,该服务获取service.configuration的实例。

问题:

  • 服务是否像我在这里一样是获取预先格式化内容的合适位置?
  • 服务配置正确吗?或者,我可以以任何方式简化test.conf_factorytest.conftest.configuration吗?目前我真的不知道我做得对不对。
  • 使用服务test.configuration并创建另一个服务(它只是在构造函数中创建一个test.configuration实例并通过方法'get‘实例的调用返回)是否更好(或者说是可能的)?(injection/introduction.html)是否有像大多数symfony开发人员那样被证明的方法?

配置工厂:

代码语言:javascript
复制
namespace Test\CoreBundle\Factory;

use Test\CoreBundle\Entity\Account;
use Test\CoreBundle\Service\Configuration;


class ConfigurationFactory
{
    private $user;
    private $configuration;

public function __construct(Account $user = null, Configuration $configuration)
    {
        $this->user = $user;
        dump($user);
        $this->configuration = $configuration;
    }

    public function get()
    {
        if ($this->user) {
         //   dump($this->user);
            $this->configuration->setAccountid($this->user->getId());
        }
        return $this->configuration;
    }
}

配置序列化:

代码语言:javascript
复制
<?php
namespace test\CoreBundle\Service;

class Configuration {

    private $em;

    private $category;

    private $accountid;

    /**
     * Values from database
     * @var array
     */
    private $ValuesFromDB = null;

    public function __construct($em)
    {
        $this->em = $em->getRepository('TestCoreBundle:Conf');
    }

    public function setAccountid($accountid) {
        $this->accountid = $accountid;
        $this->loadAll();
    }
    public function loadAll() {
         $DBconf = $this->em->findAllByAccount($this->accountid);

         foreach($DBconf as $key => $conf) {
             $values[$conf->getCategory()][$conf->getKey()] = $conf->getValue();
        }

         $this->ValuesFromDB = $values;

    }

    public function setCategory($category) {
        $this->category = $category;
    }

    public function getCategory($category) {
        return $this->ValuesFromDB[$category];
    }

    public function get($key) {
        return $this->ValuesFromDB[$this->category][$key];
    }

}
代码语言:javascript
复制
services:
    test.account_factory:
        class: test\CoreBundle\Factory\AccountFactory
        arguments: [@security.context]
    test.account:
        class: test\CoreBundle\Entity\Account
        factory_service: test.account_factory
        factory_method: get


    test.configuration:
        class: test\CoreBundle\Service\Configuration
        arguments: [@doctrine.orm.entity_manager]          

    test.conf_factory:
        class: test\CoreBundle\Factory\ConfigurationFactory
        arguments: 
            - @test.account
            - @test.configuration

    test.conf:
        class: Configuration
        factory_service: test.conf_factory
        factory_method: get
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-25 17:57:16

我不确定我是否非常了解您的设计问题,但我想您实际上要做的是根据特定的实体(这里是您的用户)从数据库中检索一些数据。那我就试着和你分享我的看法:

我从你的设计中看到的是,你误解了工厂的概念:在这里,它显然是无用的。

  • 首先,你在这里设计的不是工厂。这似乎更像是您试图使用依赖注入原则,而没有真正掌握它。
  • 其次,我认为当您必须实现这样的设计和实现这些工具时,您必须考虑您实际上是真正的试图做什么,以及您的工具应该做什么。根据我从您的问题中所能理解的,您似乎希望检索一些与您的用户相关的、没有链接到您实体的特定数据。我要问的第一个问题是:在您的业务逻辑中,您的数据为什么没有链接到用户?

然后,我会试图通过思考:我的工具应该做什么来恢复你的工具?你不认为你正在实现的设计太复杂了吗?

如果我理解你想要做好什么,我想你可以通过以下方式使用你的工具:

  • 首先,手动检索您的帐户。不需要为每个帐户注入任何帐户或创建配置实例。我想对于您的更高级别的代码,您可以首先检索您的帐户。
  • 其次,使用一个服务(是的,一个服务,因为一个服务只是一个简单的类,带有带有DI等的额外酷的东西)来检索您需要的数据。

在你的帖子开始时,你的问题实际上是不应该被问到的,因为你不应该在你的课堂上注入一个帐户实例。最后,就像这样:

代码语言:javascript
复制
// I get my account 
$myAccount = $this->getEntityManager()->getRepository('TestCoreBundle:Conf')->find(/* ... */);
$configurationManager = $this->get('configuration_manager');
// The manager permits me to get the "extra stuff" related to this account.
$confStuff = $configurationManager->getConfigurationStuffFromAccount($myAccount);

..。如果我能理解的话,应该是你解决问题的方法。

这条路:

  • 您有一个不依赖于任何存储库或任何帐户的服务configuration_manager。
  • 您可以高效地使用服务和DI。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30273873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档