Note that there are some explanatory texts on larger screens.

plurals
  1. POHow would I organize these calls using Reactive Extensions (Rx) in Silverlight?
    text
    copied!<p>I have some calls that must execute sequentially. Consider an IService that has a Query and a Load method. The Query gives a list of widgets, and the load provides a "default" widget. Hence, my service looks like this.</p> <pre><code>void IService.Query(Action&lt;IEnumerable&lt;Widget&gt;,Exception&gt; callback); void IService.Load(Action&lt;Widget,Exception&gt; callback); </code></pre> <p>With that in mind, here is a rough sketch of the view model:</p> <pre><code>public class ViewModel : BaseViewModel { public ViewModel() { Widgets = new ObservableCollection&lt;Widget&gt;(); WidgetService.Query((widgets,exception) =&gt; { if (exception != null) { throw exception; } Widgets.Clear(); foreach(var widget in widgets) { Widgets.Add(widget); } WidgetService.Load((defaultWidget,ex) =&gt; { if (ex != null) { throw ex; } if (defaultWidget != null) { CurrentWidget = defaultWidget; } } }); } public IService WidgetService { get; set; } // assume this is wired up public ObservableCollection&lt;Widget&gt; Widgets { get; private set; } private Widget _currentWidget; public Widget CurrentWidget { get { return _currentWidget; } set { _currentWidget = value; RaisePropertyChanged(()=&gt;CurrentWidget); } } } </code></pre> <p>What I'd like to do is simplify the sequential workflow of calling query and then the default. Perhaps the best way to do this is nested with lambda expressions as I've shown, but I figured there may be a more elegant way with Rx. I don't want to use Rx for the sake of Rx, but if it can allow me to organize the logic above so it is easier to read/maintain in the method, I'll take advantage of it. Ideally, something like: </p> <pre><code>Observable.Create( ()=&gt;firstAction(), ()=&gt;secondAction()) .Subscribe(action=&gt;action(),error=&gt;{ throw error; }); </code></pre> <p>With the power threading library, I'd do something like: </p> <pre><code>Service.Query(list=&gt;{result=list}; yield return 1; ProcessList(result); Service.Query(widget=&gt;{defaultWidget=widget}; yield return 1; CurrentWidget = defaultWidget; </code></pre> <p>That makes it far more evident that the workflow is sequential and eliminates nesting (the yields are part of the async enumerator and are boundaries that block until the results come back).</p> <p>Anything similar would make sense to me. </p> <p>So the essence of the question: am I trying to fit a square peg into a round hole, or is there a way to redefine the nested asynchronous calls using Rx? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload