问题
We have a breeze client solution in which we show parent entities with lists of their children. We do hard deletes on some child entities. Now when the user is the one doing the deletes, there is no problem, but when someone else does, there seems to be no way to invalidate the children already loaded in cache. We do a new query with the parent and expanding to children, but breeze attaches all the other children it has already heard of, even if the database did not return them.
My question: shouldn't breeze realize we are loading through expand and thus completely remove all children from cache before loading back the results from the db? How else can we accomplish this if that is not the case?
Thank you
回答1:
Yup, that's a really good point.
Deletion is simply a horrible complication to every data management effort. This is true no matter whether you use Breeze or not. It just causes heartache up and down the line. Which is why I recommend soft deletes instead of hard deletes.
But you don't care what I think ... so I will continue.
Let me be straight about this. There is no easy way for you to implement a cache cleanup scheme properly. I'm going to describe how we might do it (with some details neglected I'm sure) and you'll see why it is difficult and, in perverse cases, fruitless.
Of course the most efficient, brute force approach is to blow away the cache before querying. You might as well not have caching if you do that but I thought I'd mention it.
The "Detached" entity problem
Before I continue, remember the technique I just mentioned and indeed all possible solutions are useless if your UI (or anything else) is holding references to the entities that you want to remove.
Oh, you'll remove them from cache alright. But whatever is holding references to them now will continue to have a reference to an entity object which is in a "Detached" state - a ghost. Making sure that doesn't happen is your responsibility; Breeze can't know and couldn't do anything about it if it did know.
Second attempt
A second, less blunt approach (suggested by Jay) is to
- apply the query to the cache first
- iterate over the results and for each one
- detach every child entity along the "expand" paths.
- detach that top level entity
Now when the query succeeds, you have a clear road for it to fill the cache.
Here is a simple example of the code as it relates to a query of TodoLists and their TodoItems:
var query = breeze.EntityQuery.from('TodoLists').expand('TodoItems');
var inCache = manager.executeQueryLocally(query);
inCache.slice().forEach(function(e) {
inCache = inCache.concat(e.TodoItems);
});
inCache.slice().forEach(function(e) {
manager.detachEntity(e);
});
There are at least four problems with this approach:
Every queried entity is a ghost. If your UI is displaying any of the queried entities, it will be displaying ghosts. This is true even when the entity was not touched on the server at all (99% of the time). Too bad. You have to repaint the entire page.
You may be able to do that. But in many respects this technique is almost as impractical as the first. It means that ever view is in a potentially invalid state after any query takes place anywhere.
Detaching an entity has side-effects. All other entities that depend on the one you detached are instantly (a) changed and (b) orphaned. There is no easy recovery from this, as explained in the "orphans" section below.
This technique wipes out all pending changes among the entities that you are querying. We'll see how to deal with that shortly.
If the query fails for some reason (lost connection?), you've got nothing to show. Unless you remember what you removed ... in which case you could put those entities back in cache if the query fails.
Why mention a technique that may have limited practical value? Because it is a step along the way to approach #3 that could work
Attempt #3 - this might actually work
The approach I'm about to describe is often referred to as "Mark and Sweep".
Run the query locally and calculate the
inCache
list of entities as just described. This time, do not remove those entities from cache. We WILL remove the entities that remain in this list after the query succeeds ... but not just yet.If the query's
MergeOption
is "PreserveChanges" (which it is by default), remove every entity from theinCache
list (not from the manager's cache!) that has pending changes. We do this because such entities must stay in cache no matter what the state of the entity on the server. That's what "PreserveChanges" means.We could have done this in our second approach to avoid removing entities with unsaved changes.
Subscribe to the
EntityManager.entityChanged
event. In your handler, remove the "entity that changed" from theinCache
list because the fact that this entity was returned by the query and merged into the cache tells you it still exists on the server. Here is some code for that:var handlerId = manager.entityChanged.subscribe(trackQueryResults); function trackQueryResults(changeArgs) { var action = changeArgs.entityAction; if (action === breeze.EntityAction.AttachOnQuery || action === breeze.EntityAction.MergeOnQuery) { var ix = inCache.indexOf(changeArgs.entity); if (ix > -1) { inCache.splice(ix, 1); } } }
If the query fails, forget all of this
If the query succeeds
unsubscribe:
manager.entityChanged.unsubscribe(handlerId);
subscribe with orphan detection handler
var handlerId = manager.entityChanged.subscribe(orphanDetector); function orphanDetector(changeArgs) { var action = changeArgs.entityAction; if (action === breeze.EntityAction.PropertyChange) { var orphan = changeArgs.entity; // do something about this orphan } }
detach every entity that remains in the
inCache
list.inCache.slice().forEach(function(e) { manager.detachEntity(e); });
unsubscribe the orphan detection handler
Orphan Detector?
Detaching an entity can have side-effects. Suppose we have Products
and every product has a Color
. Some other user hates "red". She deletes some of the red products and changes the rest to "blue". Then she deletes the "red" Color
.
You know nothing about this and innocently re-query the Colors
. The "red" color is gone and your cleanup process detaches it from cache. Instantly every Product
in cache is modified. Breeze doesn't know what the new Color
should be so it sets the FK, Product.colorId
, to zero for every formerly "red" product.
There is no Color
with id=0 so all of these products are in an invalid state (violating referential integrity constraint). They have no Color
parent. They are orphans.
Two questions: how do you know this happened to you and what do your do?
Detection Breeze updates the affected products when you detach the "red" color.
You could listen for a PropertyChanged
event raised during the detach process. That's what I did in my code sample. In theory (and I think "in fact"), the only thing that could trigger the PropertyChanged
event during the detach process is the "orphan" side-effect.
What do you do?
- leave the orphan in an invalid, modified state?
- revert to the equally invalid former
colorId
for the deleted "red" color? - refresh the orphan to get its new color state (or discover that it was deleted)?
There is no good answer. You have your pick of evils with the first two options. I'd probably go with the second as it seems least disruptive. This would leave the products in "Unchanged" state, pointing to a non-existent Color
.
It's not much worse then when you query for the latest products and one of them refers to a new
Color
("banana") that you don't have in cache.
The "refresh" option seems technically the best. It is unwieldy. It could easily cascade into a long chain of asynchronous queries that could take a long time to finish.
The perfect solution escapes our grasp.
What about the ghosts?
Oh right ... your UI could still be displaying the (fewer) entities that you detached because you believe they were deleted on the server. You've got to remove these "ghosts" from the UI.
I'm sure you can figure out how to remove them. But you have to learn what they are first.
You could iterate over every entity that you are displaying and see if it is in a "Detached" state. YUCK!
Better I think if the cleanup mechanism published a (custom?) event with the list of entities you detached during cleanup ... and that list is inCache
. Your subscriber(s) then know which entities have to be removed from the display ... and can respond appropriately.
Whew! I'm sure I've forgotten something. But now you understand the dimensions of the problem.
What about server notification?
That has real possibilities. If you can arrange for the server to notify the client when any entity has been deleted, that information can be shared across your UI and you can take steps to remove the deadwood.
回答2:
It's a valid point but for now we don't ever remove entities from the local cache as a result of a query. But.. this is a reasonable request, so please add this to the breeze User Voice. https://breezejs.uservoice.com/forums/173093-breeze-feature-suggestions
In the meantime, you can always create a method that removes the related entities from the cache before the query executes and have the query (with expand) add them back.
来源:https://stackoverflow.com/questions/22182101/breeze-when-child-entities-have-been-deleted-by-someone-else-they-still-appear