Doctrine2 存储库是保存我的实体的好地方吗?
当我阅读有关存储库的文档时,通常是使用实体和存储库。集合,但以“只读”方式。
从来没有存储库具有诸如 insertUser(User $user)
或 updateUser(User $user)
之类的方法的示例。
然而,当使用 SOA 时,服务不应该与实体管理器一起工作(这是正确的,不是吗?)所以:
- 我的服务应该了解全局实体管理器吗?
- 我的服务是否应该只知道所使用的存储库(比如说 UserRepository 和 ArticleRepository)
从这两个问题,另一个问题,我的服务是否应该明确地 persist()
& flush()
我的实体?
When I read docs about repositories, it is often to work with entities & collection but in a "read-only" manner.
There are never examples where repositories have methods like insertUser(User $user)
or updateUser(User $user)
.
However, when using SOA, Service should not be working with Entity Manager (that's right, isn't it?) so:
- Should my service be aware of the global EntityManager?
- Should my service know only about the used Repositories (let's say, UserRepository & ArticleRepository)
From that both questions, another one, should my service ever explicitly persist()
& flush()
my entities ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,存储库通常仅用于查询。
我是这样做的。 服务层管理持久性。控制器层知道服务层,但不知道模型对象如何持久化,也不知道它们来自哪里。控制器层关心的是要求服务层持久化并返回对象——它不关心它实际上是如何完成的。
服务层本身非常适合了解持久层:实体或文档管理器、存储库等。
下面是一些代码,可以使其更清楚:
Yes, repositories are generally used for queries only.
Here is how I do it. The service layer manages the persistence. The controller layer knows of the service layer, but knows nothing of how the model objects are persisted nor where do they come from. For what the controller layer cares is asking the service layer to persist and return objects — it doesn't care how it's actually done.
The service layer itself is perfectly suitable to know about the the persistence layer: entity or document managers, repositories, etc.
Here's some code to make it clearer:
如果您查看存储库模式http://martinfowler.com/eaaCatalog/repository.html ,
据说存储库使用“类似集合的接口”。
后来,还写到“对象可以添加到存储库中或从存储库中删除,就像它们可以从简单的对象集合中一样”。
我并不是说这是一本圣经,但从概念上讲,看到像可以查询的集合这样的存储库并没有什么问题。
但由于它是一个集合,您可以添加、删除......
事实上,ObjectRepository 应该实现 Doctrine\Common\Collection :)
另一方面,最重要的是不要搞乱读写,正如 CQS 所说。
这也许就是为什么他们不允许直接这样做,以避免滥用和读/写混合。
编辑:我应该谈论
flush
。这不应该在存储库本身中进行,因为它可能会破坏事务一致性。您最好将
flush
调用移至包装整个业务事务逻辑的地方(处理命令 fe 的命令总线?)If you take a look at the repository pattern http://martinfowler.com/eaaCatalog/repository.html ,
it is stated that repositories uses a "collection-like interface".
Later, it is also written " Objects can be added to and removed from the Repository, as they can from a simple collection of objects".
I'm not saying this is a bible, but there is conceptually nothing wrong to see a repository like a collection that you can query.
But as it is a collection, you can add, remove, ...
In fact, the ObjectRepository should implement Doctrine\Common\Collection :)
On the other hand, the most important is not to mess reads and writes, as CQS says.
That's maybe why they didn't allow directly that, to avoid abuses and read/write mix.
EDIT: I should have talked about
flush
. This should not be made in the repository itself, as it might break transactional consistency.You'd better move the
flush
call to something that wraps the whole business transaction logic (a command bus handling a command f.e?)那么,在不使用entityManager 的情况下如何获取存储库呢?毕竟,如果没有连接到数据库,实体就不会神奇地保存,因此您的服务必须以某种方式了解任何类型的连接。
我不了解 SOA 服务,但在我看来,如果您使用
$_em->getRepository()->save($entity)
或根本没有区别$_em->persist($entity)
。另一方面,如果您在存储库中使用刷新,您最终可能会遇到比需要更多的查询,因为您的存储库现在已经了解业务逻辑。我认为有一种方法可以做到这一点“SOA 方式”,但我想它不会将实体保留在存储库中。
Well, how do you get your repository when not using the entityManager? After all, the entities are not going to be saved magically without a connection to the database, so your service must somehow be aware of any kind of connection.
I don't know about the SOA Services, but in my eyes it makes no difference at all if you use
$_em->getRepository()->save($entity)
or$_em->persist($entity)
. On the other hand, if you use flush in your repository, you might end up with way more queries than needed as your repository is now aware of the business logic.I think there is a way to do this "the SOA way", but I guess it's not persisting the entities within the repository.