Hi,
I had a similar question about 2 years ago so wondering if the stance on this topic might have changed.
Would it be possible to expose a list of entities tracked by a document session?
I am using reflection to get that list by accessing DocumentSession.DocumentsByEntity, but I'd prefer not to use such ugly workarounds : )
There might be different scenarios requiring this, I'll list 2:
- Before calling "SaveChanges()" do some operations based on tracked aggregate roots. For example, publish domain events held by the agg. roots, if there are any. Using the sessions before-save event would not work here as sometimes there is no change on the aggregate root but a related entity. There may be more scenarios why the built-in events do not suffice here and why it would be cleaner just to get a list of tracked entities.
- We load an entity by Id. Then another module we can't change tries to load the same entity by a uniquely constrained parameter. To prevent another DB request for the query we can look up the tracked entities if we already loaded the document.