Ilan Y
Ilan Y

Reputation: 107

Aggregate root references another aggregate root by ID, how to maintain integrity using RavenDB?

Say I have X as an aggregate root, and Y as another aggregate root. Using a NoSql document database, X holds a reference to Y by Y's Id. If Y is deleted (independently outside of X's context), then X holds a reference to a Y that does not exist.

What is a proposed solution to eliminate or solve this problem in DDD?

Upvotes: 2

Views: 269

Answers (3)

Eben Roux
Eben Roux

Reputation: 13246

I do not know if DDD speaks to your problem directly but the proposed "solution" may lie in Domain Events / Messaging. If the related aggregate is in the same bounded context a domain event may suffice else you may need messaging infrastructure to communicate with another bounded context.

What happens when you receive the 'deleted' event is another story and possibly your domain experts can help. As alluded to by @Dmitry S. you may need to denormalize the related aggregate data into a value object so that you have enough information to keep the main aggregate consistent. When processing the 'deleted' event then you may want to set some indicator on your main aggregate or update the data somehow to reflect the deletion.

Upvotes: 2

Dmitry S.
Dmitry S.

Reputation: 8503

A delete operation should have a business meaning. For example, just because someone deleted a product from the inventory collection, does not mean it should be deleted from users invoices.

If there is a real need for a delete. You can always define an index in RavenDB and update the entities containing that aggregate root ID.

Upvotes: 2

Pein
Pein

Reputation: 1246

Why would you ever delete an aggregate? You might want to Expire() it or Suspend(). Deactivate(), Disable(), Ban(), Cancel(), Finish() or Archive(). But what benefit would you get from loosing your data with Delete()?

If you really need that (possibly due to legal purposes) maybe some EvaporaitonService should be created and it would find all related aggregates and whipe all references.

Upvotes: 1

Related Questions