More LINQ with System.Interactive - Getting Started
Join the DZone community and get the full member experience.
Join For Freewith the recent release of the reactive extensions for .net (rx) on devlabs , you’ll hear quite a bit about reactive programming, based on the iobservable<t> and iobserver<t> interfaces. a great amount of resources is available on channel 9 . in this series, i’ll focus on the dual of the system.reactive assembly, which is system.interactive, providing a bunch of extensions to the linq standard query operators for ienumerable<t>. in today’s installment we’ll talk about getting started with system.interactive, also touching briefly on the deep duality.
where to get it?
to get the reactive extensions, which include system.interactive, visit the landing page on devlabs over here . downloads are available for .net framework 3.5 sp1, .net framework 4.0 beta 2 and silverlight 3. in this series, i’ll be using the “desktop clr” distributions from visual studio 2008 and visual studio 2010.
the differences between the various distributions are of a technical nature and have to do with backporting certain essentials rx relies on, to the .net framework 3.5 sp1 stack. for instance, the iobservable<t> and iobserver<t> interfaces exist in .net 4.0 but don’t in .net 3.5. similarly, the task parallel library (tpl) is available in .net 4.0’s system.threading namespace, while rx redistributes it to run on .net 3.5 sp1.
what’s in it?
once you’ve installed, have a look at your program files (x86) folder, under microsoft reactive extensions. i’m using the “desktopv2” version here, which refers to clr 2.0 and the .net framework 3.5 sp1 package. the main difference with the “desktopv4” version is the presence of system.threading, which contains the parallel extensions that ship in .net 4.0:
a brief introduction to the remaining assemblies:
- system.coreex.dll contains some commonly used types like action and func delegates with bigger arities (up to 16 parameters), new property<t> primitives, a unit type, an event type wrapping “object sender, eventargs e” pairs, a notification<t> (which will be discussed extensively) and some notions of time in the form of timeinterval<t> and timestamped<t>.
- system.interactive.dll , the subject of this new series, contains extension methods for ienumerable<t> and additional linq to objects operators, provided in a type called enumerableex.
- system.reactive.dll , which is where rx gets its name for and which will be discussed in future series, is the home for reactive programming tools. it contains iobservable<t> and iobserver<t>, as well as various combinators over it (sometimes referred to as “linq to events”). in addition, it provides primitives like subjects and contains a join library (more about this in a separate installment).
duality? help!
as we like to use expensive words like “mathematical dual” it makes sense to provide some easy to grasp introduction to the subject. the first thing to look at is the distinction between interactive and reactive programming. in the diagram below, this is illustrated:
in the world of interactive programming, the application asks for more information. it pulls data out of a sequence that represents some data source, in particular by calling movenext on an enumerator object. the application is quite active in the data retrieval process: besides getting an enumerator (by calling getenumerator on an enumerable), it also decides about the pace of the retrieval by calling movenext at its own convenience.
in the world of reactive programming, the application is told about more information. data is pushed to it from a data source by getting called on the onnext method of an observer object. the application is quite passive in the data retrieval process: apart from subscribing to an observable source, it can’t do anything but reacting to the data pushed to it by means of onnext calls.
the nice thing about those two worlds is that they’re dual. the highlighted words in the paragraphs above have dual meanings. because of this observation, it’s desirable to search for dualities on a more formal and technical level as well. in particular, the interfaces being used here are the exact duals of one another: ienumerable<t> is to iobservable<t> as ienumerator<t> is to iobserver<t>. dualization can be achieved by turning inputs (e.g. method parameters) into output (e.g. return values):
lots of dualities exist in various disciplines, providing for great knowledge transfers between different domains. for example, in formal logic, de morgan’s law allows converting expressions built from conjunctions into ones built from disjunctions, and vice versa. in electronics, similarities exist between the behavior of capacitors and inductances: know one and how to go back and forth between domains, and you know the other. fourier calculus provides duals between time and frequency domains.
one thing all those have in common is a way to go back and forth between domains. such a mechanism exists in the world of system.reactive and system.interactive as well. every observable collection can be turned into an enumerable one and vice versa, using operators called toenumerable and toobservable. to get a feel about how those work, imagine an enumerable collection first. the only thing one can do to retrieve its data is enumerate over it. for all the values received, signal them on the resulting observable’s observer. in the opposite direction, you subscribe on an observable collection to receive the values thrown at you and keep them so that the resulting enumerable can fetch them.
in this series, we’ll not look over the garden wall to the reactive world just yet. instead, we’ll get our hands dirty in the world of system.interactive, a logical extension to .net 3.5’s ienumerable<t> extension methods, known as the standard query operators.
operators overview
the system.linq.enumerableex static class in system.interactive contains various (extension) methods that operator on ienumerable<t> enumerable collections. it should be seen as a logical extension to the system.linq.enumerable class in system.core. in the illustration below i’ve summarize the various categories those new operators fall into. some could be considered to fall in multiple categories, so take this with a grain of salt. nevertheless, we’ll look at those big buckets in subsequent posts in this series:
- imperative use – provides operators that execute a sequence (run) and inject side-effecting actions in a chain of query operator calls (do), which is handy for debugging.
- exceptions – enumeration of sequences can cause exceptions (e.g. if you write an iterator, but also by other means – see later), which may need to be handled somehow. methods like catch, finally, using, onerrorresumenext and retry provide means to make a sequence resilient in face of exceptions.
- constructors – instead of creating an iterator yourself, it’s possible to let the system create a sequence on your behalf, e.g. by providing it a generator function (generate), by composing sequences and elements (return, startwith, throw), or triggering the call of a deferred constructor function when a client start enumerating (defer).
- code = data – the triplet of onnext, onerror and oncomplete seen on iobserver<t> is a very code-centric way of signaling various outcomes of data consumption. an alternative view is to treat those outcomes as pieces of data, called notifications (notification<t>). using materialize and dematerialize, one can transfer back and forth between those two domains.
- combinators – producing sequences out of one or more existing sequences is what combinators generally do. one can repeat a sequence a number of times (repeat), zip two sequences together (zip), let two sequences battle to provide a result the fastest (amb), and more. those operators are most “in line” with what you already know from system.linq today.
- functional – while the imperative and exception categories acknowledge the possibility for sequence to exhibit side-effects, the functional category is meant to tame the side-effects, typically in one-producer-many-consumer scenarios. when a sequence may produce side-effects during iteration, it may be desirable to avoid duplication of those when multiple consumers iterate.
- miscellaneous – just that, miscellaneous.
next time, we’ll start by looking at the “imperative use” category. download the libraries today and start exploring!
Published at DZone with permission of Bart De Smet, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Top Six React Development Tools
-
Decoding eBPF Observability: How eBPF Transforms Observability as We Know It
-
How to Use an Anti-Corruption Layer Pattern for Improved Microservices Communication
-
TDD vs. BDD: Choosing The Suitable Framework
Comments