We ran into some odd behavior in our custom Save Interceptor that I've tracked down to some DevForce logic that confuses me. Some background:
We have a custom Save Interceptor that overrides ExecuteSave() and does some logic before and after we call base.ExecuteSave(). Often times we executes queries against the EntityManager. We've noticed that if we execute a query before base.ExecuteSave(), then by the time base.ExecuteSave() finishes, the EntityManager will have its DefaultQueryStrategy set to CacheOnly. That ends up messing with the queries we execute after base.ExecuteSave() since they never make it to the Data Source. We could just force them to be DataSource only but that seems excessive and I'm wondering why the entity manager gets forced to Cache Only in the first place.
Tracing through a bit of the DevForce code, it seems that whenever an entity is added or removed from the Entity Manager during a Save (which happens when I query for an entity), the EntityServerSaveInterceptor sets WorkStateNeedsUpdate to true (which makes sense). But then when UpdateWorkState is called, it ends up forcing the entity manager to Cache Only - which I don't understand (although, I wouldn't be surprised if there is a good reason).
Here is some sample code to illustrate our problem:
public class TestSaveInterceptor : EntityServerSaveInterceptor { protected override bool ExecuteSave() { // These are two entity keys that we know will always exist in our DB. // This is used as an example of one way we execute queries var keyThatExists1 = new EntityKey(typeof(Thing), 1); var keyThatExists2 = new EntityKey(typeof(Thing), 2);
//If it's the 13th, load the first thing (yes, obviously not our actual code) if(DateTime.Today.Day == 13) { var thing1 = keyThatExists1.ToKeyQuery(EntityManager).Execute().Single(); }
//Do the normal save var result = base.ExecuteSave(); //If it is a Friday, load the second thing if(DateTime.Today.DayOfWeek == DayOfWeek.Friday) { var thing2 = keyThatExists2.ToKeyQuery(EntityManager).Execute().Single(); }
//As long as it isn't Friday *and* the 13th, this code will run fine. If only one of the queries runs, // there are no problems. It's just when they both run that the problem comes up since the first query // causes the entity manager to be in CacheOnly which means the second query doesn't return any results // (since thing 2 isn't in our cache) which then breaks the call to 'Single'.
return result; } } |
I'm not sure whether I'm just not supposed to be doing queries during save or maybe this is a bug. Or maybe I should just clear out the Entity Manager's DefaultQueryStrategy after the call to base.ExecuteSave(), or ....
Thanks,
-Stephen