If I have a domain class with an instance method Delete()
. What is the best way to ensure that any leftover references to an instance that has "deleted itself" are subsequently unusable?
This question is very similar to mine, but I think the OP is more broadly concerned with how the API as a whole should be notified of deletions that occur elsewhere and react to such deletions. Any mention of how the deleted instances should behave is merely peripheral.
For instance, one bullet point says:
If my wrapper object finds out that its unerlying [sic] storage has been deleted (e.g. because of access to an uncached property returned an error), should it now mark itself as Deleted (so that all access to properties and methods return some sort of ObjectDeletedException) or should it just clear all caches and allow future calls to fail naturally.
I'm more concerned with how to implement the failure that should occur when properties or methods of the deleted instance are used. I can put a gateway check inside each property and method that throws an exception (as stated in the quote above), but that would create extremely brittle code since then every future developer -- including me -- would have to remember to add that check when creating new properties or methods.
Furthermore, I don't feel that such checks would be enough, as the mere presence of an object reference could be meaningful, regardless of its underlying properties or methods. For example, a client of my API may hold references to these instances they believe exist, and if one has deleted itself, that instance should no longer be valid and should cause all sorts of hell. Maybe it could just raise a "NotifyDeleted" event and leave it up to the client to respond, but I'm not sure if that's sufficient.