I'm trying to search some article with best practices on using async, await and Tasks with local databases on Windows Phone 8, but I can only find examples using WebClient or HttpClient.
I wonder whether what i'm trying to do is the best approach or there is a better way of doing this. I have a method in my viewmodelbase that is like this:
protected virtual Task<T> ExecuteDataAsync<T>(ExecuteDataAsyncAction<T> action)
{
var task = new Task<T>(() => { return action(); });
task.Start();
return task;
}
then, in my savedata, that is called by and ICommand, I have something like this:
private async void SaveData()
{
if (!ValidateBeforeSave() || IsBusy)
return;
IsBusy = true;
using (var db = new AppDataContext())
{
if (db.Sources.Any(o => o.Name == SourceTitle))
{
messageBoxService.Show(AppResources.MSG_ThereIsSourceSameTitle);
return;
}
//the method in the viewModelBase with some operation
var source = await ExecuteDataAsync<Source>(() =>
{
var source = db.Source.FirstOrDefault();
return GetSource(source, false);
});
db.Sources.InsertOnSubmit(source);
//the method in the viewModelBase with some operation
var source = await ExecuteDataAsync<Source>(() =>
{
db.SubmitChanges();
});
}
IsBusy = false;
}
I'm not exactly sure if this is the best approach for operations that don't involve a WebClient. Also, I could write async methods in my AppDataContext or something like this, but I don't know if it's gonna work...
How would you recommend me working with database operations and other processor intensive operations that doesn't rely on network?