TheDeveloper
TheDeveloper

Reputation: 1217

Xamarin Forms: Is Reactive Extensions (Rx) a framework that replaces MVVM pattern?

I have a question regarding Xamarin forms. Is reactive extensions just an extension to existing .net library? does all mvvm frameworks(FreshMvvm, MvvmCross,MvvmLight) support it? Or is it a new mvvm framework?

Upvotes: 0

Views: 1289

Answers (1)

Enigmativity
Enigmativity

Reputation: 117029

I can't answer if all of the MVVM frameworks support Rx, but I can definitely say that Rx is not a replacement for MVVM and you certainly wouldn't call it an MVVM framework.

Rx is a framework for composing collections of values that get pushed (like events do). It certainly can be used in an MVVM framework, again, like events can be used.

Here's a small example of using Rx to consume an event:

void Main()
{
    var foo = new Foo();
    var bars = Observable.FromEventPattern<int>(h => foo.Bar += h, h => foo.Bar -= h);
    var subsciption = bars.Subscribe(ep => Console.WriteLine(ep.EventArgs));
    foo.OnBar(42);
}

public class Foo
{
    public event EventHandler<int> Bar;
    public void OnBar(int i)
    {
        this.Bar?.Invoke(this, i);
    }
}

This will write 42 to the console.

But I could do this:

    var query =
        from b in bars
        where b.EventArgs > 21
        select b.EventArgs * 2;

    var subsciption = query.Subscribe(i => Console.WriteLine(i));

    foo.OnBar(42);
    foo.OnBar(7);
    foo.OnBar(20);
    foo.OnBar(21);
    foo.OnBar(22);

And now I have a more complex query that will write only 84 && 44 to the console.

This type of thing can be used in MVVM frameworks to make them more powerful.

And excellent example of an MVVM framework that makes strong use of Rx is http://reactiveui.net/. Give it a go.

Upvotes: 3

Related Questions