I would like to have a reference between two entities stored in the RavenDB document database. Since this is not a relational db I know that I am supposed to use the Denormalized Reference technique described on RavenDBs documentation. Whilst at first this seems fine, once I start to create a real-world domain ‘hierarchy’ including bidirectional references the effort of keeping all those references up to date feels disproportionate. I feel I may be going wrong somewhere.
Can you explain the best / simplest way to model a reasonably complex domain hierarchy using RavenDB?
Thanks
I am not sure whether this will go far enough to answer your question but here is how I go about creating a Denormalized Reference in RavenDB (this is taken from real code with non-essentials removed for clarity)
Domain
You can see that I have a
Assessmentclass that references aUser. This user reference are managed using theUserReferenceclass below.Denormalized Reference
Note how the reference class also carries the
UserName. This value will not change very often but it may change so we need a way to update theUserNameproperty in theUserReferenceproperty held in theAssessmentclass. To make the change we must first find the correctAssessmentinstances from RavenDB and for that we need an index.Raven Index
This index needs to be invoked whenever a
User‘sUserNamevalue is updated. I have aUserServiceclass that helps me co-ordinate all my User related functions, so that is where I put this code.I reuse this code for other references so it has been abstracted out a little. This may help you create the more complex hierarchies (or perhaps ‘domain graph’ is a better description) you want.
UserService
That is it. And you know, now that I lay it all out I can see what you mean. It is a lot of work just to update a reference. Perhaps the Service code can be made more DRY and reused for different relationship types, but I don’t see how to get away from writing lots of indexes, one per referenced type.