Readmodels for collections on ARs
I have tried to make reference to the UserNickname example from the documentation to clarify how I should create a ReadModel for a collection of properties on an AggregateRoot, but I am none the wiser.
I can't see how the Id returned from the UserNicknameReadModelLocator winds up with a string NickName on the UserNicknameReadModel class..? How is this configured? I am assuming that UserId is the AggregateRoot.Id and would (for MSSQL) be decorated with MsSqlReadModelIdentityColumn. But if we're storing multiple rows per Aggregate then we need a composite key, surely?
Is NickName supposed to be a ValueObject in order to live in a collection on the AggregateRoot? I am not doing this, so perhaps that's what's missing.
I have searched the code for an example and can't find one. Does anyone have a working example of a ReadModel for collections on an AggregateRoot being built up into multiple ReadModel rows?
Your locator for the nicknames should return a unique id for each nickname, this will result in a readmodel for each nickname. Each readmodel will then be applied with the event. So depending on the event being about 1 nickname or maybe a list of nicknames (nicknamesAddedEvent or so) the same event will be applied to 1 or multiple readmodels. In the multiple case you have to select the correct nickname based on the the readmodel the event Is currently applied to. This is can be found in the injected context for these cases.
Most of the time when you query the readmodels you might want to find all for the userid so that should then be part of the readmodel and is the aggregateroot Id.
But the identidycolumn propery should be the unername Id.
So in short : For 1..n relation's the n relation should have unique id's (maybe base them on identity). Locator returns 1 or more id's from an event. The event is applied to all readmodels created from the returned id's.
There is a example in the integration tests implementation.
Look for class thingymessagelocator. And look MsSqlThingyMessageReadModel to see how a concrete implementation could look like. In the example you see a event for a single message and one that results in multiple message readmodels from 1 event
@wgtmpeters I have got this working now with your input. Thanks for the clarification, much appreciated!
Closing this as solved