Daniel Cazzulino's Blog : Reactive Framework Extensions Generator

Reactive Framework Extensions Generator

You probably know already that the Reactive Framework Extensions (Rx) is a new library on top of .NET 4.0 and Silverlight that allows developers to leverage the expressiveness and power of LINQ for .NET events. It brings an entirely new paradigm for doing event-driven apps, and therefore shines in WPF/Silverlight scenarios.

Read more about Rx at the team blog, the project home page and Matthew excelent blog series.

Even with the general availability of the bits for VS2010 beta2 at DevLabs, there’s still quite a bit of work you need to do in order to leverage the extensions. Specifically, you need to turn your events into IObservables that can then use the Rx extensions for querying and subscribing. This is a lot of repetitive and boring code that can be easily automated.

That’s precisely what this Clarius Labs provided extension does, enabling Reactive Framework Extensions for arbitrary assemblies, without writing any code. It basically traverses all public types in a given assembly (i.e. PresentationFramework.dll, for WPF) and generates a “Reactive” assembly for it (i.e. PresentationFramework.Reactive.dll) which contains the necessary extension methods for all public types that expose generic or custom delegate events that can be automatically converted to IObservables. With that in place, you can simply use the Reactive() extension method on your classes and access in a strong-typed fashion all events of that type as IObservables:

Typed events as IObservables

In order to get the extensions assembly generated, you simply right-click on a project or assembly reference, and select “Create Reactive Extensions”:

Executing the command

and a new assembly will be generated and referenced automatically:

Reference added

which will enable you to use LINQ operators and the new Observable APIs for all events exposed on all public types for the given assembly.

Matthew sample using our generator looks like:

var mouseMoves = from mm in mainCanvas.Reactive().MouseMove
                 let location = mm.EventArgs.GetPosition(mainCanvas)
                 select new { location.X, location.Y };

var mouseDiffs = mouseMoves
    .Skip(1)
    .Zip(mouseMoves, (l, r) => new { X1 = l.X, Y1 = l.Y, X2 = r.X, Y2 = r.Y });

var mouseDrag = from _ in mainCanvas.Reactive().MouseLeftButtonDown
                from md in mouseDiffs.Until(
                    mainCanvas.Reactive().MouseLeftButtonUp)
                select md;

Go to the Visual Studio Gallery and give it a try!

/kzu

/kzu dev↻d