Introduction to Rx
Kindle edition (2012)
Reactive programming is not a new concept. I remember studying my first Event Driven module for Visual Basic 5 in 2000. Even then the technology (Visual Basic 5) was already considered somewhat dated. Long before VB5 and the turn of the millennium, we have seen languages supporting events. Over time languages like Smalltalk, Delphi and the .NET languages have popularized reactive or event-driven programming paradigms. This not to say that events are passé: current trends such as CEP (Complex Event Processing), CQRS (Command Query Responsibility Segregation) and rich immersive GUIs, all have events as a fundamental part of their makeup.
The event driven paradigm allows for code to be invoked without the need for breaking encapsulation or applying expensive polling techniques. This is commonly implemented with the Observer pattern, events exposed directly in the language (e.g. C#) or other forms of callback via delegate registration. The Reactive Extensions extend the callback metaphor with LINQ to enable querying sequences of events and managing concurrency.
As Rx is just a library, the team at Microsoft delivering Rx was able to isolate themselves from the release schedule of the .NET Framework. This proved important as the libraries saw fairly constant evolution since late 2009 through to their official release in mid 2011. This evolution has been largely enabled by the openness of the team and their ability to take onboard criticisms, suggestions and feature requests from the brave community of pre-release users.
While Rx is just a library, it is a significant and bold move forward for the team at Microsoft and for any consumers of the library. Rx will change the way you design and build software for the following reasons:
- The way that it tackles the Observer pattern is a divorce from .NET events toward a Java-style interface pattern but far more refined.
- The way it tackles concurrency is quite a shift how many .NET developers would have done it before.
- The abundance of (extension) methods in the library.
- The way in which it integrates with LINQ to leverage LINQ's composability & declarative style, makes Rx very usable and discoverable to those already familiar with LINQ and IEnumerable<T>.
- The way it can help any .NET developer that works with event driven and/or asynchronous programs. Developers of Rich Clients, Web Clients and Services alike can all benefit from Rx.
- The future plans seem even grander, but that is a different book for some time in the future :-)
This book aims to teach you:
- about the new types that Rx will provide
- about the extension methods and how to use them
- how to manage subscriptions to "sequences" of data
- how to visualize "sequences" of data and sketch your solution before coding it
- how to deal with concurrency to your advantage and avoid common pitfalls
- how to compose, aggregate and transform streams
- how to test your Rx code
- some guidance on best practices when using Rx.
The best way to learn Rx is to use it. Reading the theory from this book will only help you be familiar with Rx, but will not really enable you to fully understand Rx. You can download the latest version of Rx from the Microsoft Data Developer site (http://msdn.microsoft.com/en-us/data/gg577609) or if you use NuGet you can just download Rx via that.
My experience with Rx is straight from the trenches. I worked on a team of exceptional developers on a project that was an early adopter of Rx (late 2009). The project was a financial services application that started off life as a Silverlight project then expanded into an integration project. We used Rx everywhere; client side in Silverlight 3/4, and server side in .NET 3.5/4.0. We used Rx eagerly and sometimes too eagerly. We were past leading edge, we were bleeding edge. We were finding bugs in the early releases and posting proposed fixes to the guys at Microsoft. We were constantly updating to the latest version. It cost the project to be early adopters, but in time the payment was worth it. Rx allowed us to massively simplify an application that was inherently asynchronous, highly concurrent and targeted low latencies. Similar workflows that I had written in previous projects were pages of code long; now with Rx were several lines of LINQ. Trying to test asynchronous code on clients (WPF/Win Forms/Silverlight) was a constant challenge, but Rx solved that too. Today if you ask a question on the Rx Forums, you will most likely be answered by someone from that team (or Dave Sexton).
I would like to take this quick pause to recognize the people that made this book possible. First is my poor wife for losing a husband to a dark room for several months. Her understanding and tolerance is much appreciated. To my old team "Alpha Alumni"; every developer on that team has helped me in some way to better myself as a developer. Specific mention goes to James Miles, Matt Barrett, John Marks, Duncan Mole, Cathal Golden, Keith Woods, Ray Booysen & Olivier DeHeurles for all the deep dive sessions, emails, forum banter, BBM exchanges, lunch breaks and pub sessions spent trying to get our heads around Rx. To Matt Davey for being brave enough to support us in using Rx back in 2009. To the team at Microsoft that did the hard work and brought us Rx; Jeffery Van Gogh, Wes Dyer, Erik Meijer & Bart De Smet. Extra special mention to Bart, there is just something about the content that Bart produces that clicks with me. Finally to the guys that helped edit the book; Joe Albahari and Gregory Andrien. Joe is a veteran author of books such as the C# in a nutshell, C# pocket reference and LINQ pocket reference, and managed to find time to help out on this project while also releasing the latest versions of these books. For Gregory and I, this was a first for both of us, as editor and author respectively. Gregory committed many late nights to helping complete this project. There is also some sweet irony in having a French person as the editor. Even though English is not his native tongue, he clearly has a better grasp of it than I.
It is my intention that from the experiences both good and bad, I can help speed up your understanding of Rx and lower that barrier to entry to using Rx. This will be a progressive step-by-step approach. It may seem slow in places, but the fundamentals are so important to have a firm grasp on the powerful features. I hope you will have the patience to join me all the way to the end.
The content of this book was originally posted as a series of blog posts at http://LeeCampbell.blogspot.com and has proved popular enough that I thought it warranted being reproduced as an e-book. In the spirit of other books such as Joe Albahari's Threading in C# and Scott Chacon's Pro Git books, and considering the blog was free, I have made the first version of this book free.
The version that this book has been written against is the .Net 4.0 targeted Rx assemblies version 1.0.10621.0 (NuGet: Rx-Main v1.0.11226).
So, fire up Visual Studio and let's get started.
Additional recommended reading
|Moving on to : PART 1 - Getting started>>|