将symfony容器放入存储库或实体是一个好的或坏的做法?(Put a symfony container inside a repository or an entity is a good or

编程入门 行业动态 更新时间:2024-10-26 00:18:20
将symfony容器放入存储库或实体是一个好的或坏的做法?(Put a symfony container inside a repository or an entity is a good or bad practice?)

我见过大量使用doctrine的项目,其中一些项目将容器或其他服务注入到存储库中。 我在这里也看到了一些关于存储库中容器的问题:

在实体存储库中注入容器 依赖注入 ...

我只有将容器o其他服务添加到存储库或实体时才遇到问题。 我认为可能会有一些副作用。 例如,如果某些代码依赖于特定上下文中不可用的服务,则无法重用某些代码。 例如,如果我的存储库依赖于Symfony\Component\HttpFoundation\Request则它不能在命令中使用。

可以更好地解决创建服务而不是耦合存储库的问题吗?

I've seen a huge amount of projects that uses doctrine and some of them inject the container or other services inside a repository. I've seen also some questions here that talk about container inside a repository:

inject container in entity repository dependency injection ...

I got only problem adding container o other services to a repository or an entity. I think there could be some side effects. For example I cannot reuse some code if it depends on a service that is not available in a particular context. For example if my repository depends on Symfony\Component\HttpFoundation\Request it cannot be used inside a command.

Can be a better solution the creation of a service instead of a coupled repository?

最满意答案

有一些如何创建实体管理器(或存储库)的示例,相关内容如FOSUserBundle中所述,其中doctrine实体管理器已作为对象管理器注入到依赖注入中。

if ('custom' !== $config['db_driver']) { if (isset(self::$doctrineDrivers[$config['db_driver']])) { $loader->load('doctrine.xml'); $container->setAlias('fos_user.doctrine_registry', new Alias(self::$doctrineDrivers[$config['db_driver']]['registry'], false)); } else { $loader->load(sprintf('%s.xml', $config['db_driver'])); } $container->setParameter($this->getAlias().'.backend_type_'.$config['db_driver'], true); } if (isset(self::$doctrineDrivers[$config['db_driver']])) { $definition = $container->getDefinition('fos_user.object_manager'); $definition->setFactory(array(new Reference('fos_user.doctrine_registry'), 'getManager')); }

在这种情况下,UserManager而不是具有相同角色的存储库。

在此示例中,所有服务都已初始化并直接在依赖项注入中注入

在bundle构造/或“编译”(依赖注入)中直接调用(私有)服务而不是在服务内部集合容器更强大

There are some example of how to create Entity managers (or repository), relevant is as described in FOSUserBundle where doctrine Entity manager has been injected as object manager inside the Dependency injection.

if ('custom' !== $config['db_driver']) { if (isset(self::$doctrineDrivers[$config['db_driver']])) { $loader->load('doctrine.xml'); $container->setAlias('fos_user.doctrine_registry', new Alias(self::$doctrineDrivers[$config['db_driver']]['registry'], false)); } else { $loader->load(sprintf('%s.xml', $config['db_driver'])); } $container->setParameter($this->getAlias().'.backend_type_'.$config['db_driver'], true); } if (isset(self::$doctrineDrivers[$config['db_driver']])) { $definition = $container->getDefinition('fos_user.object_manager'); $definition->setFactory(array(new Reference('fos_user.doctrine_registry'), 'getManager')); }

In this case there are UserManager instead of repository that have the same role.

In this example all services has been initialized and injected directly inside the dependency injection

It is more powerful to call (private) services directly inside the bundle construction / or "compilation" (the dependency injection) instead of colling container inside services

更多推荐

本文发布于:2023-07-21 09:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1209003.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是一个   容器   实体   放入   做法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!