I'm using a DataContext to insert an object. Currently it's working, but I'm feeling things could be done better. Method is called from UI (Using Caliburn Micro Binding). IsBusy is a property used by the UI (Xceed Extended WPF Toolkit) to display a wait message.
Repository inherits from DataContext (using Entity Code First).
public async void AddTemplate()
{
IsBusy = true;
await Task.Run(() => {
using (var repo = new Repository())
{
var template = new Template() { ... };
try
{
repo.Templates.Add(template);
repo.SaveChanges();
this.TryClose(true);
}
catch (SqlException ex)
{
_util.Log.AddException(Exceptions.DB_EXC, ex.Message);
}
catch (EntityException ex)
{
_util.Log.AddException(Exceptions.EF_EXC, ex.Message);
}
catch (Exception ex)
{
_util.Log.AddException(Exceptions.US_EXC, ex.Message);
}
finally
{
IsBusy = false;
}
}
this.TryClose(false);
});
}
I'm feeling odd about covering using
with the await Task.Run
since I can also await for repo.SaveChangesAsync()
, but then IsBusy will not change (and the Wait message will not be made visible).