参数 - 此服务的最佳实践

时间:2021-09-03 20:07:59

After reading a lot about Symfony2 DIC and Semantic Configuration, I've asked myself: "So, now how do have I to procede for reach what I'm trying to reach!?"

在阅读了很多关于Symfony2 DIC和语义配置的内容后,我问自己:“那么,现在我该如何处理达到我想要达到的目标呢?”

Scenario

Imagine you want to create a service that is pretty like a "admin dashboard dispatcher".
This means that I have some entities that I want to do CRUD on. What I don't want to do is a combination of route->action->logic for every entity in my project; that's because, in that way, I'll have to write for each object something like:

想象一下,您想要创建一个非常像“管理仪表板调度程序”的服务。这意味着我有一些我想要做CRUD的实体。我不想做的是为我的项目中的每个实体组合route-> action->逻辑;那是因为,以这种方式,我将不得不为每个对象写一些像:

  • /add/entity
  • /添加/实体
  • addEntityAction
  • addEntityAction
  • entityLogic
  • entityLogic

where "entity", of course, is the name of entity in object.

其中“实体”当然是对象中实体的名称。

I'm building up a service (like a dispatcher) that will respond on action (add-delete-edit) but that is parameterized: I'll write onto address bar something like /add/entityA or /add/entityB and that routes will lead me at the same action (of course) were I recall my service with entity name.
At this point, my service, should be smart to know what is EntityClass, FormTypeClass and template file for the entity in object.

我正在构建一个服务(如调度程序),它将响应操作(add-delete-edit)但参数化:我会写入地址栏,如/ add / entityA或/ add / entityB和那些路由我会以同样的行动引导我(当然)我记得我的服务是实体名称。此时,我的服务应该很聪明,知道对象中实体的EntityClass,FormTypeClass和模板文件是什么。

And here comes my mental chaos

这就是我的精神混乱

I have to do something preliminaries configurations like following php array (this is only an example)

我必须做一些预备配置,如下面的php数组(这只是一个例子)

array(
'EntityA'=>array(
  'class'=>'BundleName/Entity/EntityA',
  'form'=>'BundleName/Form/entityAType',
  'template'=>'EntityATemplate'),
'EntityB'=>array(
  'class'=>'BundleName/Entity/EntityB',
  'form'=>'BundleName/Form/entityBType',
  'template'=>'EntityBTemplate'),
[...]
);

or something equivalent.

或类似的东西。

BUT ....

What is the best practice, for this scenario, taking into account that only me and my team will use this bundle (so isn't a third-part bundle)?

对于这种情况,考虑到只有我和我的团队才会使用此捆绑包(因此不是第三部分捆绑包),最佳做法是什么?

First solution

Use a parameters.php file were I define some constant or static members

使用一个parameters.php文件,我定义了一些常量或静态成员

Second solution

Use a map (like previous array) directly into my service

使用地图(如以前的数组)直接进入我的服务

Third solution

Use semantic configuration for my bundle, defining an Extension class and so on

对我的bundle使用语义配置,定义Extension类等等

If I have to be honest, I can't say what is better. Obviously first one and third one are better that second one.
First solution broke up symfony2 philosophy but seems the only way because the second one, seems to be useful only if I have to trigger up creation of some services (in cascad fashion) that are handled directly my DIC, or for have configuration hierarchy, or for merge more that one configuration file, or (maybe the most importat for third party bundle) for make some kind of validation on config. file

如果我必须诚实,我不能说什么更好。显然,第一个和第三个比第二个更好。第一个解决方案打破了symfony2的理念,但似乎是唯一的方法因为第二个,只有当我必须触发一些直接处理我的DIC的某些服务(以cascad方式)创建或者具有配置层次结构时才有用,或者合并更多的一个配置文件,或(可能是第三方捆绑的最重要),以便在配置上进行某种验证。文件

What's your opinion? Do you see some other methods for do this, or do you simply prefer third solution? I want to know your opinions.

你怎么看?您是否看到其他方法可以做到这一点,或者您只是更喜欢第三种解决方案?我想知道你的意见。

Edit: Obviously I can use builder injection or setter injection, but I consider it into "DIC" section (so, third solution)

编辑:显然我可以使用构建器注入或setter注入,但我认为它进入“DIC”部分(所以,第三个解决方案)

1 个解决方案

#1


0  

If you want to do it the Symfony way, I'd suggest using a configuration (if I understood it correctly):

如果你想以Symfony的方式做,我建议使用一个配置(如果我理解正确):

acme_foo:
    entities:
        FooEntity:
            class_type: Acme\FooBundle\Entity\Foo
            form_type: Acme\FooBundle\Form\Type\FooType
            template: AcmeFooBundle:Entity:foo.html.twig
        BarEntity:
            class_type: Acme\FooBundle\Entity\Bar
            form_type: Acme\FooBundle\Form\Type\BarType
            template: AcmeFooBundle:Entity:bar.html.twig

Using the the AcmeFooExtension class you can set the container parameter which contains the configuration. You can then inject this into your service (which is an event listener). When /add/Foo is called you can dispatch a acme_foo.entity_add event which is consumed by the listener.

使用AcmeFooExtension类可以设置包含配置的容器参数。然后,您可以将此注入您的服务(这是一个事件监听器)。调用/ add / Foo时,您可以调度侦听器使用的acme_foo.entity_add事件。

The thing you should remember is that everything you can put into a config.php or parameters.php file can be put into the app configuration (config.yml).

您应该记住的是,您可以将所有可放入config.php或parameters.php文件的内容放入app配置(config.yml)。

#1


0  

If you want to do it the Symfony way, I'd suggest using a configuration (if I understood it correctly):

如果你想以Symfony的方式做,我建议使用一个配置(如果我理解正确):

acme_foo:
    entities:
        FooEntity:
            class_type: Acme\FooBundle\Entity\Foo
            form_type: Acme\FooBundle\Form\Type\FooType
            template: AcmeFooBundle:Entity:foo.html.twig
        BarEntity:
            class_type: Acme\FooBundle\Entity\Bar
            form_type: Acme\FooBundle\Form\Type\BarType
            template: AcmeFooBundle:Entity:bar.html.twig

Using the the AcmeFooExtension class you can set the container parameter which contains the configuration. You can then inject this into your service (which is an event listener). When /add/Foo is called you can dispatch a acme_foo.entity_add event which is consumed by the listener.

使用AcmeFooExtension类可以设置包含配置的容器参数。然后,您可以将此注入您的服务(这是一个事件监听器)。调用/ add / Foo时,您可以调度侦听器使用的acme_foo.entity_add事件。

The thing you should remember is that everything you can put into a config.php or parameters.php file can be put into the app configuration (config.yml).

您应该记住的是,您可以将所有可放入config.php或parameters.php文件的内容放入app配置(config.yml)。