Does the MVVM pattern assume binding updates are always synchronous?

  Kiến thức lập trình

This question is not specific to any one UI platform, but suppose in a WPF-like UI I have 3 RadioButtons each two-way bound to separate view model properties – bool Value1, bool Value2, and bool Value3 – and grouped under a common group name so that the UI enforces a mutually exclusive relationship. Suppose that Value1 is true by default.

Now suppose in some other part of the code, say in a command action handler, I programmatically set Value2 to true, which updates the bound RadioButton. The UI then also enforces mutual exclusivity, setting the Value1-bound RadioButton to false, and raises a target-triggered source update to the Value1 binding to set its value to false.

We know typically all this happens synchronously and so Value1‘s value will be false as soon as Value2‘s setter completes. However, is this merely an implementation detail, or does MVVM itself presume or even require this to be the case in order for the pattern to work coherently?

For background, the question is motivated by my exploration of building an MVVM-type framework in asynchronous contexts – one concrete example being Blazor where Javascript is always invoked asynchronously from the C# side. Thus in such a framework, the answer to my question would clearly be no – you can’t rely on the UI doing anything prior to the return of the setter and the PropertyChanged raise, and so you can’t assume Value1‘s value is immediately in sync with the UI. In such a case the view model itself would have to enforce the mutual exclusivity and not rely on any behavior innate to a RadioButton.

On the other hand all the major MVVM UIs I know of operate on a single UI thread and so typically the entire control flow from Value2‘s PropertyChanged firing all the way to Value1‘s source update should occur synchronously. The only exception would be when the view model itself is operating on multiple threads and UI updates have to be posted to a dispatcher, which is outside the scope of what I’m concerned with. I therefore wonder if it’s likely that the synchronicity of binding updates is simply baked into the pattern and assumed by most everyone, if not explicitly documented anywhere.

Thus I’m not exactly asking the question for myself – nor am I asking for a specific framework like WPF – but I’m trying to determine whether an MVVM framework that was only capable of operating with asynchronous bindings is viable, or whether indeed the pattern just isn’t compatible with asynchronous bindings due to the strong likelihood of the view and view model going out of sync for nontrivial periods of time.

LEAVE A COMMENT