Ever looking for a new, generic, way to incorporate asynchronous programming capabilities into your applications? Ever wish you could just write a LINQ query over your application events and just create reaction methods for when an event occurs?
Well now you can with the .NET Rx Framework - which provides a new look at asynchronous programming. It’s really just LINQ over IEnumerable…
Here’s a good way on how to think about this new capability – start with the fact that there are really only (2) types of programs out there:
Today, reactive programming by specified by methods that are called at unpredictable times – like:
button.MouseMove += (o, mouseEventArgs) => Debug.Writeline(“You moved the mouse to {0}”, mouseEventArgs.GetPosition(button));
Compare this with an .NET Rx query that creates a dragging event for a Silverlight/WPF control:
IObservable<Event<MouseEventArgs>> draggingEvent = from mouseLeftDownEvent in control.GetMouseLeftDown() from mouseMoveEvent in control.GetMouseMove().Until(control.GetMouseLeftUp()) select mouseMoveEvent; A verbal description of this query might be:
IObservable<Event<MouseEventArgs>> draggingEvent = from mouseLeftDownEvent in control.GetMouseLeftDown() from mouseMoveEvent in control.GetMouseMove().Until(control.GetMouseLeftUp()) select mouseMoveEvent;
A verbal description of this query might be:
“For each mouse left down event, get each mouse move event and return it until the next mouse left up event occurs.”
As you can see, using “from” allows us to declaratively sequence events. The data passed to event handlers and callbacks can be thought of as sequences of data that are “pushed” at you rather than “pulled.”…
Every time an event is fired we get “pushed” a new piece of data: the EventArgs. Similarly when a callback is invoked it is typically “pushed” the result of the asynchronous method.
Check out these blog posts for additional information:
http://visualstudiomagazine.com/articles/2009/10/01/net-rx-framework.aspx
http://themechanicalbride.blogspot.com/2009/07/introducing-rx-linq-to-events.html