<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Gideon Dsouza - system.reactive</title><link>http://www.gideondsouza.com:80/Tags/system.reactive</link><description>Gideon Dsouza - system.reactive</description><item><title>Abstracting reactive extensions for sql server compact and implementing an instant search</title><link>http://www.gideondsouza.com:80/blog/abstracting-reactive-extensions-for-sql-server-compact-and-implementing-an-instant-search</link><description>&lt;p&gt;So in the &lt;a href="http://www.gideondsouza.com/blog/implementing-simple-instant-search-with-rx-reactive-extensions"&gt;last Rx article&lt;/a&gt; I talked about instant search. This article, we're going to go quite a few steps further and search an sql-compact-4 database asychronously! Thats right!&lt;/p&gt;

&lt;p&gt;But! Its going to be a little head churner with lambdas and what nots, because we're going to encapsulate retreiving the data in a nice clean way(in my opinion) &lt;/p&gt;

&lt;p&gt;You could also just:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="#code"&gt;Skip to the code below&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.gideondsouza.com/Media/Default/Misc/BasicRxSearchClean.zip"&gt;Download the Sample&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Check the source on &lt;a href="https://github.com/thegiddygeek/RxSearchDB"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What are we making:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src="http://www.gideondsouza.com/Media/Default/BlogPost/blog/product.JPG" alt="enter image description here"&gt;&lt;/p&gt;

&lt;p&gt;You'll need the &lt;a href="http://visualstudiogallery.msdn.microsoft.com/0e313dfd-be80-4afb-b5e9-6e74d369f7a1/"&gt;SQL CE Compact Toolbox&lt;/a&gt; because we are going to want an entity framework object context. (Which visual studio won't create for sqlce4 if its not a web project)&lt;/p&gt;

&lt;p&gt;You'll also want the solution from the &lt;a href="http://www.gideondsouza.com/blog/implementing-simple-instant-search-with-rx-reactive-extensions"&gt;previous blog post on Rx&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Instructions:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt; - Open the solution, open solution explorer (Ctrl + W, S). Right click the project and choose &lt;em&gt;add new item&lt;/em&gt;. Select &lt;em&gt;Data&lt;/em&gt; on the left and choose &lt;em&gt;SQL Server Compact 4 Local Database&lt;/em&gt; (Phew!)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt; - Its more easier to use Visual Studio's server explorer to create a new table. Open server explorer (Ctrl + W, L), it should show your database. (If it doesn't, right click connections and click &lt;em&gt;add connection&lt;/em&gt;). Expand your database, Right click Tables and click &lt;em&gt;Create Table&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Create a table like the following, note &lt;em&gt;ID&lt;/em&gt; is an identity:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://www.gideondsouza.com/Media/Default/BlogPost/blog/step2.JPG" alt="enter image description here"&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt; - Throw in some data:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://www.gideondsouza.com/Media/Default/BlogPost/blog/step3.JPG" alt="enter image description here"&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt; - Add an entity model for your database. You'll need to have installed SQL Compact Toolbox, goto &lt;code&gt;Tools&amp;gt;Sql Compact Toolbox&lt;/code&gt;, Right Click your database and click &lt;em&gt;Add Entity Data Model to current project&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://www.gideondsouza.com/Media/Default/BlogPost/blog/step4.jpg" alt="enter image description here"&gt;&lt;/p&gt;

&lt;h4&gt;The code&lt;p id="code"&gt;&lt;/h4&gt;

&lt;p&gt;We'll add two helper classes to &lt;strong&gt;abstract&lt;/strong&gt; the task of searching our database asychronously with Rx.&lt;/p&gt;

&lt;script src="https://gist.github.com/1422970.js"&gt; &lt;/script&gt;

&lt;p&gt;So we're defining a method here that will be based on a Type T, which will be our entity object and TContext which will be the DataContext. This complete generic interface will help you later on when you want to mock and/or use DI. (I typically use &lt;a href="http://mef.codeplex.com/"&gt;MEF&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;We then implement it like so:&lt;/p&gt;

&lt;script src="https://gist.github.com/1422975.js"&gt; &lt;/script&gt;

&lt;p&gt;Notice, we'll use the &lt;code&gt;T&lt;/code&gt; at &lt;em&gt;runtime&lt;/em&gt; so we can use this like a repository for any entity object. &lt;/p&gt;

&lt;p&gt;Our one and only function &lt;code&gt;GetAllAsObservables&lt;/code&gt;, uses the &lt;code&gt;ToAsync&lt;/code&gt; method to &lt;strong&gt;convert&lt;/strong&gt; the lambda passed as an argument, into a function which returns an &lt;code&gt;IObservable&lt;/code&gt;. We then make a call to this function &lt;code&gt;getall()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We wrote &lt;code&gt;ToAsync(Database1Entities, IQueryable&amp;lt;T&amp;gt;)&lt;/code&gt; so &lt;code&gt;getall&lt;/code&gt; is a function that takes an &lt;strong&gt;instance&lt;/strong&gt; of &lt;code&gt;Database1Entities&lt;/code&gt; and returns an &lt;code&gt;IObservable&amp;lt;T&amp;gt;&lt;/code&gt; which we transform/project into &lt;code&gt;IObservable&amp;lt;IList&amp;lt;T&amp;gt;&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Our MainForm code now looks like this:&lt;/p&gt;

&lt;script src="https://gist.github.com/1422976.js"&gt; &lt;/script&gt;

&lt;h4&gt;Explaination :&lt;/h4&gt;

&lt;p&gt;First off, what we did &lt;a href="http://www.gideondsouza.com/blog/implementing-simple-instant-search-with-rx-reactive-extensions"&gt;last time&lt;/a&gt; was, we changed the &lt;code&gt;Textbox.TextChanged&lt;/code&gt; event to an observable sequence and throttled it by 500 milliseconds. So, our code got called by the &lt;em&gt;system&lt;/em&gt; every 500ms, we then executed &lt;code&gt;DO&lt;/code&gt; every time this happened. &lt;/p&gt;

&lt;p&gt;The original &lt;code&gt;SearchList&lt;/code&gt; function does not &lt;strong&gt;search asynchronously&lt;/strong&gt;, you could try and put &lt;code&gt;Thread.Sleep(5000)&lt;/code&gt; inside and it will block the UI.&lt;/p&gt;

&lt;p&gt;So now!! We execute an async call through our little helper class, to search our database.&lt;/p&gt;

&lt;p&gt;Remember our &lt;code&gt;GetAllAsObservables()&lt;/code&gt; expects a lamba that takes our data context and returns an &lt;code&gt;IQueryable&amp;lt;T&amp;gt;&lt;/code&gt;, this means you can pretty much run any query against your context an long as it returns the right type. Also, this runs in the context of sql, not in memory.&lt;/p&gt;

&lt;p&gt;We get in return an &lt;code&gt;IObservable&amp;lt;IList&amp;lt;T&amp;gt;&amp;gt;&lt;/code&gt; getfn. We then use &lt;code&gt;ObserveOn&lt;/code&gt; because we're going to touch UI from another thread. (This is equivalent to those &lt;code&gt;Invoke()&lt;/code&gt; calls you made in your old async code). This time on subscribe you are returned a an &lt;code&gt;IList&amp;lt;Customer&amp;gt;&lt;/code&gt;. The lambda you pass into &lt;code&gt;Subscribe&lt;/code&gt; is executed when the search is done async!&lt;/p&gt;

&lt;p&gt;So try and run the code! Type in some names! =D&lt;/p&gt;

&lt;p&gt;I hope this was useful, I'll take this further and get into how to properly abstract data access with Rx by creating Observable repositories. I've used these quite sucessfully in a number of apps!&lt;/p&gt;

&lt;p&gt;Any critique,  good, bad, ugly is welcome!&lt;/p&gt;

&lt;p&gt;Happy Coding&lt;/p&gt;
</description><pubDate>Sat, 19 Nov 2011 11:04:27 GMT</pubDate><guid isPermaLink="true">http://www.gideondsouza.com:80/blog/abstracting-reactive-extensions-for-sql-server-compact-and-implementing-an-instant-search</guid></item><item><title>Implementing simple instant search with Rx (Reactive Extensions)</title><link>http://www.gideondsouza.com:80/blog/implementing-simple-instant-search-with-rx-reactive-extensions</link><description>&lt;p&gt;Haven't heard of Rx, this could be a little starter tutorial for you.&lt;/p&gt;

&lt;p&gt;Rx or Microsoft's &lt;a href="http://msdn.microsoft.com/en-us/data/gg577609"&gt;Reactive extensions &lt;/a&gt;makes life simple when it comes to asychronous programming&lt;/p&gt;

&lt;p&gt;My most favorite thing about Rx is how simple it makes &lt;em&gt;instant&lt;/em&gt; search! 
A while ago I &lt;a href="http://stackoverflow.com/questions/4278069/implement-instant-threaded-search-algorithm"&gt;asked around on stackoverflow&lt;/a&gt; and just architecturing it was so difficult.&lt;/p&gt;

&lt;p&gt;So I'm going to go through a dead simple example, where we're going to have an &lt;strong&gt;instant search&lt;/strong&gt;, but, we'll have some dummy data in memory that we're going to search.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt; : I wrote a continuation of this article &lt;a href="http://gideondsouza.com/blog/abstracting-reactive-extensions-for-sql-server-compact-and-implementing-an-instant-search"&gt;here&lt;/a&gt; where I describe instant search with Rx on a SqlCE database and a nice clean abstraction.&lt;/p&gt;

&lt;p&gt;Here's what it'll look like:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://www.gideondsouza.com/Media/Default/BlogPost/blog/appCapture.JPG" alt="Instant Search App"&gt;&lt;/p&gt;

&lt;p&gt;I hope to do a post on searching real data, maybe with sql-compact 4, and then I'll get on to how to write &lt;a href="http://stackoverflow.com/questions/5349140/iasyncrepository-or-iobservablerepository-for-silverlight-4-wcf-data-services"&gt;Observable Repositories&lt;/a&gt; which makes life easier, especially for Silverlight!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How To:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So head over to Visual Studio and create a new form (or you can &lt;a href="http://www.gideondsouza.com/Media/Default/Misc/BasicRxSearchClean.zip"&gt;download the sample&lt;/a&gt;). Add a textbox and listbox like the screenshot.&lt;/p&gt;

&lt;p&gt;We need to install &lt;a href="http://nuget.org/List/Packages/Rx-Main"&gt;Rx-Main&lt;/a&gt; and &lt;a href="http://nuget.org/List/Packages/Rx-WinForms"&gt;Rx-WinForms&lt;/a&gt; from nuget.&lt;/p&gt;

&lt;p&gt;You can right click your solution and choose &lt;code&gt;Manage Nuget Packages..&lt;/code&gt; and search and install these.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://www.gideondsouza.com/Media/Default/BlogPost/blog/nugetScrenie.JPG" alt="Visual Studio Package Manager"&gt;&lt;/p&gt;

&lt;p&gt;This is the code then:&lt;/p&gt;

&lt;script src="https://gist.github.com/1422982.js"&gt; &lt;/script&gt;

&lt;p&gt;&lt;strong&gt;What/How/Why?&lt;/strong&gt; :&lt;/p&gt;

&lt;p&gt;The starting point to observables is the &lt;a href="http://msdn.microsoft.com/en-us/library/hh244252(v=VS.103).aspx"&gt;Observable&lt;/a&gt; class, the &lt;a href="http://msdn.microsoft.com/en-us/library/hh229251(v=VS.103).aspx"&gt;FromEventPattern&lt;/a&gt; method creates an observable sequence from the &lt;code&gt;TextChanged&lt;/code&gt; event of the textbox. &lt;/p&gt;

&lt;p&gt;Think of this like a list of notifications coming to you. &lt;/p&gt;

&lt;p&gt;We then chain a method call to &lt;a href="http://msdn.microsoft.com/en-us/library/hh229400(v=VS.103).aspx"&gt;Throttle&lt;/a&gt; (my fav part!), it well, throttles this list of notifications by 500 milliseconds. &lt;/p&gt;

&lt;p&gt;Here, for win-forms you need the &lt;a href="http://msdn.microsoft.com/en-us/library/hh212067(v=VS.103).aspx"&gt;ControlScheduler&lt;/a&gt; class otherwise the throttling doesn't work too well. This class comes from the Rx-WinForms package we added earlier. The &lt;a href="http://msdn.microsoft.com/en-us/library/hh228941(v=VS.103).aspx"&gt;Do&lt;/a&gt; method just does the metho you want to run, and you send off the text from the the textbox over. Lastly, calling Subscribe starts off the notifications.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.gideondsouza.com/Media/Default/Misc/BasicRxSearchClean.zip"&gt;Download the Sample here.&lt;/a&gt;(1.3MB) It's a clean solution but with the Rx nuget packages.&lt;/p&gt;

&lt;p&gt;Hope this was useful!&lt;/p&gt;
</description><pubDate>Thu, 17 Nov 2011 19:21:00 GMT</pubDate><guid isPermaLink="true">http://www.gideondsouza.com:80/blog/implementing-simple-instant-search-with-rx-reactive-extensions</guid></item></channel></rss>