What is Reactive Programming (in a nutshell)?

Reactive programming is a programming paradigm oriented around data flows and the propagation of change. This means that it should be possible to express static or dynamic data flows with ease in the programming language used, and that the underlying execution model will automatically spread changes through the data flow.

 

Why is Reactive Programming important for me?

Many of the most complex problems that we face in the development of our applications are data flows, or from now on what we call “sequences”.

Our sequences are like pipes that are emitting elements that we can transform, combine and mix in a way similar with LINQ. This allows changes to be expressed naturally and in a way that is very reminiscent of functional programming, with the advantages that this entails, especially immutability.

This is of enormous importance when it comes to addressing problems such as the management of data flows or events that depend on time. It´s therefore less of a headache, or at least, it isn’t so much of one..

Can you give me an example?

Below, you have the following example to help get your head around things:

 

VIEW CODE

var urls = new List

{

@”http:\\www.amazon.com”,

@”http:\\www.bing.com”,

@”http:\\www.google.com”,

@”http:\\www.twitter.com”,

@”http:\\www.google.com”

};

var client = new HttpClient();

var contents = urls

.ToObservable()

.SelectMany(uri => client.GetStringAsync(new Uri(uri, UriKind.Absolute)));

contents.Subscribe(Console.WriteLine);

 

What does this code do?

“Using the ToObservable method, it takes a list of URLs and converts it into a sequence. Once you’ve done this, it applies the SelectMany operator (which you might be familiar with) and projects the asynchronous GetStringAsync method, which obtains the content of each of the URL.

Finally, we subscribe to the sequence with the Console.WriteLine method, which prints the contents of each element (the URL).

As we can see, we have found a way to manage concurrency in a natural and readable manner. We are asynchronously downloading the content of several URLs. No Loops, no async or await. We simply use the composition through a chain of calls (Fluent API) and define our sequence through operators.”


Reactive UI

This way of programming has implications both in the back-end and in the front-end. In the front-end we often face typical problems such as the calculation of derived properties, managing the changes that occur in the ViewModels or observing the changes in a rudimentary way (subscriptions to events, for example).

ReactiveUI facilitates the “reactive approach” providing us with a series of “helpers” with which we can handle the changes and react to them declaratively.

At the end, our ViewModels are transformed into definitions: definitions of each property and how the rest of the properties are derived + how they react to these changes.

Take a look at this Viewmodel example so you can visualize it more easily:

 

VIEW CODE

class ViewModel : ReactiveObject

{

private string firstName;

private string lastName;

private readonly ObservableAsPropertyHelper<string> fullName;

 

public ViewModel()

{

fullName = this

.WhenAnyValue(x => x.FirstName, x => x.LastName, (f, l) => $”{f} {l}”)

.ToProperty(this, x => x.FullName);

}

 

public string FirstName

{

get => firstName;

set => this.RaiseAndSetIfChanged(ref firstName, value);

}

 

public string LastName

{

get => lastName;

set => this.RaiseAndSetIfChanged(ref lastName, value);

}

 

public string FullName => fullName.Value;

}

Given 2 properties, FirstName and LastName, the FullName is derived from them as the combination of both.

Whenever the user changes either first name or last name, the full name will be automatically calculated in a functional fashion using the definition we gave:

fullName = this

.WhenAnyValue(x => x.FirstName, x => x.LastName, (f, l) => $”{f} {l}”)

.ToProperty(this, x => x.FullName);

This means: whenever FirstName or LastName changes, combine their values into a new derived value ($“{f} {l}”that are the first name and the last name respectively) and project the value into the FullName property (ToProperty).

 

How can I benefit?

This way of defining the flow of changes completely simplifies the most complicated of scenarios. The code turns into declarative definitions that are rather easy to maintain. The only problem is that most developers still don’t have the basic notions to understand what’s happening because they’re not familiarized with either functional programming or reactive programming.

But that’s precisely what we are going to address in our next webinar, all about Reactive UI which you can sign up to here!