Are Doctrine2 repositories a good place to save my entities?

后端 未结 3 447
我在风中等你
我在风中等你 2021-01-29 20:20

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 m

3条回答
  •  心在旅途
    2021-01-29 21:06

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

提交回复
热议问题