Reactive Extensions: Observing an Event
Thursday, November 13, 2014 Category : CEP, Reactive Extensions, RX 0
In the last post, NEsper (.NET library on Esper for Java) complex event processing was discussed. This post discusses the basics of observing a simple .NET event using .NET Reactive Extensions.
.NET Reactive Extensions build on IObservable and make event based programming simpler. One advantage I came across of using RX over NEsper is that it is easier to write unit tests. NEsper hides a lot of its functionality in internal libraries for which writing unit test cases tightly integrates with the NEsper queries and it may not be possible to cover everything. In the same breath, it also comes to mind that performance-wise NEsper may perform better since it is Java based. Performance comparison of NEsper and RX to be done in one of the subsequent posts.
For now, this post focuses on writing a very simple example of how to observe another class' events. Let us assume we have a data generator class:
The above code creates an instance of the data generator class (SomeDataGenerator). The FromEventPattern method allows attaching and detatching to the event we want to observe, which is dg.ev. Once we start observing the event, we can subscribe to the event firing by calling Subscribe on the observable.
So whats the big deal? We can do that by attaching events anyway in .NET without RX. The difference is apparant when we try to do something more complex like filtering data out of an incoming stream of data| or grouping it | or performing aggregations on windows like moving average. This code subscribes to each firing of the event. So the code given to Subscribe is called everytime the event is raised. This behaviour can be changed for grouping/aggregating/complex-merging the continuous stream of data thrown by SomeDataGenerator. Detailed sample to be discussed in next post on that.
.NET Reactive Extensions build on IObservable and make event based programming simpler. One advantage I came across of using RX over NEsper is that it is easier to write unit tests. NEsper hides a lot of its functionality in internal libraries for which writing unit test cases tightly integrates with the NEsper queries and it may not be possible to cover everything. In the same breath, it also comes to mind that performance-wise NEsper may perform better since it is Java based. Performance comparison of NEsper and RX to be done in one of the subsequent posts.
For now, this post focuses on writing a very simple example of how to observe another class' events. Let us assume we have a data generator class:
internal class SomeDataGenerator
{
public event EventHandler ev;
Timer tim;
public SomeDataGenerator()
{
tim = new Timer();
tim.Interval = 1000;
tim.Elapsed += tim_Elapsed;
tim.Start();
}
void tim_Elapsed(object sender, ElapsedEventArgs e)
{
if (ev != null)
ev(this, null);
}
}
For observing such an event that conforms to the standard .NET event pattern (having two parameters - sender and eventargs), we can use the Observable.FromEventPattern method from the System.Reactive.Linq library:
public static event EventHandler SimpleEvent;
static void Main(string[] args)
{
SomeDataGenerator dg = new SomeDataGenerator();
IObservable<EventPattern<object>> eventAsObservable = Observable.FromEventPattern(
ev => dg.ev += ev,
ev => dg.ev -= ev);
Console.WriteLine("Subscribe");
var subs = eventAsObservable.Subscribe(ar => Console.WriteLine("Received event..."));
Console.Read();
}
The above code creates an instance of the data generator class (SomeDataGenerator). The FromEventPattern method allows attaching and detatching to the event we want to observe, which is dg.ev. Once we start observing the event, we can subscribe to the event firing by calling Subscribe on the observable.
So whats the big deal? We can do that by attaching events anyway in .NET without RX. The difference is apparant when we try to do something more complex like filtering data out of an incoming stream of data| or grouping it | or performing aggregations on windows like moving average. This code subscribes to each firing of the event. So the code given to Subscribe is called everytime the event is raised. This behaviour can be changed for grouping/aggregating/complex-merging the continuous stream of data thrown by SomeDataGenerator. Detailed sample to be discussed in next post on that.