I am looking for best practice for asynchronous communication between layers. I am using mvvm light toolkit
I am currently using a background worker in a model because I saw this in auto-generated code. Not with the background, but with an asynchronous call.
public void GetConfig(Action<Config, Exception> callback)
{
BackgroundWorker backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += (backgroundWorkerSender, backgroundWorkerArgs) =>
{
try
{
backgroundWorkerArgs.Result = AppEnvironment.Instance.Config;
}
catch (Exception exception)
{
backgroundWorkerArgs.Result = null;
}
};
backgroundWorker.RunWorkerCompleted += (backgroundWorkerSender, backgroundWorkerArgs) =>
{
if (backgroundWorkerArgs.Result != null)
{
callback((Config) backgroundWorkerArgs.Result, null);
}
else
{
}
};
backgroundWorker.RunWorkerAsync();
}
Now I have found AsyncDelegateCommand , which implements the asynchronous part in the ViewModel.
private ICommand _refreshObjectDefinitionCommand;
public ICommand RefreshObjectDefinitionCommand
{
get
{
return _refreshObjectDefinitionCommand
?? (_refreshObjectDefinitionCommand = new AsyncDelegateCommand(delegate
{
IsBusy = true;
_dataService.GetObjectDefinition(
(xmlObjectDef, errorConfig) =>
{
if (errorConfig != null)
{
return;
}
ObjectDefinition = xmlObjectDef;
});
_dataService.GetObjectDefinitionTreeView(
(treenodes, errorConfig) =>
{
if (errorConfig != null)
{
return;
}
TreeNodes = treenodes;
});
},
() => _isConnected, o => IsBusy = false, exception => IsBusy = false));
}
}
Am I a little confused by best practice? I read a lot of articles. But for some reason they always have different opinions. Is there any condition for better compatibility with normal maintenance efforts?
Some food for thought
Model:
http://csharperimage.jeremylikness.com/2009/12/simplifying-asynchronous-calls-in.html
http://www.dzone.com/articles/mvvmlight-and-async
ViewModel
http://www.codeproject.com/Articles/123183/Asynchronus-MVVM-Stop-the-Dreaded-Dead-GUI-Problem
http://www.codeproject.com/Articles/441752/Async-MVVM-Modern-UI