Quantcast
Viewing latest article 1
Browse Latest Browse All 9

Experiments with F# + WPF + M-V-VM

In last few days I was thinking about what this new decade is going to bring to software development. I feel we will experience more rapid pace of technological changes and how we do things in this decade. Just look around and you can see things like cloud computing, functional programming, domain specific language and few more on the horizon.

I decided to experiment with functional programming using F#, it is bundled with Visual Studio 2010 (beta 2 as of now).  I did not come across many UI applications done using F# and WPF, so I decided to do one. Following screenshot shows outcome of this exercise.

Image may be NSFW.
Clik here to view.
BingSearch_5
 
It simply uses Bing API’s OpenSearch compliant RSS interface for querying the web and image search result for the user specified term. The web and image search results are displayed on different tabs. The Bing API’s RSS interface doesn’t require any registration or application id.
There are so many new programming constructs available in F# that it takes a little time to get up-to-speed with what's available and how it can be used. This sample might not demonstrate the most elegant way of doing functional programming, I hope to cover that in future posts.

I have employed M-V-VM pattern for presentation and logic separation. Views are done using WPF/ XAML , the view model is written in F# while the Bing API provides the data. I have clubbed the Bing API interaction logic with the view model. I have used WPF Toolkit’s DelegateCommand to handle commands from the views. Instead of referencing the complete WPF MVVM Toolkit, I have simply added couple of required classes from it in a separate project which is referenced by F# view model project. Following are the classes present in the view model, name of the classes are descriptive enough to indicate the role it plays.

Image may be NSFW.
Clik here to view.
Bing_Classes
 
I use System.Net.WebRequest to get the RSS XML from the Bing API asynchronously. Following code snippet shows the F# code to do this

   1: async{ let request = WebRequest.Create uri
   2:        use! response = request.AsyncGetResponse()    
   3:        use stream = response.GetResponseStream()
   4:        use reader = new StreamReader(stream)
   5:        let lines = [ while not reader.EndOfStream doyield reader.ReadLine() ]
   6:return lines } 

 
Concurrency is going to be the next major revolution in how we write the software.  Though Moore’s Law is not over yet, we will have to prepare ourselves to take advantage of multi-core processors. Designers of new languages like F# do consider this upcoming trend and provides constructs to parallelize the computation and I/O operations. Following code snippet shows how the code shown above is executed parallelly using Async.Parallel construct against a for loop.

   1: member se.Search(searchTerm:string)= 
   2:         async{  let searchAPIUrls = new List<string>()
   3:                 searchAPIUrls.Add("http://api.search.live.com/rss.aspx?source=web&query=" + searchTerm)
   4:                 searchAPIUrls.Add("http://api.search.live.com/rss.aspx?source=image&query=" + searchTerm)
   5:                 let webResponses = 
   6:                     Async.Parallel [for site in searchAPIUrls -> GetWebResponse site ]
   7:                     |> Async.RunSynchronously
   8:  
   9:                 let rssParser = RSSParser()
  10:                 let results = new Dictionary<WebSearchType,ObservableCollection<RSSItem>>()
  11:                 let tempResults = new List<string>()
  12:for str in webResponses do
  13:                     tempResults.Add(str.Item(0))
  14:  
  15:                 let mutable items = ObservableCollection<RSSItem>()
  16:
  17:                 items <- rssParser.Parse tempResults.[0]     
  18:                 results.Add(WebSearchType.Web,items)
  19:  
  20:                 items <- rssParser.Parse tempResults.[1]     
  21:                 results.Add(WebSearchType.Image,items)
  22:
  23:return results}

The code for this sample application can be downloaded from here

I would be happy to hear your comments on this.

Technorati Tags: ,,


Image may be NSFW.
Clik here to view.

Viewing latest article 1
Browse Latest Browse All 9

Trending Articles