This blog post is for those folks comfortable with creating a data stream from an IEnumerable, IObservable or StreamInsight adapter data source. If you’d like more details on creating those, have a look at this blog post, which covers creating an IEnumerable ‘canned’ data source then wrapping a LINQPad context around it.
Creating a data context is very straightforward:
- Create a new C# class library project in Visual Studio 2010 called SampleContext.
- Ensure that the Framework type is set to .NET Framework 4 (and NOT .NET Framework 4 Client Profile)
- Add a reference to Microsoft.ComplexEventProcessing.dll
- Add a reference to Microsoft.ComplexEventProcessing.Observable.dll
- This is required for the IObservable<T>.ToPointStream extension method.
- Add a reference to Microsoft.ComplexEventProcessing.Adapters.dll
- Optional – this is only required if you have an adapter embedded inside of your context
- Add a reference to System.Reactive.dll
- Optional – this is only required for creating different types of IObservable streams.
- Note: System.Reactive.dll is part of the Reactive Extensions framework, which needs to be downloaded separately.
- Derive a class from StreamInsightContext in the StreamInsightLinqPad.Samples assembly (available as part of the LINQPad driver for StreamInsight download).
- Expose your stream as a property of the class.
|Download the sample project here.|
That’s it! Here’s what this looks like in practice. First we establish the basic class
Now we’ll go ahead and expose three types of streams. The first will be one generated from an IEnumerable.
Next, we’ll use IObservable (created via Observer.Interval) to generate a “live” stream of events:
Finally, we’ll leverage the DataGenerator sample adapter (from http://streaminsight.codeplex.com/) to demonstrate creating a LINQPad stream from an adapter.
To test, we’ll need to configure LINQPad with our assembly as a data context. To do this:
- Add Connection, then select Microsoft StreamInsight from the list of drivers.
- Select Custom Context from the Context Kind dropdown.
- Click on the ellipsis for Assembly Path (…), then browse to the directory containing SampleContext.dll.
- Click OK to finish selecting the new context.
The context streams should now be visible in LINQPad, similar to the screenshot below. Create a new Query, and select C# Statements as the Language, and StreamInsight: SampleContext as the database.
Let’s dump our streams, using this LINQ code:
This will show us our enumerable and observable streams:
Finally, let’s dump out some data from our adapter stream:
|Note – I had to make one change to the adapter code to make it work with ToObservable. The GeneratedEvent definition doesn’t define a default constructor (which is needed by ToObservable). In GeneratedEvent.cs, on line 23 I added the code:
That’s it – a LINQPad data context in a nutshell.