phpcqrsevent-sourcingprooph

Getting aggregate by other property than ID in Prooph


How simplest get aggregate from aggregate repository without know his ID but knowing of other unique property? For example I have Cart which has ID as AggregateId and ownerId as other property.


Solution

  • In CQRS (assuming you have such a system), when transacting against an aggregate root, you need the state of the aggregate to make the decision in order to preserve the invariants.

    On the read side, the typical pattern is to project/denormalise/index the data as appropriate to facilitate querying as you'll need it.

    So typically, you'll have a projection track each event and index based on the OwnerId to facilitate the querying. If this is only for the purpose of lookup to run some decision processing command, that can be as simple as a map of OwnerId to owned CartIds.


    You haven't provided much context; it depends whether you're trying to build an order history system or a shipping etc. You're much likely to get a good answer to any followup question if you explain more about what you're trying to achieve overall