c# - what is asynchronous in MVVM? The Model or ViewModel. Best practise? -
i'm searching best practise communicate asynchronous between layers. i'm using mvvm lite toolkit
currently utilize backgroundworker in model because saw in autogenerated code. not backgroundworker async call.
public void getconfig(action<config, exception> callback) { backgroundworker backgroundworker = new backgroundworker(); backgroundworker.dowork += (backgroundworkersender, backgroundworkerargs) => { seek { backgroundworkerargs.result = appenvironment.instance.config; } grab (exception exception) { backgroundworkerargs.result = null; } }; backgroundworker.runworkercompleted += (backgroundworkersender, backgroundworkerargs) => { if (backgroundworkerargs.result != null) { callback((config) backgroundworkerargs.result, null); } else { /* todo: exceptionhandling */ } }; backgroundworker.runworkerasync(); }
now found asyncdelegatecommand implements asynchronous part in viewmodel.
private icommand _refreshobjectdefinitioncommand; public icommand refreshobjectdefinitioncommand { { homecoming _refreshobjectdefinitioncommand ?? (_refreshobjectdefinitioncommand = new asyncdelegatecommand(delegate { isbusy = true; _dataservice.getobjectdefinition( (xmlobjectdef, errorconfig) => { if (errorconfig != null) { /* todo lenz: exceptionhandling */ return; } objectdefinition = xmlobjectdef; }); _dataservice.getobjectdefinitiontreeview( (treenodes, errorconfig) => { if (errorconfig != null) { /* todo lenz: exceptionhandling */ return; } treenodes = treenodes; }); }, () => _isconnected, o => isbusy = false, exception => isbusy = false)); } }
i'm little confused best practise? i've read lot of articles. somehow different opinions. there provision best compatibility under normal effort maintain?
some nutrient 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
well, fecthing of model , transforming view model async. it, depends on architecture, can done on view model or can utilize controller layer such async load ups , mapping of initialized vm view. backgroundworkers past should utilize task class parallel operations. , of course of study not forget invoke through dispatcher when notifying view on changes vm.
code sample:
task<string>.factory.startnew(() => { string text = getarticletext(); application.current.dispatcher.begininvoke(new action(()=>mytextproperty = text)); });
c# .net wpf mvvm mvvm-light
No comments:
Post a Comment