Pages

Monday, December 2, 2013

Learning Reactive Extensions(Rx) with the help of LinqPad

I am currently attempting to become proficient in using ReactiveUI (https://github.com/reactiveui/ReactiveUI) for WPF and cross-platform mobile development.  In order to understand the paradigm I am first trying to understand the core concepts of reactive programming in general using Reactive Extensions (Rx).  

I am using the online book http://www.introtorx.com by Lee Campbell, which so far is excellent.

My method is to use LinqPad (http://www.linqpad.net) to run the examples and play around with different Rx queries.  If you do not have LinqPad, go get it now...I will wait. 

Why LinqPad you ask?  Why not just use Visual Studio?  Well, because Visual Studio is mind numbingly slow.  LinqPad is extremely fast and doesn’t get in the way of your cognitive flow.  Trust me, in the time it takes VS to compile and run a simple Rx query you will be off reading Reddit or Facebook.

Now that you have LinqPad, you may find it tedious to run Reactive based code.  This is because you need to add references and namespaces.  You can, and I suggest that you do use LinqPad to download built in Rx samples, which work out of the box, since all of the references are built in.  You could just overwrite one of these templates and piggyback on the references, but I will show you how to setup your own.

1) Open the LinqPad installation folder and create a References sub-folder
     Default location is C:\Program Files (x86)\LINQPad4\References

2) Add the following dll’s to the reference folder.
       The way I obtained the dll’s was to create a project in Visual Studio and then use nuget/Package Manage Console to add Reactive Extensions (install-package Rx-Main). I then went to the download directory and copied the dll’s. If you purchase the full version of LinqPad you can use nuget directly.

System.Reactive.Core.dll
System.Reactive.Interfaces.dll
System.Reactive.Linq.dll
System.Reactive.PlatformServices.dll
System.Reactive.Windows.Threading.dll


3) Create a new query (File->New Query). And add the following code:

If you attempt to run it now it will not work due to missing references.


4) Hit F4. That will open the Query properties screen and allow you to add references. Use the Browse button
to locate the dll’s in the References folder created in step 1.


5) Select the “Additional Namespace Imports” tab and add the following namespace.


6) Hit OK. You should now be able to run you Reactive Extension queries in LinqPad.

One thing to note is that you may need to add more namespace imports based on what
reactive objects you use.  A few that I needed right away were:
System.Reactive.Disposables
System.Reactive.Linq
System.Reactive.Subjects

Happy Coding