DataWindow and Asynchronous Save

Mar 20, 2012 at 10:11 PM

What's the best practice for implementing asynchronous saving to a domain service from the view model of a DataWindow?  At the time an overrided Save() is called the IEditableObject models have not ended editing.  Should the data model handle its own Saved event or its Save() implementation reach down and end the editing session of all models?

Any advice is appreciated.

Mar 21, 2012 at 8:53 AM
Edited Mar 21, 2012 at 8:54 AM

I usually create two additional booleans (_isSaving and _isSaved).


public override bool Save()
	if (_isSaved)
		return true;
	if (_isSaving)
{ return false; } _isSaving = true; // TODO: Kick of async saving _myService.SaveAsync(() => { _isSaving = false; _isSaved = true; return Save(); }); return false; // do not accept save now }


We might create a standard solution for this in Catel one day (but at the moment, we have too many other features to implement).

Mar 21, 2012 at 2:19 PM

I apologize for not being more clear, but the issue I have is that calling _myService.SaveAsync() within Save() is a problem because at that point all the IEditableObject model objects have not ended editing.  Looking at the Catel source, editing ends immediately after Save() returns.  I can just put this in Close(), though, even though that feels a little inappropriate.  Thanks for the help.

Mar 21, 2012 at 2:37 PM

Although the ending has not edited, the actual values are already in the model, so it shouldn't be a problem. Do you have a repro of this situation where it will not work?

Mar 21, 2012 at 8:09 PM

We are using RIA Services.  The generated DomainContext will throw an exception if SubmitChanges() is called before EndEdit() is called on the model objects.

Mar 21, 2012 at 8:11 PM

Until we come up with a better solution, you will have to commit the model yourself (casting it to IEditableObject and invoke EndEdit).