Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reactive Extensions (Rx) + MVVM =?

One of the main examples being used to explain the power of Reactive Extensions (Rx) is combining existing mouse events into a new 'event' representing deltas during mouse drag:

var mouseMoves = from mm in mainCanvas.GetMouseMove()                  let location = mm.EventArgs.GetPosition(mainCanvas)                  select new { location.X, location.Y};  var mouseDiffs = mouseMoves     .Skip(1)     .Zip(mouseMoves, (l, r) => new {X1 = l.X, Y1 = l.Y, X2 = r.X, Y2 = r.Y});  var mouseDrag = from _  in mainCanvas.GetMouseLeftButtonDown()                 from md in mouseDiffs.Until(                     mainCanvas.GetMouseLeftButtonUp())                 select md; 

Source: Matthew Podwysocki's Introduction to the Reactive Framework series.

In MVVM I generally strive to keep my .xaml.cs file as empty as possible and one way of hooking up events from the view with commands in the viewmodel purely in markup is using a behavior:

<Button Content="Click Me">     <Behaviors:Events.Commands>         <Behaviors:EventCommandCollection>             <Behaviors:EventCommand CommandName="MouseEnterCommand" EventName="MouseEnter" />             <Behaviors:EventCommand CommandName="MouseLeaveCommand" EventName="MouseLeave" />             <Behaviors:EventCommand CommandName="ClickCommand" EventName="Click" />         </Behaviors:EventCommandCollection>     </Behaviors:Events.Commands> </Button> 

Source: Brian Genisio.

The Reactive Framework seems to be more geared towards the traditional MVC pattern where a controller knows the view and can reference its events directly.

But, I want to both have my cake and eat it!

How would you combine these two patterns?

like image 830
Jesper Larsen-Ledet Avatar asked Nov 19 '09 13:11

Jesper Larsen-Ledet


People also ask

Is Mvvm reactive?

Additionally the disadvantage of MVVM architecture, in my view, is that if an action is happening in the View, we directly call a method in the ViewModel to notify it, which means in the case of actions the MVVM isn't reactive, but we want to fully support reactive idea by RMVVM.

What is the purpose of reactive extension?

ReactiveX (also known as Reactive Extensions) is a software library originally created by Microsoft that allows imperative programming languages to operate on sequences of data regardless of whether the data is synchronous or asynchronous.

What is reactive C#?

In reactive programming, correlating events means correlating multiple observable messages into a single message that is the result of two or more original messages.


1 Answers

I've written a framework that represents my explorations in this question called ReactiveUI

It implements both an Observable ICommand, as well as ViewModel objects who signal changes via an IObservable, as well as the ability to "assign" an IObservable to a property, who will then fire INotifyPropertyChange whenever its IObservable changes. It also encapsulates a lot of common patterns, like having an ICommand who runs a Task in the background, then marshalls the result back to the UI.

I have absolutely zero documentation up right now, but I'll be working on adding that information over the coming days, as well as a sample application I've coded up

UPDATE: I now have quite a lot of documentation up, check out http://www.reactiveui.net

like image 200
Ana Betts Avatar answered Sep 16 '22 14:09

Ana Betts