Search code examples
design-patternsravendbdocument-database

document db - denormalized references - best practice


Yo - this is not a ravendb issue per se - it's another design question pertaining to document dbs in general.

So I've been using DenormalizedReference quite a bit and it has just occured to me that in a lot of places this makes DDD rather hard.

So, say for instance i have an object that has a child collection:

 List<DenormalizedReference<SomeType>>

This child collection is created when the parent is instanciated with an overloaded constructor accepting a List<SomeOtherType>

now - in the ctor i'd like to create this list from the SomeOtherType collection being passed in - which means i will need to create a new SomeType for each SomeOtherType.

Without passing in the DocumentSession into my domain - which i'm not going to do - this wont work because when the parent AR is saved we lose the properties on each child object except for Id and Name

How is everyone else handling this?


Solution

  • You can't do that. Or rather, you shouldn't do that.

    There are several ways to resolve this, usually using a static gateway to get the session, but the problem is that this approach creates a lazy loaded collection, with all the problems that this have.

    RavenDB was specifically designed to avoid this sort of issues. And you can include items to avoid going back to the server, but you shouldn't try to move between ids and types transparently like that.