Print Page | Close Window

Detecting that there are changes that need to be saved

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2122
Printed Date: 28-Mar-2025 at 5:22am


Topic: Detecting that there are changes that need to be saved
Posted By: leeatkinson
Subject: Detecting that there are changes that need to be saved
Date Posted: 03-Sep-2010 at 8:55am
Hi

I have a button that I use to save changes in the entity manager. I want to disable this button when there are no entities to save. However, there doesn't seem to be a HasChangesChanged event, so I've tried EntitytChanged event, and filtering on the Action being EntityAction.Change. However, this seems to say that there are changes when there hasn't been.

entityManager.EntityChanged += (s, e) => { this.HasChanges = (e.Action == EntityAction.Change); };
entityManager.Saved += (s, e) => { this.HasChanges = false; };
However, this doesn't work as it reports that there are changes when there aren't.
Is there a better way to do this?
Lee



Replies:
Posted By: davidklitzke
Date Posted: 03-Sep-2010 at 9:25am
Use the HasChanges method of the EntityManager
 
Determines whether the EntityManager cache has any changes that have not yet been persisted to the backend data source.


Posted By: leeatkinson
Date Posted: 03-Sep-2010 at 9:53am
Sorry, perhaps I wasn't clear - what I want is an event that is raised when HasChanges() has changed.


Posted By: kimj
Date Posted: 03-Sep-2010 at 3:15pm
There isn't a HasChanges event on the EntityManager, but you should be able to use the EntityChanged event for what you want.  I'm surprised that you're seeing EntityAction.Change events raised when there aren't any actual changes - do you have more information on when you see this?  You should, though, be careful in setting your HasChanges flag - right now you'll be setting it on and off more frequently than you want, since this event is raised for quite a few different actions. 
 
Something similar to the following should work, so that you're not setting the flag off for other actions, and optionally also testing the EM for changes if you find that Change actions are fired erroneously.   I should note that Add and Delete activities will fire both the Add/Delete action and the Change action, so checking only for Change should be sufficient.    
 
entityManager.EntityChanged += (s, e) => {
  if (this.HasChanges) return;
  this.HasChanges = e.Action == EntityAction.Change && entityManager.HasChanges();
};



Print Page | Close Window