Im facing a problem that probably is quite common but i can't find any solution to it. The problem occurs when a user has entities in its cache on the client and another user removes some of those entities (on the server). When the first user then wants to update its data the removed entities is not removed from the cache. You could solve it by clearing the cache each time you update but then you also looses all non-saved changes. Am I missing something obvious?
Example:
Model:
public class Order
{
[Key]
public int Id { get; set; }
public ICollection<OrderDetail> OrderDetails { get; set; }
}
public class OrderDetail
{
[Key]
public int Id { get; set; }
[ForeignKey("Order")]
public int Order_Id { get; set; }
public virtual Order Order { get; set; }
}
Client code:
function getOrder(orderId, orderObservable) {
var query = EntityQuery.from("Orders")
.where("orderId", "==", orderId)
.expand("orderDetails");
return manager.executeQuery(query).then(querySucceeded).fail(queryFailed);
function querySucceeded(data) {
var order = data.results[0];
// NOTE: the removed orderdetail is still there 'order.orderDetails'
orderObservable(order);
}
}
Step-by-step scenario:
In the breeze-docs, under the headline "Important Caveats about cache clearing", there is a solution that removes cached entities by comparing the cache and the result from the query and detaches the missing entities in the result. http://www.breezejs.com/documentation/entitymanager-and-caching But that doesn't work in this case. I'm guessing it has to do with the fact that orderdetails is related to the order and that it is "picked up" from the cache before it is passed to the success-callback.
All help is appreciated!
The problem you are facing isn't with Breeze, but with design in general. There are a couple of options that come to mind -
Use SignalR to notify your web application that a change has occurred, detach any removed entities from the cache.
Use an archived or deleted flag instead of removing the entities from the database.
Both have their advantages and disadvantages.
With SignalR you will need to get the pipe work in place for notifications and set up a specific work flow around removing deleted entities
manager.detachEntity(entityToDetach);
The reason you would detach instead of deleting is because if you set it to deleted then your Breeze entity manager still thinks you need to persist that change to the database.
If you use a flag then you could simply set your business logic to ignore entities that are flagged as deleted or archived and when you query the DB it will return the change to that entity and stop showing it
myEntity().archived(true);
The problem here would be if your entity doesn't match your query it would never return the updated entity to let the client know that it was archived or deleted. The other caveat is that you would have information laying around in your database that isn't active anymore.
Depending on which type of application and requirements you have you should make one of these choices, or come up with another. Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With