dongrui6787 2013-04-15 16:20
浏览 42

参数 - 此服务的最佳实践

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!?"

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:

  • /add/entity
  • addEntityAction
  • 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.

And here comes my mental chaos

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

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

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

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

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)

  • 写回答

1条回答 默认 最新

  • dst3605528 2013-04-16 13:46
    关注

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

    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.

    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).

    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线