0

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?

svick
  • 236,525
  • 50
  • 385
  • 514
Eric.M
  • 827
  • 1
  • 14
  • 29
  • 1
    `ExecuteDataAsync()` already exists, it's called `Task.Run()`. – svick May 28 '13 at 13:31
  • Is see, what would you recommend for me while using databases? Thank you! – Eric.M May 28 '13 at 13:33
  • 1
    The first thing you need to think about is: why are you using async-await here? And why are you building so many synchronous-like tasks and sequentially await on them? And **async void** is evil. It should only be used on event handlers or similar. You are not doing any exception handling and, if any occurs, it will bubble up. – Paulo Morgado May 29 '13 at 01:16
  • I see, would you recommend to change the method to return a Task? The idea is to take some data and save it in the database, sometimes It's a this Source comes with a list of articles, so it takes a little while to save in the database. I'm a little lost on how I'm gonna build the method in order to do this. The operations is: take the objects I have instantiated, create the db objects from them then save them in the database. the GetSource method does the puts the data in the objects in the database objects... I don't know if I was clear, but that's the general idea. Thank you! – Eric.M May 29 '13 at 04:26
  • async void can't be awaited. It is defines essentially a fire-and-forget method. So you definitely should return a Task. – Panagiotis Kanavos May 30 '13 at 13:32

0 Answers0