<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7299750660882548845</id><updated>2012-01-26T06:05:38.767-05:00</updated><category term='Rx'/><category term='LINQ'/><category term='DLR'/><category term='WCF RIA Services'/><category term='MVVM'/><category term='JAVA'/><category term='SQL'/><category term='Powershell'/><category term='CSS'/><category term='NCLOB'/><category term='UTF-8'/><category term='HTTPS'/><category term='UTF-32'/><category term='PLINQ'/><category term='ASCII'/><category term='XML'/><category term='IIS'/><category term='Azure'/><category term='Business Intelligence'/><category term='CLOB'/><category term='ASP.NET'/><category term='HTTP'/><category term='C#'/><category term='UTF-16'/><category term='Sets'/><category term='HTML Agility Pack'/><category term='JQuery'/><category term='Windows Phone 7'/><category term='Extension Methods'/><category term='Base-64'/><category term='HTML'/><category term='Authentication'/><category term='Observer pattern'/><category term='Certificates'/><category term='IronRuby'/><category term='Data Warehouse'/><category term='SSL'/><category term='Android'/><category term='Design Patterns'/><category term='BLOB'/><category term='Entity Framework'/><category term='Silverlight'/><category term='HTML5'/><category term='.NET'/><category term='Excel'/><category term='DOS'/><title type='text'>Using System.Life.Code;</title><subtitle type='html'>(blog *) malloc(sizeof(myBrain))</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.akhildeshpande.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>45</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-2985423405759765898</id><published>2011-11-13T16:39:00.001-05:00</published><updated>2011-11-13T17:17:39.268-05:00</updated><title type='text'>Task and ConcurrentQueue</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog, I will create a sample to demo the usage of Task and ConcurrentQueue. Task allows us to represent an asynchronous operation. ConcurrentQueue is a FIFO data structure similar to Queue, however, it is thread-safe. Thread-safe means that we will not have undesirable results in case multiple threads operate over this data structure.&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;First, let's see the code sample:&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; QueueObject&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; DateTime StartTime { get; set; }&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; DateTime EndTime { get; set; }&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Id { get; set; }&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; ConsumingThreadId { get; set; }&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; ProducingThreadId { get; set; }&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; DecimalPlaces { get; set; }&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Answer { get; set; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;class&lt;/span&gt; Program&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; ConcurrentQueue&amp;lt;QueueObject&amp;gt; bc = &lt;span class="kwrd"&gt;new&lt;/span&gt; ConcurrentQueue&amp;lt;QueueObject&amp;gt;();&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"Main Thread Id: "&lt;/span&gt; + Thread.CurrentThread.ManagedThreadId);&lt;br /&gt;           &lt;br /&gt;            var t1 = Task.Factory.StartNew(ProduceTasks);&lt;br /&gt;            &lt;br /&gt;            var t2 = Task.Factory.StartNew(ConsumeTasks);&lt;br /&gt;            var t3 = Task.Factory.StartNew(ConsumeTasks);&lt;br /&gt;            var t4 = Task.Factory.StartNew(ConsumeTasks);&lt;br /&gt;&lt;br /&gt;            Task.WaitAll(t1, t2, t3, t4);&lt;br /&gt;&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"ALL DONE"&lt;/span&gt;);&lt;br /&gt;            Console.ReadLine();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ProduceTasks()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 1; i &amp;lt;= 10; i++)&lt;br /&gt;            {&lt;br /&gt;                Thread.Sleep(1000);&lt;br /&gt;                var qo = &lt;span class="kwrd"&gt;new&lt;/span&gt; QueueObject&lt;br /&gt;                             {&lt;br /&gt;                                 StartTime = DateTime.Now,&lt;br /&gt;                                 Id = i,&lt;br /&gt;                                 DecimalPlaces = 25000,&lt;br /&gt;                                 ProducingThreadId = Thread.CurrentThread.ManagedThreadId&lt;br /&gt;                             };&lt;br /&gt;                bc.Enqueue(qo);&lt;br /&gt;                Console.WriteLine(&lt;span class="str"&gt;"Enqueued: "&lt;/span&gt; + qo.Id + &lt;span class="str"&gt;"\t"&lt;/span&gt; + qo.ProducingThreadId + &lt;span class="str"&gt;"\t"&lt;/span&gt; + qo.StartTime.ToLongTimeString() + &lt;span class="str"&gt;"\t"&lt;/span&gt; + qo.DecimalPlaces);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ConsumeTasks ()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;try&lt;/span&gt;&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="rem"&gt;// Consume bc&lt;/span&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;while&lt;/span&gt; (&lt;span class="kwrd"&gt;true&lt;/span&gt;)&lt;br /&gt;                {&lt;br /&gt;                    QueueObject qo;&lt;br /&gt;                    &lt;span class="kwrd"&gt;if&lt;/span&gt; (bc.TryDequeue(&lt;span class="kwrd"&gt;out&lt;/span&gt; qo))&lt;br /&gt;                    {&lt;br /&gt;                        qo.ConsumingThreadId = Thread.CurrentThread.ManagedThreadId;&lt;br /&gt;                        qo.Answer = CalculatePi.Run(qo.DecimalPlaces);&lt;br /&gt;                        qo.EndTime = DateTime.Now;&lt;br /&gt;                        Console.WriteLine(&lt;span class="str"&gt;"Dequeued: "&lt;/span&gt; + qo.Id + &lt;span class="str"&gt;"\t"&lt;/span&gt; + qo.ConsumingThreadId + &lt;span class="str"&gt;"\t"&lt;/span&gt; + qo.EndTime.ToLongTimeString() + &lt;span class="str"&gt;"\t"&lt;/span&gt; + qo.DecimalPlaces);&lt;br /&gt;                    }&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;            &lt;span class="kwrd"&gt;catch&lt;/span&gt; (InvalidOperationException)&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="rem"&gt;// IOE means that Take() was called on a completed collection&lt;/span&gt;&lt;br /&gt;                Console.WriteLine(&lt;span class="str"&gt;"That's All!"&lt;/span&gt;);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In the main method, we are creating 4 tasks. 1 for producing (adding items to the queue) and 3 others for consuming (removing items from the queue). The StartNew() argument for Task takes an Action delegate item. By calling the WaitAll() method we direct the main thread to wait until all the threads have completed. We can also call the RunSynchronously() method on a task to make it run in the main thread rather than in a new thread.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In the ProduceTasks() method, we just line up 10 items in the queue. The QueueObject is created just to keep track of what the Queue is doing, so that we can display the details later.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In the ConsumeTasks() method, we call the TryDequeue() method to remove items from the queue. I use the QueueObject's DecimalPlaces property to calculate the value of pi. This gives me a real time consuming operation compared to using Thread.Sleep. You can download the pi calculation code from this link:&amp;nbsp;http://www.boyet.com/articles/picalculator.html&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Once the value of pi is calculated, we print the results on the console. The results are displayed below:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-tRlvnqSRH3U/TsBAfkv-ksI/AAAAAAAAB7o/LeTwseCqjGk/s1600/Capture.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-tRlvnqSRH3U/TsBAfkv-ksI/AAAAAAAAB7o/LeTwseCqjGk/s1600/Capture.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;As you can see, Main Thread id is 9, while the enque (Task1) happened on thread 10. The Deque operation happened (Task2, Task3, Task4) across 3 thread 11, 12 and 13. The 3rd column shows the end time and the 4th column shows the number of decimal places pi was calculated to.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Similar to this, we can implement ConcurrentStack with pop and push operations.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-2985423405759765898?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/2985423405759765898'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/2985423405759765898'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/11/task-and-concurrentqueue.html' title='Task and ConcurrentQueue'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-tRlvnqSRH3U/TsBAfkv-ksI/AAAAAAAAB7o/LeTwseCqjGk/s72-c/Capture.JPG' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-2042024593225401966</id><published>2011-10-30T10:17:00.001-04:00</published><updated>2011-10-30T10:19:17.457-04:00</updated><title type='text'>ASP.NET MVC Caching</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog post, I will document the caching mechanism provided in ASP.NET MVC3. MVC allows us to cache results from the whole controller or just some selected actions. It also provides parameters which help in configuring caching according to our needs.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The attribute used to enable caching is OutputCache. It can be applied to controllers or actions. Within OutputCache attribute we can specify the following arguments -&lt;/span&gt;&lt;br /&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Duration - in seconds to keep the cache.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Location - specifies where the content should be cached. Possible values -&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Any - default&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Client - stored on client browser. It should be used in case you have personalized content per user.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Downstream - any proxy servers or client browser.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Server - stored on web server. It should be used in case the content is not user dependent.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;None - no caching&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;ServerAndClient - caching on both locations&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Nostore - informs proxy servers and browsers to not store a permanent copy of cached content&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;VaryByParam - allows to cache based on different parameters. It can increase the size of you cache dramatically, so it should be carefully used. Possible values -&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;* - Create a different cache whenever any parameters change&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;none - never create different cached versions&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;parameters separated by commas - create a different cache whenever any parameters in this list change.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;CacheProfile - takes a string name for a profile named in config. The advantage of this is that its easier to manage all the cache settings in one place. Also, they can be changed without the need to recompile the application.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif; font-size: x-small;"&gt;&lt;i&gt;&lt;b&gt;MVC3 CacheProfile:&lt;/b&gt; In MVC3, CacheProfile doesn't work for child actions. In that scenario, we have to use the other arguments such as Duration.&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-2042024593225401966?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/2042024593225401966'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/2042024593225401966'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/10/aspnet-mvc-caching.html' title='ASP.NET MVC Caching'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-1382436726709833533</id><published>2011-09-15T20:55:00.003-04:00</published><updated>2011-09-15T21:30:26.581-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JQuery'/><category scheme='http://www.blogger.com/atom/ns#' term='HTML5'/><title type='text'>HTML 5 - Web Sockets and Microdata</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog, I will continue to look at the new features of HTML5. This time I will pay attention to web sockets and microdata. Read on to learn more.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Web Sockets&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;web sockets are a mechanism to provide full-duplex communication over a single TCP socket.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;A socket is basically a unique combination of local socket address, remote socket address and protocol (TCP, UDP).&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;socket address is combination of IP address and port number&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;HTTP by default is half-duplex i.e. at any given point of time, the communication can be only in one direction.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In case of web socket, client sends request to connect. Once the connection is made, client and server can communicate without the need to connect for every request. This comes in very handy for chat programs.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The other advantage of web sockets is performance. Normal HTTP headers can be 100s of bytes long but socket header is just around 2 bytes of data.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;To use sockets in HTML5 is pretty easy. Once you have a server which allows for sockets, we can write the code as shown below to start web socket communication.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre class="csharpcode"&gt;// web socket&lt;br /&gt;    $('#logWebSocket').append("&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Connecting to web server...&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;");&lt;br /&gt;    var server = new WebSocket('ws://localhost:8181/server');&lt;br /&gt;    server.addEventListener("message", msgWebSockHandler, false);&lt;br /&gt;    server.addEventListener("open", openWebSockHandler, false);&lt;br /&gt;    server.addEventListener("close", closeWebSockHandler, false);&lt;br /&gt;    server.addEventListener("error", errorWebSockHandler, false);&lt;br /&gt;&lt;br /&gt;    function msgWebSockHandler(e) {&lt;br /&gt;        $('#logWebSocket').append("&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Server Says: " + e.data + "&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;");&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    function openWebSockHandler(e) {&lt;br /&gt;        $('#logWebSocket').append("&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Connected to web server&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;");&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    function closeWebSockHandler(e) {&lt;br /&gt;        $('#logWebSocket').append("&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Disconnected from web server&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;");&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    function errorWebSockHandler(e) {&lt;br /&gt;        $('#logWebSocket').append("&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Error: " + e + "&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;");&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    $('#inputSocket').change(function (e) {&lt;br /&gt;        server.send(this.value);&lt;br /&gt;    });&lt;/pre&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this code, first, we get a new socket by doing a new on WebSocket and provide it the server address. After that, we register event handlers for message, open, close and error. As you can see in the code, I am just outputting whatever data was sent back by the server. In the last piece of code, &amp;nbsp;I have an input text box on the page. Everytime, the text changes in the text box, jquery sends the text to the server.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;To create the server, I used sample code from nugget.codeplex.com. This is a very basic developement server. For production environments, something more robust should be used. Here is the image of server -&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-Q3sFTV8zSQc/TnKae7wWr2I/AAAAAAAAB7M/BrdJHh5F6lw/s1600/1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-Q3sFTV8zSQc/TnKae7wWr2I/AAAAAAAAB7M/BrdJHh5F6lw/s1600/1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The client looks something like this -&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-FzHuYrKQck4/TnKaxutvxeI/AAAAAAAAB7Q/y9bsDg1u8p0/s1600/2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-FzHuYrKQck4/TnKaxutvxeI/AAAAAAAAB7Q/y9bsDg1u8p0/s1600/2.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Microdata&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Microdata is an HTML5 specification which helps machines to read and understand the data. It allows to put semantics within existing content on web pages.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Search engines, web crawlers, browsers look for this data to provide other better user experience such as search results, snippets etc.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;A collection of commonly used microdata vocabularies is located at http://data-vocabulary.org. This includes Person, Product, Offer, Event etc.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Some of the keywords used when describing microdata are -&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;itemscope - defines a container. For ex, if you put itemscope in a div element, that means that the descendants of this div element are going to contain the information about it.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;itemtype - defines the url to the vocabulary. It tells what kind of data is contained in this itemscope.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;itemprop - indicates that it holds the value of specified item property.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;itemid - to uniquely identify at the scope level&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-1382436726709833533?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/1382436726709833533'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/1382436726709833533'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/09/html-5-web-sockets-and-microdata.html' title='HTML 5 - Web Sockets and Microdata'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-Q3sFTV8zSQc/TnKae7wWr2I/AAAAAAAAB7M/BrdJHh5F6lw/s72-c/1.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-1297314652645886558</id><published>2011-09-05T22:00:00.001-04:00</published><updated>2011-09-10T14:49:08.340-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JQuery'/><category scheme='http://www.blogger.com/atom/ns#' term='HTML5'/><title type='text'>HTML5 - Web Storage and Offline Capabilities</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog, I will try to understand the new concepts with HTML5, namely, web storage which allows storing data on client side easier and offline capabilities which allow web apps to function even if they are offline.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Web Storage&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;There are 2 types of storage capabilities that we can use&lt;/span&gt;&lt;br /&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Local&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;stored forever (until we delete either through app or clear temp files)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;similar to cookies&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;accessed through window.localStorage property&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;is a collection of key-value pairs&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;can be removed using window.localStorage.removeItem(key) function&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Whole storage can be cleared by using the window.localStorage.clear() function&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The amount of storage used can be tracked by using the window.localStorage.length property&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The amount of storage space remaining can be tracked by using the window.localStorage.remainingSpace property&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If the space is exceeded, an exception of type QUOTA_EXCEEDED_ERR is thrown&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Session&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;similar to local but data is lost once the session is over i.e, user closes the browser or user session ends.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;accessed through window.sessionStorage property&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;is a collection of key-value pairs&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;can be removed using window.sessionStorage.removeItem(key) function&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Whole session can be cleared by using the window.sessionStorage.clear() function&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The amount of storage used can be tracked by using the window.sessionStorage.length property&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The amount of storage space remaining can be tracked by using the window.sessionStorage.remainingSpace property&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If the space is exceeded, an exception of type QUOTA_EXCEEDED_ERR is thrown&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The storage component of HTML5 also exposes an event whenever any storage functionality happens. This event can be tapped into by using the window.addEventListener("storage", function (e) { // do something }). The argument e provides access to which key fired the event, old value, new value etc.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;// storage event&lt;br /&gt;    window.addEventListener("storage", function (e) {&lt;br /&gt;        alert("storage event fired. Key = " + e.key +&lt;br /&gt;            "\nNew Value: " + e.newValue +&lt;br /&gt;            "\nOld Value: " + e.oldValue +&lt;br /&gt;            "\nURL: " + e.url +&lt;br /&gt;            "\nStorage Area: " + e.storageArea);&lt;br /&gt;    }, false);&lt;br /&gt;&lt;br /&gt;    // local storage&lt;br /&gt;    if (localStorage.PageCount)&lt;br /&gt;        localStorage.PageCount = Number(localStorage.PageCount) + 1;&lt;br /&gt;    else&lt;br /&gt;        localStorage.PageCount = 1;&lt;br /&gt;    $('#locStore').val(localStorage.PageCount);&lt;br /&gt;&lt;br /&gt;    if (localStorage.SomeValue)&lt;br /&gt;        $('#locStore1').val(localStorage.SomeValue);&lt;br /&gt;&lt;br /&gt;    $('#btnSaveLocStore1').click(function () {&lt;br /&gt;        localStorage.SomeValue = $('#locStore1').val();&lt;br /&gt;    });&lt;br /&gt;&lt;br /&gt;    $('#btnDelLocStore1').click(function () {&lt;br /&gt;        localStorage.removeItem("SomeValue");&lt;br /&gt;        $('#locStore1').val("");&lt;br /&gt;    });&lt;br /&gt;&lt;br /&gt;    // session storage&lt;br /&gt;    if (sessionStorage.PageCount)&lt;br /&gt;        sessionStorage.PageCount = Number(sessionStorage.PageCount) + 1;&lt;br /&gt;    else&lt;br /&gt;        sessionStorage.PageCount = 1;&lt;br /&gt;&lt;br /&gt;    $('#sessStore').val(sessionStorage.PageCount);&lt;/pre&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Here, I am providing some jQuery code which explains how to use the storage capabilities of HTML5. The first part is listening to the storage event. Currently, this only works in IE9. Afterwards, I use the local storage to keep track of page counts on this machine and also a user-defined value. I also provide a delete button to delete the local storage data. After that, there is a similar example for session storage. But that data is lost once you close the browser. The local storage data persists even if you close the browser. Here, is an image of the HTML form.&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-iQKsQP9OqIk/TmVrkIHCM4I/AAAAAAAAB7A/fe051cKSyKU/s1600/WebStorage.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-iQKsQP9OqIk/TmVrkIHCM4I/AAAAAAAAB7A/fe051cKSyKU/s1600/WebStorage.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Offline&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;HTML5 provides capabilities to control a web app even when the user is offline. HTML5 provides the developer some easy rules to work with so that they can create apps that work when the user is offline and then once the user comes online, it can do some work to make the data sync with the server. Following are some of the pointers to get an offline app working -&lt;/span&gt;&lt;br /&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Any app that should work offline should have a manifest file defined. It is this file that HTML5 looks for to provide offline functionality.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The rules for manifest file are as below -&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;first line in this file must be CACHE MANIFEST&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;second line identifies the version. This line is used to identify whether the version of files has been changed or not.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;second section starts with the word CACHE&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;here we provide the list of all files that we want to be cached.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;It is these files that get downloaded and stored on the client side when any change in the version is detected&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;if any one of the file download fails, HTML5 treats as the whole operation has failed.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;third section is NETWORK&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;here we provide an exclusion list. This is a list of files that should never be cached.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;fourth section is FALLBACK&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;here you can define a map to tie up online and offline pages. For ex. if you want to show an ABC page when you are online and an ABC-fallback page when you are offline, you can do so by providing a mapping here similar to /ABC /ABC-fallback&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;we also need to make sure that the ContentType of manifest file is set to "text/cache-manifest" and ResponseEncoding is set to "utf-8"&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;the offline api can be accessed using window.applicationCache property&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;there are few browser events that we can tap into as well. These events are called at certain stages -&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;First time loading&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Checking (window.applicationCache.onchecking)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Downloading (window.applicationCache.ondownloading)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Progress&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Cached (window.applicationCache.oncached)&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;subsequent loads (manifest hasn't changed)&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Checking&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;No Update (window.applicationCache.onnoupdate)&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;subsequent loads (manifest has changed)&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;same as first time loading&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;at any given time, we can have the following cache states&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;0 - uncached&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;1 - idle&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;2 - checking&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;3 - downloading&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;4 - updateready (window.applicationCache.onupdateready) - this happens when download has been successful and browser can be notified that the app is ready to be reloaded.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;5 - obsolete (window.applicationCache.onobsolete)&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If we want to provide a manual action to update the application, we just need to call location.reload()&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;To do an automatic update we just need to call location.reload() in onupdateready() to trigger the reload.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-1297314652645886558?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/1297314652645886558'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/1297314652645886558'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/09/html5-web-storage-and-offline.html' title='HTML5 - Web Storage and Offline Capabilities'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-iQKsQP9OqIk/TmVrkIHCM4I/AAAAAAAAB7A/fe051cKSyKU/s72-c/WebStorage.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-2716353269336296373</id><published>2011-09-04T21:30:00.001-04:00</published><updated>2011-09-04T21:33:25.901-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JQuery'/><category scheme='http://www.blogger.com/atom/ns#' term='HTML5'/><title type='text'>HTML 5 GeoLocation Example</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog, I will try to create a simple sample which uses the new geo location feature of HTML5. Read on -&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The HTML5 geo location feature is very simple to use. In a nutshell, you call a method which returns a position object. This position object contains the position information such as&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;lattitude&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;longitude&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;accuracy (in metres)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;altitude&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;altitude accuracy (in metres)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;heading - direction in which you are moving&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;speed - speed with which you are moving&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The sample code below shows how this method is called and how the values are populated in simple text boxes.&lt;/span&gt;&lt;/div&gt;&lt;pre class="csharpcode"&gt;    $('#btnGeoLoc').click(function () {&lt;br /&gt;        navigator.geolocation.getCurrentPosition(function (position) {&lt;br /&gt;            $('#lat').val(position.coords.latitude);&lt;br /&gt;            $('#long').val(position.coords.longitude);&lt;br /&gt;            $('#accu').val(position.coords.accuracy);&lt;br /&gt;            $('#alt').val(position.coords.altitude);&lt;br /&gt;            $('#hdng').val(position.coords.heading);&lt;br /&gt;            $('#speed').val(position.coords.speed);&lt;br /&gt;&lt;br /&gt;            $('#viewMap').attr("href", "http://maps.google.com/maps?q=" +&lt;br /&gt;                    $('#lat').val() + ",+" + $('#long').val() +&lt;br /&gt;                    "+(You+are+here!)&amp;amp;iwloc=A&amp;amp;hl=en");&lt;br /&gt;&lt;br /&gt;        },&lt;br /&gt;        function () {&lt;br /&gt;            alert("Error occurred");&lt;br /&gt;        });&lt;br /&gt;    });&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Here, I am showing the code where there is a button on whose click I use some jQuery events to populate the textboxes. In the end, I enable a "view map" anchor tag with a link to google maps by using the latitude and longitude that we just obtained.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The getCurrentPosition method takes 2 arguments. Both are functions. The first one will get if the call is successful and the second one will get called if there was an error. We can then go through the error code to see what kind of error occurred and take subsequent action.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this example, we used the getCurrentPosition() method. This method is used to get the position once. If you want to continuously monitor your position, you can use the watchPosition() method.&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-2716353269336296373?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/2716353269336296373'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/2716353269336296373'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/09/html-5-geolocation-example.html' title='HTML 5 GeoLocation Example'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-3378551671784146099</id><published>2011-08-29T23:01:00.007-04:00</published><updated>2011-09-04T00:39:23.941-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JQuery'/><category scheme='http://www.blogger.com/atom/ns#' term='CSS'/><title type='text'>JQuery Cheatsheet</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;To be good at JQuery, it is very important to understand and know how to select the HTML DOM elements. In this blog, I just wanted to provide some examples (more on the lines of a cheat sheet) of JQuery selectors which I use most often. I also wanted to document some of the methods that I use most of the times. I didn't want to go through JQuery documentation all the time. As I said, this list is not comprehensive but these are the items I use the most.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Selectors&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;/div&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$&lt;/b&gt; is same as jquery.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('#id')&lt;/b&gt; selects the element with id = id&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('p')&lt;/b&gt; selects all &amp;lt;p&amp;gt; elements&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('p,a,span')&lt;/b&gt; selects all paragraphs, anchor and span tags&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('ancestor descendants')&lt;/b&gt; selects all the descendants of ancestor. For ex. $('table tr') selects all tr tags in the table element. Descendants are children, grandchildren etc of ancestor.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('.classname')&lt;/b&gt; selects all elements which have class = classname&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('.classA,.classB')&lt;/b&gt; selects all elements which have either classA or classB&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('p.classA')&lt;/b&gt; selects only &amp;lt;p&amp;gt; tags which have class = classA&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('a[title]') &lt;/b&gt;selects all &amp;lt;a&amp;gt; tags which have a title attribute&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('a[title="somelink"]')&lt;/b&gt; selects all &amp;lt;a&amp;gt; tags which have title attribute = somelink&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$(':input')&lt;/b&gt; selects all input elements including input, textarea, select, button, image, radio, checkboxes etc. $('input') will select only &amp;lt;input&amp;gt; elements. Textarea is also input element but its written like &amp;lt;TextArea&amp;gt; that's why :input is handy.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('div:contains("internet")')&lt;/b&gt; selects all the div elements whose value contains the text "internet"&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('tr:odd')&lt;/b&gt; and &lt;b&gt;$('tr:even')&lt;/b&gt; selects all the odd and even rows respectively.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('tr:first-child')&lt;/b&gt; and &lt;b&gt;$('tr:last-child')&lt;/b&gt; selects the first and the last row respectively&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$(input[value^="internet"])&lt;/b&gt; selects all inputs whose value attribute STARTS with the word "internet"&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$(input[value$="internet"])&lt;/b&gt; selects all inputs whose value attribute ENDS with the word "internet"&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$(input[value*="internet"])&lt;/b&gt; selects all inputs whose value attribute CONTAINS the word "internet"&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('input', $('#mainDivs'))&lt;/b&gt; will find all inputs inside the mainDivs element&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('div:eq(0)', $('#mainDivs'))&lt;/b&gt; will find the first div inside the mainDivs element&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Methods&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;.each(function(index, Element))&lt;/b&gt; - loops through each element. It also provides the index of the item being looped. The current item can be referenced by using $(this) or using $(Element) where Element is the input passed. Usually we dont need to use $(Element) as $(this) does the job. So mostly the usage of each is like .each(function(index)).&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;.html()&lt;/b&gt; - gets the html.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;.html(htmlString)&lt;/b&gt; - sets the html of the referenced element by htmlString.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;.attr("id")&lt;/b&gt; - gets the value of the attribute id.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;.attr("id","5")&lt;/b&gt; - sets the value of attribute id to 5.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;.attr({"title":"some title", "alt":"5"})&lt;/b&gt; - sets multiple attributes using JSON object. Text inside {} is a JSON object.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('.phone').append("&amp;lt;br/&amp;gt;Ph: 123-456-7890")&lt;/b&gt; will APPEND the phone number to all elements which have class phone.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('.phone').prepend("Ph: 123-456-7890&amp;lt;br/&amp;gt;")&lt;/b&gt; will PREPEND the phone number to all elements which have class phone.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('.phone').wrap("&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;")&lt;/b&gt; will WRAP the div around all elements which have class phone.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('.phone').unwrap()&lt;/b&gt; will UNWRAP the parent around all elements which have class phone.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('.classA').css("border", "2px red solid")&lt;/b&gt; will apply border style with value 2px red and solid on all elements which have class = classA&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('.classA').css({"border":"2px red solid","font-size":"16px"})&lt;/b&gt; will apply multiple styles (border and font-size) to elements with classA using JSON object&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;val()&lt;/b&gt; - gets the value of the element&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;val(str)&lt;/b&gt; - sets the value of the element to str&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('#t1').addClass("classexample")&lt;/b&gt; - adds the class classexample to element id #t1&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('#t1').removeClass("classexample")&lt;/b&gt; - removes the class classexample from element id #t1&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('#t1').toggleClass("highlight")&lt;/b&gt; - toggles the class i.e if class is applied, it removes it, if its not there, it adds it&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('#id').clone()&lt;/b&gt; - clones the id element&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Events&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('#id').click(function(){ // do something })&lt;/b&gt; will register a click event to element with id = id&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('#id').change(function(){ // do something })&lt;/b&gt; change is mostly used for &amp;lt;select&amp;gt; elements. It gets fired &amp;nbsp;everytime selection is changed. Inside the function $(this) will give you the changed value. change also works with textarea and input also.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('#id').mouseenter(function(){ // do something}) &lt;/b&gt;is for mouseenter&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('#id').mouseleave(function(){ // do something})&lt;/b&gt; is for mouseleave&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('#id').bind("mousenter", mouseEnterFunc)&lt;/b&gt; - binds the mouseenter event to item with id = id.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('#id').bind("mouseenter mouseleave mouseup", bindingExample)&lt;/b&gt; - bind's main advantage is that it allows you to bind multiple events in a simple manner. Then in the bindingExample method, you can use e.type to see which event is called in case you want to do different things.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('#id').bind({mouseenter:funcenter, mouseleave:funcleave, mouseup:funcup})&lt;/b&gt; - will do the same thing as above but in JSON object&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('#id').unbind()&lt;/b&gt; - unbinds all the events associated with id&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('#id').unbind("mouseenter")&lt;/b&gt; - unbinds only the event specified&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('#id').delegate('input','keypress' function() { // do something }) &lt;/b&gt;- bind and delegate are similar. the only difference is that when you attach events through bind, any elements added through code or after load, the event will not get attached to these new elements. If you use delegate, it will keep on monitoring and even if new elements are added, it will attach the event handler. In this example, for anything under the context of id, a keypress event is attached to all inputs. When keypress happens, function will get called. You can use the undelegate function to get rid of event handlers.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('#id').hover(func1, func2)&lt;/b&gt; - func1 is mouseenter and func2 is mouseleave.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('#id').hover(func1)&lt;/b&gt; - func1 will be used as mouseenter and mouseleave. Its a good place to use toggleClass method.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('#id').toggle(func1, func2, callback)&lt;/b&gt; - when you click id, first func1 will get called, at 2nd click func2 will get called. In the end callback will be called. This is good place to add some class in func1 add a different class in func2 and then remove the classes in callback. 4th click will then again call func1.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;$('#id').toggle() &lt;/b&gt;- hides or shows the element. You can also provide a boolean for show and hide or duration and callback for it to fade out etc.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;div&gt;&lt;b&gt;AJAX Functions&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;b&gt;$('#id').load()&lt;/b&gt; - loads HTML data from the server. It provides capabilities to get parts of html by providing selectors or if the page takes arguments, we can send the arguments as well using JSON object. It also provides a call back function after the load is completed.&lt;/li&gt;&lt;li&gt;&lt;b&gt;$.get(url,data,callback,type)&lt;/b&gt; - fires a get request. data is data sent to the server. callback contains the data returned from server apart from other arguments such as textStatus and jqXHR which is jquery XML Http Request object. type is the type of data server will return (HTML, XML, JSON)&lt;/li&gt;&lt;li&gt;&lt;b&gt;$.getJson(ur, data, callback) &lt;/b&gt;- similar to get with type = JSON.&lt;/li&gt;&lt;li&gt;&lt;b&gt;$.post(url,data,callback,type)&lt;/b&gt; - fires a post request. rest is same as get.&lt;/li&gt;&lt;li&gt;&lt;b&gt;$.ajax({//settings json object})&lt;/b&gt; - the low level api. Load, Get, Post all of them ultimately fire ajax function. ajax function arguments can be provided in the form of a json object and it provides more control with what we want to do with ajax calls.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;CSS Selectors&lt;/b&gt;&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;ul style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;li&gt;&lt;b&gt;#id&lt;/b&gt; word following after # is the id of the element to which css can be applied&lt;/li&gt;&lt;li&gt;&lt;b&gt;.class&lt;/b&gt; word following after . is the class of elements to which css can be applied&lt;/li&gt;&lt;/span&gt;&lt;/ul&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Other Notes&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;ul style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;li&gt;&lt;b&gt;Document.Ready&lt;/b&gt; is called when the page is loaded. i.e HTML DOM is loaded. It may mean that all images have not been loaded yet but DOM is loaded.&lt;/li&gt;&lt;li&gt;keyword &lt;b&gt;this &lt;/b&gt;represents the raw DOM object while $(this) represents the raw DOM object wrapped inside jquery. Therefore, $(this) has jquery methods available to it while this does not. this has access to raw properties though.&lt;/li&gt;&lt;li&gt;&lt;b&gt;CDN benefits&lt;/b&gt; - faster, cached, 99.9% uptime. Good for internet apps. For Intranet better to have your own copy.&lt;/li&gt;&lt;/span&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt; &lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-3378551671784146099?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/3378551671784146099'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/3378551671784146099'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/08/jquery-selectors-cheatsheet.html' title='JQuery Cheatsheet'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-5015268014462548412</id><published>2011-07-16T03:24:00.000-04:00</published><updated>2011-09-29T09:06:19.053-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='Entity Framework'/><title type='text'>EF Code First Example</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Entity Framework (EF) recently started supporting code-first development. This means that we don't have to create our database first before we can write any code. It allows developers to create their models (business entities, data objects etc) and then it will automatically create the database after very little configuration. I wanted to see how EF handles relationships. So I created this sample to demonstrate how EF code first handles many-to-many relationship.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;For my app, the scenario is very simple. We have Person and Address entities. A person can have many addresses and an Address can belong to many persons. So, its a many to many relationship. To get up and running with an app, all the code I needed to write was this.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Data.Entity;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Linq;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Text;&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CodeFirst&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;class&lt;/span&gt; Program&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;        {&lt;br /&gt;            var ctx = &lt;span class="kwrd"&gt;new&lt;/span&gt; CodeFirstContext();&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;//add few persons&lt;/span&gt;&lt;br /&gt;            ctx.Persons.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Person { Name = &lt;span class="str"&gt;"Lionel Messi"&lt;/span&gt; });&lt;br /&gt;            ctx.Persons.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Person { Name = &lt;span class="str"&gt;"Rafael Nadal"&lt;/span&gt; });&lt;br /&gt;            ctx.SaveChanges();&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// add few addresses&lt;/span&gt;&lt;br /&gt;            ctx.Addresses.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Address {City = &lt;span class="str"&gt;"Barcelona"&lt;/span&gt;});&lt;br /&gt;            ctx.SaveChanges();&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;//get Messi&lt;/span&gt;&lt;br /&gt;            var person = ctx.Persons.Where(p =&amp;gt; p.Name.StartsWith(&lt;span class="str"&gt;"Lionel"&lt;/span&gt;)).SingleOrDefault();&lt;br /&gt;            var addr = ctx.Addresses.Where(a =&amp;gt; a.City == &lt;span class="str"&gt;"Barcelona"&lt;/span&gt;).SingleOrDefault();&lt;br /&gt;            person.Addresses = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Address&amp;gt;();&lt;br /&gt;            person.Addresses.Add(addr);&lt;br /&gt;            person.Addresses.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Address{City = &lt;span class="str"&gt;"Mallorca"&lt;/span&gt;});&lt;br /&gt;            ctx.SaveChanges();&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// get Nadal&lt;/span&gt;&lt;br /&gt;            person = ctx.Persons.Where(p =&amp;gt; p.Name.StartsWith(&lt;span class="str"&gt;"Rafael"&lt;/span&gt;)).SingleOrDefault();&lt;br /&gt;            addr = ctx.Addresses.Where(a =&amp;gt; a.City == &lt;span class="str"&gt;"Mallorca"&lt;/span&gt;).SingleOrDefault();&lt;br /&gt;            person.Addresses = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Address&amp;gt;();&lt;br /&gt;            person.Addresses.Add(addr);&lt;br /&gt;            ctx.SaveChanges();&lt;br /&gt;           &lt;br /&gt;            &lt;span class="rem"&gt;//get all&lt;/span&gt;&lt;br /&gt;            var persons = ctx.Persons.Include("Addresses");&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;foreach&lt;/span&gt;(var p &lt;span class="kwrd"&gt;in&lt;/span&gt; persons)&lt;br /&gt;            {&lt;br /&gt;                Console.WriteLine(&lt;span class="str"&gt;"\n"&lt;/span&gt; + p.Id + &lt;span class="str"&gt;"   "&lt;/span&gt; + p.Name);&lt;br /&gt;                &lt;span class="kwrd"&gt;foreach&lt;/span&gt;(var a &lt;span class="kwrd"&gt;in&lt;/span&gt; p.Addresses)&lt;br /&gt;                    Console.WriteLine(&lt;span class="str"&gt;"\t"&lt;/span&gt; + a.Id + &lt;span class="str"&gt;"    "&lt;/span&gt; + a.City);&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            Console.Read();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Person&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Id { get; set; }&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; List&amp;lt;Address&amp;gt; Addresses { get; set; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Address&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Id { get; set; }&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; City { get; set; }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; List&amp;lt;Person&amp;gt; Persons { get; set; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CodeFirstContext : DbContext&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; DbSet&amp;lt;Person&amp;gt; Persons { get; set; }&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; DbSet&amp;lt;Address&amp;gt; Addresses { get; set; }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;I also needed to make a small entry for connection string in the app.config as shown below.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;="1.0"&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;="utf-8"&lt;/span&gt; ?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;configuration&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;connectionStrings&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;add&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="CodeFirstContext"&lt;/span&gt; &lt;br /&gt;         &lt;span class="attr"&gt;connectionString&lt;/span&gt;&lt;span class="kwrd"&gt;="Data Source=|DataDirectory|CodeFirst.sdf"&lt;/span&gt;&lt;br /&gt;         &lt;span class="attr"&gt;providerName&lt;/span&gt;&lt;span class="kwrd"&gt;="System.Data.SqlServerCe.4.0"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;connectionStrings&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;configuration&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In the database, you can see that EF created Persons, Addresses and AddressPersons table (junction table) to achieve many-to-many relationship.&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-Xnp8QQNHkqQ/Tn4uFIobw2I/AAAAAAAAB7U/849FOgXJcn8/s1600/1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-Xnp8QQNHkqQ/Tn4uFIobw2I/AAAAAAAAB7U/849FOgXJcn8/s1600/1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If you see the code, all the code in main function is just trying to do CRUD operations to the DB. But to get the DB up and running we needed to write very little code. Also, for this code to work you will have to get the latest EF Code First package using Nuget. The output of this program is shown below -&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-w2gcQYaroeQ/Tn4uHvr-9aI/AAAAAAAAB7Y/OlubzMbhUiM/s1600/2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-w2gcQYaroeQ/Tn4uHvr-9aI/AAAAAAAAB7Y/OlubzMbhUiM/s1600/2.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-5015268014462548412?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/5015268014462548412'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/5015268014462548412'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/09/ef-code-first-example.html' title='EF Code First Example'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/-Xnp8QQNHkqQ/Tn4uFIobw2I/AAAAAAAAB7U/849FOgXJcn8/s72-c/1.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-7373459702230962189</id><published>2011-05-27T20:26:00.001-04:00</published><updated>2011-05-27T20:27:25.946-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Rx'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Observer pattern'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>.NET Reactive Extensions Rx - 3 (Cleanup)</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog, I will create a small app to describe how to perform cleanup after the Rx subscription has finished its work.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Let's take a look at the code first -&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; A03CleanupEx&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Run()&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"Thread: {0}\tMain application thread"&lt;/span&gt;, Thread.CurrentThread.ManagedThreadId);&lt;br /&gt;            var oSeq = Observable.Using&amp;lt;&lt;span class="kwrd"&gt;char&lt;/span&gt;, MyStreamReader&amp;gt;(&lt;br /&gt;                () =&amp;gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; MyStreamReader(&lt;span class="kwrd"&gt;new&lt;/span&gt; FileStream(&lt;span class="str"&gt;"chars.txt"&lt;/span&gt;, FileMode.Open)),&lt;br /&gt;                sr =&amp;gt; sr.ReadToEnd().ToCharArray().Select(c=&amp;gt;c).ToObservable(Scheduler.NewThread).Finally(FinalStep)&lt;br /&gt;                );&lt;br /&gt;&lt;br /&gt;            oSeq.Subscribe(Print);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; FinalStep()&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"Thread: {0}\tFinally getting executed"&lt;/span&gt;, Thread.CurrentThread.ManagedThreadId);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Print(&lt;span class="kwrd"&gt;char&lt;/span&gt; c)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"Thread: {0}\t{1}"&lt;/span&gt;, Thread.CurrentThread.ManagedThreadId,c);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MyStreamReader : StreamReader&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; MyStreamReader(Stream fs) :&lt;span class="kwrd"&gt;base&lt;/span&gt;(fs)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"Thread: {0}\tStream Initialized"&lt;/span&gt;, Thread.CurrentThread.ManagedThreadId);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Dispose(&lt;span class="kwrd"&gt;bool&lt;/span&gt; disposing)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"Thread: {0}\tStream Disposed"&lt;/span&gt;, Thread.CurrentThread.ManagedThreadId);&lt;br /&gt;            &lt;span class="kwrd"&gt;base&lt;/span&gt;.Dispose(disposing);&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;and here are the results -&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-s3e0mIr0gSA/TeBAYUdCCOI/AAAAAAAAB3c/t2ywJQu_56Y/s1600/1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-s3e0mIr0gSA/TeBAYUdCCOI/AAAAAAAAB3c/t2ywJQu_56Y/s1600/1.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Rx provides 2 ways of performing cleanup - using and finally. It is a good practice to cleanup the resources you might have used once you are done so that the streams are closed, the threads are returned back to thread pool etc.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Using&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Rx framework provides Observable.Using to achieve this. We templatize the Using by providing first the type that will be the output once the subscription is run. In our case it is char. The second type is the type that you want to dispose once the operation is over. In our case it is MyStreamReader. I created MyStreamReader class just to tap into the constructor and the dispose method so that I can display some message on the console. Other than that, this class is the same as StreamReader. The Using method takes 2 arguments and both of them are delegates. The first delegate's output must be an object that you want to dispose. In our case, its type would be MyStreamReader. The second delegate than takes this object (sr) and should output an IObservable&amp;lt;T&amp;gt; where T in our case is char. The using method itself outputs an IObservable&amp;lt;T&amp;gt; where T in our case is char. This var can now be used to subscribe.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Finally&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In my previous blog also, I touched a little bit about the Finally method. Finally method is similar in nature to our common try-catch-finally constructs in that the code in Finally will always get executed, irrespective of whether the operation is successful or an error occurred. As you can see in the results, after the subscription was over, Finally was called and once Finally was finished, Dispose was called to dispose the MyStreamReader object.&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-7373459702230962189?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/7373459702230962189'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/7373459702230962189'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/05/net-reactive-extensions-rx-3-cleanup.html' title='.NET Reactive Extensions Rx - 3 (Cleanup)'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-s3e0mIr0gSA/TeBAYUdCCOI/AAAAAAAAB3c/t2ywJQu_56Y/s72-c/1.JPG' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-8004854542094065630</id><published>2011-05-25T01:08:00.013-04:00</published><updated>2011-05-26T12:13:39.330-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Rx'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Observer pattern'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>.NET Reactive Extensions Rx - 2 (Concurrency)</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog, I will further explore Rx framework and specifically look into the concurrency and threading enhancements because I think this is where most of the value for Rx is.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Rx provides a very fine-grained control to the developers to choose on what kind of threads they want to run their subscriptions and observations. We can say that subscriptions are basically the calls to get the next item in the observable collection and observations are what we do with that item once we have got it. So, if we are iterating through a collection of numbers, subscription means getting that number (through the query) and observation means doing the OnNext, OnError and OnCompleted operations on that number.&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Keep in mind that even if we use different threads, Rx guarantees that the order is maintained. That is, if the observable collection contains 1,2,3 and you do some Rx operations on this, it will be processed in the same order. Meaning that, operation on 2 wont be called until 1 is completed and so on and so forth. The only caveat to this is that Rx only guarantees the order of observations and doesn't guarantee the order for subscriptions.&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;To demonstrate threading scenarios, I have created a simple WPF app with a bunch of buttons that display some text in the underlying textbox. Since, there are cases in which I wanted to do the observation operations in different thread, it was not possible for me to append the text to textbox (as textbox is in the main thread). In that case I output my text to Debug-&amp;gt;Output window by using the PrintToDebug() method. Let's take a look at the code and its results -&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MainWindow : Window&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; MainWindow()&lt;br /&gt;        {&lt;br /&gt;            InitializeComponent();&lt;br /&gt;            Example8Stop.IsEnabled = &lt;span class="kwrd"&gt;false&lt;/span&gt;;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; PrintThreadId()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"Thread:"&lt;/span&gt; + Thread.CurrentThread.ManagedThreadId;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; CleanTB()&lt;br /&gt;        {&lt;br /&gt;            tb.Text = &lt;span class="str"&gt;"Main "&lt;/span&gt; + PrintThreadId() + &lt;span class="str"&gt;"\n"&lt;/span&gt;;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Print(&lt;span class="kwrd"&gt;string&lt;/span&gt; str)&lt;br /&gt;        {&lt;br /&gt;            tb.AppendText(str + &lt;span class="str"&gt;"\nObservation\tPrint\t\t"&lt;/span&gt; + PrintThreadId() +&lt;span class="str"&gt;"\n"&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PrintToDebug(&lt;span class="kwrd"&gt;string&lt;/span&gt; str)&lt;br /&gt;        {&lt;br /&gt;            Debug.WriteLine(str + &lt;span class="str"&gt;"\nObservation\t\tPrint\t\t"&lt;/span&gt; + PrintThreadId() + &lt;span class="str"&gt;"\n"&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; EndMethod()&lt;br /&gt;        {&lt;br /&gt;            tb.AppendText(&lt;span class="str"&gt;"\nEndMethod\t"&lt;/span&gt; + PrintThreadId() + &lt;span class="str"&gt;"\tTask is complete\n"&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ErrorMethod(Exception ex)&lt;br /&gt;        {&lt;br /&gt;            tb.AppendText(&lt;span class="str"&gt;"\nErrorMethod"&lt;/span&gt; + PrintThreadId() + &lt;span class="str"&gt;"\t"&lt;/span&gt; + ex.Message + &lt;span class="str"&gt;"\n"&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Example1_Click(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            CleanTB();&lt;br /&gt;            tb.AppendText(&lt;span class="str"&gt;"\nDemonstrating Scheduler.NewThread. See output in the debug-output window."&lt;/span&gt;);&lt;br /&gt;            var numbers = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; { 1, 2, 3 };&lt;br /&gt;            var query = numbers.Select(n =&amp;gt; SlowDown(n));&lt;br /&gt;&lt;br /&gt;            var oQuery = query.ToObservable(Scheduler.NewThread);&lt;br /&gt;            oQuery.Subscribe(PrintToDebug);&lt;br /&gt;        }&lt;br /&gt;       &lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Example2_Click(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            CleanTB();&lt;br /&gt;            tb.AppendText(&lt;span class="str"&gt;"\nDemonstrating SubscribeOn On NewThread."&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;            var numbers = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; { 1, 2, 3 };&lt;br /&gt;            var query = numbers.Select(n =&amp;gt; SlowDown(n));&lt;br /&gt;&lt;br /&gt;            var oQuery = query.ToObservable();&lt;br /&gt;            oQuery.SubscribeOn(Scheduler.NewThread).Subscribe(PrintToDebug); &lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Example3_Click(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            CleanTB();&lt;br /&gt;            tb.AppendText(&lt;span class="str"&gt;"\nDemonstrating ObserveOn on NewThread."&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;            var numbers = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; { 1, 2, 3 };&lt;br /&gt;            var query = numbers.Select(n =&amp;gt; SlowDown(n));&lt;br /&gt;&lt;br /&gt;            var oQuery = query.ToObservable();&lt;br /&gt;            oQuery.ObserveOn(Scheduler.NewThread).Subscribe(PrintToDebug);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Example4_Click(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            CleanTB();&lt;br /&gt;            tb.AppendText(&lt;span class="str"&gt;"\nDemonstrating SubscribeOn and ObserveOn on NewThread."&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;            var numbers = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; { 1, 2, 3 };&lt;br /&gt;            var query = numbers.Select(n =&amp;gt; SlowDown(n));&lt;br /&gt;&lt;br /&gt;            var oQuery = query.ToObservable();&lt;br /&gt;            oQuery.SubscribeOn(Scheduler.NewThread).ObserveOn(Scheduler.NewThread).Subscribe(PrintToDebug);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Example5_Click(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            CleanTB();&lt;br /&gt;            tb.AppendText(&lt;span class="str"&gt;"\nDemonstrating SubscribeOn and ObserveOn on ThreadPool."&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;            var numbers = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; { 1, 2, 3 };&lt;br /&gt;            var query = numbers.Select(n =&amp;gt; SlowDown(n));&lt;br /&gt;&lt;br /&gt;            var oQuery = query.ToObservable();&lt;br /&gt;            oQuery.SubscribeOn(Scheduler.ThreadPool).ObserveOn(Scheduler.ThreadPool).Subscribe(PrintToDebug);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Example6_Click(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            CleanTB();&lt;br /&gt;            tb.AppendText(&lt;span class="str"&gt;"\nDemonstrating SubscribeOn NewThread and ObserveOnDispatcher."&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;            var numbers = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; { 1, 2, 3 };&lt;br /&gt;            var query = numbers.Select(n =&amp;gt; SlowDown(n));&lt;br /&gt;&lt;br /&gt;            var oQuery = query.ToObservable();&lt;br /&gt;            oQuery.SubscribeOn(Scheduler.NewThread).ObserveOnDispatcher().Subscribe(Print);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Example7_Click(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            CleanTB();&lt;br /&gt;            tb.AppendText(&lt;span class="str"&gt;"\nDemonstrating SubscribeOnDispatcher and ObserverOn NewThread."&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;            var numbers = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; { 1, 2, 3 };&lt;br /&gt;            var query = numbers.Select(n =&amp;gt; SlowDown(n));&lt;br /&gt;&lt;br /&gt;            var oQuery = query.ToObservable();&lt;br /&gt;            oQuery.SubscribeOnDispatcher().ObserveOn(Scheduler.NewThread).Subscribe(PrintToDebug);&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Example8_Click(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            Example8.IsEnabled = &lt;span class="kwrd"&gt;false&lt;/span&gt;;&lt;br /&gt;            Example8Stop.IsEnabled = &lt;span class="kwrd"&gt;true&lt;/span&gt;;&lt;br /&gt;            CleanTB();&lt;br /&gt;            tb.AppendText(&lt;span class="str"&gt;"\nDemonstrating Finally and disposing the subscription"&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;            var numbers = Enumerable.Range(1, 100);&lt;br /&gt;            var query = numbers.Select(n =&amp;gt; SlowDown(n));&lt;br /&gt;&lt;br /&gt;            var oQuery = query.ToObservable()&lt;br /&gt;                .SubscribeOn(Scheduler.NewThread)&lt;br /&gt;                .ObserveOnDispatcher()&lt;br /&gt;                .Finally(FinalAction);&lt;br /&gt;            m_sub = oQuery.Subscribe(Print);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Example8Stop_Click(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            m_sub.Dispose();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; FinalAction()&lt;br /&gt;        {&lt;br /&gt;            tb.AppendText(&lt;span class="str"&gt;"\nFinal Action\t"&lt;/span&gt; + PrintThreadId());&lt;br /&gt;            Example8.IsEnabled = &lt;span class="kwrd"&gt;true&lt;/span&gt;;&lt;br /&gt;            Example8Stop.IsEnabled = &lt;span class="kwrd"&gt;false&lt;/span&gt;;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; SlowDown(&lt;span class="kwrd"&gt;int&lt;/span&gt; n)&lt;br /&gt;        {&lt;br /&gt;            Thread.Sleep(500);&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"\nSubscription\tSlowDown\t"&lt;/span&gt; + PrintThreadId() + &lt;span class="str"&gt;"\t\tValue:"&lt;/span&gt; + n;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; IDisposable m_sub;&lt;br /&gt;    }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;As you can see in this code SlowDown() method is part of subscription as it is part of the query that iterates to get the next item while Print() and PrintToDebug() methods are part of observations as they are the OnNext delegates.&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Example1&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-MR7vOTkuuqA/Td1tRV1nAsI/AAAAAAAAB28/nXlRy9UcoZg/s1600/Ex1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-MR7vOTkuuqA/Td1tRV1nAsI/AAAAAAAAB28/nXlRy9UcoZg/s1600/Ex1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In Example1, when we provide the Scheduler.NewThread argument to the ToObservable() method, it means that all the subscriptions (getting next items) should be performed on a new thread. So as you can see in the application that main thread id is 8 but subscriptions ran on thread 10. Since we didnot provide any such parameter for observations, they also ran on thread 10.&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Example2&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-3G6sE5zVmhw/Td1wBixqA-I/AAAAAAAAB3A/95jFdCeaAxg/s1600/Ex2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-3G6sE5zVmhw/Td1wBixqA-I/AAAAAAAAB3A/95jFdCeaAxg/s1600/Ex2.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Example2 does the same thing as Example1. The only difference is that instead of providing Scheduler.NewThread argument to the ToObservable() method, we are using the SubscribeOn(Scheduler.NewThread) to achieve the same result.&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Example3&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-lJrBIjl5i9k/Td1w2yayEPI/AAAAAAAAB3E/vT_FbSLorMA/s1600/Ex3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-lJrBIjl5i9k/Td1w2yayEPI/AAAAAAAAB3E/vT_FbSLorMA/s1600/Ex3.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In Example3, we utilize the ObserveOn() method. This tells the Rx framework that all observations (in our case the print methods) should run on a new thread. As you can see, the main thread is 10 and all subscriptions ran on thread 10 (since we didn't provide anything for subscriptions) while all observations ran on different new threads.&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Scheduler.NewThread for subscriptions means that run the subscription on any other thread other than the main thread. While, Scheduler.NewThread for observations means that run the observations on a completely new thread.&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Example4&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-C8a62UnbjSs/Td1xp6GbN5I/AAAAAAAAB3I/jHUI1wrhM8g/s1600/Ex4.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-C8a62UnbjSs/Td1xp6GbN5I/AAAAAAAAB3I/jHUI1wrhM8g/s1600/Ex4.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In Example4, we use both the SubscribeOn and ObserveOn methods. And as i said before that for subscriptions NewThread just means a different thread from the main thread while for observations, it means a completely NewThread, we can see the same in the results. All subscriptions ran on thread 13 which is different from thread 10 (main thread) while all the observations ran on a completely different new thread.&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Example5&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-cngPSTZ4hqg/Td1yhTV-77I/AAAAAAAAB3M/YJ-f2TKuJ7s/s1600/Ex5.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-cngPSTZ4hqg/Td1yhTV-77I/AAAAAAAAB3M/YJ-f2TKuJ7s/s1600/Ex5.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Example5 is similar to Example4 and the only difference is that instead of NewThread we are using ThreadPool. This means that instead of creating new threads, available and free threads from the pool will be used. The other difference between them is that NewThread creates the thread in foreground while ThreadPool threads are in the background. Foreground threads will make the main app wait until all their tasks are done while background threads will not execute if the main thread has finished its work. Since this time we used the ThreadPool, all observations ran on thread 11 which was available and free. If you run this code for large numbers you will see different threads being used again and again as and when they became free.&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Example6&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-qKDi8kgk4Sk/Td1zfeCd-7I/AAAAAAAAB3Q/9ESI8ZSM7yQ/s1600/Ex6.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-qKDi8kgk4Sk/Td1zfeCd-7I/AAAAAAAAB3Q/9ESI8ZSM7yQ/s1600/Ex6.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In example6, we are using subscribe on new thread but ObserveOnDispatcher(). This means that all observations should run on the dispatcher thread which is the main thread. It is for this reason, that we are able to print the results in this window rather than in the debug window. As you can see in the results, due to NewThread on subscriptions, they ran on a different thread (7) than main thread (9) while all the observations ran on the main thread (9).&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Example7&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-PFYnuhwdFu0/Td10FR4MmWI/AAAAAAAAB3U/kszY926Hqis/s1600/Ex7.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-PFYnuhwdFu0/Td10FR4MmWI/AAAAAAAAB3U/kszY926Hqis/s1600/Ex7.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In example7, we use the SubscribeOnDispatcher() while run the observations on a new thread. The results show that all subscriptions ran on the same thread (9) as the main thread while all observations ran on the new thread.&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Example8&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-WnGt5UszJVQ/Td2hZAiVnKI/AAAAAAAAB3Y/rO9m6Hvpdl0/s1600/Ex8.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-WnGt5UszJVQ/Td2hZAiVnKI/AAAAAAAAB3Y/rO9m6Hvpdl0/s1600/Ex8.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Finally, example 8 adds a few more important points the the example to make it complete. First thing to notice is that we have added a corresponding Ex 8 Stop button for Ex 8 button. This stop button allows us to stop the execution midway. To do this, first we declare a local IDisposable variable to keep its value outside the scope of the function. Then, when Ex 8 Stop is clicked, we just call the Dispose() on this variable, as shown in the click handler of Ex 8 Stop. The other thing to note here is that to perform some kind of cleanup, we have added a Finally method. This Finally method is called even if the execution is stopped or an error occurs or the execution completes successfully. For this reason, we couldn't use the OnCompleted to perform cleanup because OnCompleted will not get called if the execution is stopped mid-way or if an error occurs.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;There are few other helpful methods, which I won't go in detail, but they are useful. We can use Buffer and Window methods in cases where we need to work with multiple items rather than single item. So for ex, we can specify in the buffer method a count of 10. Then, in the subscription, instead of getting one item, we will get a list of 10 items. Both these methods also allow us to provide a timespan instead of a count. If we provide timespan than the buffer will contain as many items as it was able to fetch in that timespan. Buffer and Window work similarly and they only differ in their return type.&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-8004854542094065630?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/8004854542094065630'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/8004854542094065630'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/05/net-reactive-extensions-rx-2.html' title='.NET Reactive Extensions Rx - 2 (Concurrency)'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-MR7vOTkuuqA/Td1tRV1nAsI/AAAAAAAAB28/nXlRy9UcoZg/s72-c/Ex1.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-3009307524686876363</id><published>2011-05-23T01:59:00.004-04:00</published><updated>2011-05-24T18:24:33.502-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Rx'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Observer pattern'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>.NET Reactive Extensions Rx - 1 (Basics)</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog, I will explore the reactive extensions (Rx) framework. Rx extends .NET. Rx extends LINQ so that results of a query can be converted to an observable sequence. Let's explore more..&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;You can download Rx from the MSDN website. Once the msi is run, you can add references to System.Reactive dll to get started. Since Rx is heavily based on Observer pattern - let's take a look at what this pattern is -&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Observer Pattern&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;I always find it easy to understand a pattern with an example and the example of bulb-switch is used commonly to describe the Observer pattern. In a bulb-switch example, the bulb is an observer as it is observing the switch. Whenever the state of switch changes, bulb has to do something. The switch in this case is called a subject. Rx defines 2 interfaces that describe the behavior of observer and switch. For observer, Rx has IObserver interface which defines OnNext(), OnCompleted() and OnError(Exception ex) methods. For subject, Rx has IObservable interface which has one method called Subscribe(IObserver) which takes as input an IObserver.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Let's take a look at some very simple code that uses Rx framework:&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Program&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;        {&lt;br /&gt;            var numbers = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; {1, 2, 0, 3, 4, 5, 6, 7, 8, 9, 10};&lt;br /&gt;            var query = numbers.Take(5);&lt;br /&gt;            &lt;br /&gt;            &lt;span class="rem"&gt;// simple way&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;foreach&lt;/span&gt;(var digit &lt;span class="kwrd"&gt;in&lt;/span&gt; query)&lt;br /&gt;              Print(digit);&lt;br /&gt;            EndMethod();&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// oQuery is the subject (switch) and Print is the observer (bulb)&lt;/span&gt;&lt;br /&gt;            var oQuery = query.ToObservable();&lt;br /&gt;            oQuery.Subscribe(Print);&lt;br /&gt;            Console.WriteLine();&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// demonstrating an OnComplete scenario&lt;/span&gt;&lt;br /&gt;            oQuery = query.ToObservable();&lt;br /&gt;            oQuery.Subscribe(Print, EndMethod);&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// demonstrating an error scenario&lt;/span&gt;&lt;br /&gt;            oQuery = query.ToObservable().Select(n =&amp;gt; 10 / n);&lt;br /&gt;            oQuery.Subscribe(Print, ErrorMethod, EndMethod);&lt;br /&gt;            &lt;br /&gt;            Console.Read();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Print(&lt;span class="kwrd"&gt;int&lt;/span&gt; i)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(i);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; EndMethod()&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"Task is complete"&lt;/span&gt;);&lt;br /&gt;            Console.WriteLine();&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ErrorMethod(Exception ex)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(ex.Message);&lt;br /&gt;            Console.WriteLine();&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;and when I run this app, these are the results that I get -&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-AxU9-vqlq9c/TdwvFY1K9FI/AAAAAAAAB24/GRD1-Y5RwqY/s1600/1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-AxU9-vqlq9c/TdwvFY1K9FI/AAAAAAAAB24/GRD1-Y5RwqY/s1600/1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The foreach loop demonstrates a typical way in which we are used to do the things. We loop through each number and then we do something with that number and in the end get out of that code block.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In the next code piece, we start to use the Rx framework. As mentioned earlier, we need an IObserver and an IObservable to work with. We convert the query to IObservable by calling the ToObservable method. This method converts IEnumerables to IObservables. Once the query is converted to an IObservable, we call the Subscribe() method on it. In the Subscribe() method we have to provide an IObserver. We don't really need to provide an IObserver but instead we can provide the 3 methods that IObserver implements, i.e, OnNext(), OnCompleted() and OnError(). In my code, I have 3 code pieces using different overloads. In the first one, I just pass a delegate for OnNext. In the second one, I pass OnNext() and OnCompleted() and in the third one, I pass all the 3. The rule to remember here is that OnNext() will be called one or more times depending on the number of items. OnError will get called if there is an error. If OnError is called, OnCompleted() won't be called and if OnCompleted() is called that means OnError() was not called and the operation was successful. OnNext() will never get called after either OnError() or OnCompleted() is called.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;You might wonder as to what is the big deal about this Rx framework since all that we did above can be easily done using the foreach loop and try-catches. The real power of Rx comes into play when we start to utilize it in asynchronous scenarios, which will be the topic of my next blog.&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-3009307524686876363?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/3009307524686876363'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/3009307524686876363'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/05/net-reactive-extensions-rx-1-basics.html' title='.NET Reactive Extensions Rx - 1 (Basics)'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-AxU9-vqlq9c/TdwvFY1K9FI/AAAAAAAAB24/GRD1-Y5RwqY/s72-c/1.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-5454965638884715985</id><published>2011-05-22T01:13:00.007-04:00</published><updated>2011-05-26T17:49:40.133-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='HTML5'/><title type='text'>HTML 5 Basic Examples</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog, I will try to understand what is HTML5 and what enhancements it brings over current HTML. I will also try to write some sample code to demonstrate the new features.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In very simple terms, HTML5 is a combination of existing markup, new markup additions and JavaScript APIs. The JavaScript APIs form the bulk of what is new in HTML5. One of the reasons why HTML5 is gaining so much attention is because it has inherent support for rich media such as audio and video. So, we don't have to install other plugins to achieve this result.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;I will just publish here the code for some sample tags, new markups that I wrote and in the end there is an image of the webpage running in chrome. One good thing about HTML5 is that it is very simple to learn almost to the extent of being self-explanatory.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Keep in mind that some of the stuff might not work on your machine depending upon the browser you are using. Also, as you can see, I am using scripts from &lt;a href="http://www.modernizr.com/"&gt;http://www.modernizr.com/&lt;/a&gt; to detect HTML5 capabilities on my browser.&lt;/span&gt;&lt;br/&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;!&lt;/span&gt;&lt;span class="html"&gt;DOCTYPE&lt;/span&gt; &lt;span class="attr"&gt;HTML&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;html&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;head&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;HTML5 Examples&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt; &lt;span class="attr"&gt;src&lt;/span&gt;&lt;span class="kwrd"&gt;="Scripts/modernizr-1.7.min.js"&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="text/javascript"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &amp;lt;script type=&lt;span class="str"&gt;"text/javascript"&lt;/span&gt;&amp;gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;function&lt;/span&gt; init() {&lt;br /&gt;            &lt;span class="kwrd"&gt;var&lt;/span&gt; c = document.getElementById(&lt;span class="str"&gt;"myCanvas"&lt;/span&gt;);&lt;br /&gt;            &lt;span class="kwrd"&gt;var&lt;/span&gt; cxt = c.getContext(&lt;span class="str"&gt;"2d"&lt;/span&gt;);&lt;br /&gt;            cxt.fillStyle = &lt;span class="str"&gt;"#FF0000"&lt;/span&gt;;&lt;br /&gt;            cxt.beginPath();&lt;br /&gt;            cxt.arc(70, 18, 15, 0, Math.PI * 2, &lt;span class="kwrd"&gt;true&lt;/span&gt;);&lt;br /&gt;            cxt.closePath();&lt;br /&gt;            cxt.fill();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt; (localStorage.pagecount) {&lt;br /&gt;            localStorage.pagecount = Number(localStorage.pagecount) + 1;&lt;br /&gt;        }&lt;br /&gt;        &lt;span class="kwrd"&gt;else&lt;/span&gt; {&lt;br /&gt;            localStorage.pagecount = 1;&lt;br /&gt;        }&lt;br /&gt;        document.writeln(&lt;span class="str"&gt;"&amp;lt;h2&amp;gt;Local Storage Example&amp;lt;/h2&amp;gt;"&lt;/span&gt;);&lt;br /&gt;        document.writeln(&lt;span class="str"&gt;"&amp;lt;h4&amp;gt;Stored forever&amp;lt;/h4&amp;gt;"&lt;/span&gt;);&lt;br /&gt;        document.writeln(&lt;span class="str"&gt;"Visits: "&lt;/span&gt; + localStorage.pagecount + &lt;span class="str"&gt;" time(s)."&lt;/span&gt;);&lt;br /&gt;        document.writeln(&lt;span class="str"&gt;"&amp;lt;hr color=orange /&amp;gt;"&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt; (sessionStorage.pagecount) {&lt;br /&gt;            sessionStorage.pagecount = Number(sessionStorage.pagecount) + 1;&lt;br /&gt;        }&lt;br /&gt;        &lt;span class="kwrd"&gt;else&lt;/span&gt; {&lt;br /&gt;            sessionStorage.pagecount = 1;&lt;br /&gt;        }&lt;br /&gt;        document.writeln(&lt;span class="str"&gt;"&amp;lt;h2&amp;gt;Session Storage Example&amp;lt;/h2&amp;gt;"&lt;/span&gt;);&lt;br /&gt;        document.writeln(&lt;span class="str"&gt;"&amp;lt;h4&amp;gt;Stored for the session - as long as the browser is open&amp;lt;/h4&amp;gt;"&lt;/span&gt;);&lt;br /&gt;        document.writeln(&lt;span class="str"&gt;"Visits: "&lt;/span&gt; + sessionStorage.pagecount + &lt;span class="str"&gt;" time(s)."&lt;/span&gt;);&lt;br /&gt;        document.writeln(&lt;span class="str"&gt;"&amp;lt;hr color=orange /&amp;gt;"&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;        document.writeln(&lt;span class="str"&gt;"&amp;lt;h2&amp;gt;Modernizr Example&amp;lt;/h2&amp;gt;"&lt;/span&gt;);&lt;br /&gt;        document.writeln(&lt;span class="str"&gt;"Audio capability: "&lt;/span&gt; + Modernizr.audio + &lt;span class="str"&gt;"&amp;lt;br/&amp;gt;"&lt;/span&gt;);&lt;br /&gt;        document.writeln(&lt;span class="str"&gt;"Video capability: "&lt;/span&gt; + Modernizr.video + &lt;span class="str"&gt;"&amp;lt;br/&amp;gt;"&lt;/span&gt;);&lt;br /&gt;        document.writeln(&lt;span class="str"&gt;"Web SQL DB capability: "&lt;/span&gt; + Modernizr.websqldatabase + &lt;span class="str"&gt;"&amp;lt;br/&amp;gt;"&lt;/span&gt;);&lt;br /&gt;        document.writeln(&lt;span class="str"&gt;"&amp;lt;hr color=orange /&amp;gt;"&lt;/span&gt;);&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;head&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt; &lt;span class="attr"&gt;onload&lt;/span&gt;&lt;span class="kwrd"&gt;="init()"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;header&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="http://www.google.com"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hgroup&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Header Example&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h3&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;grouped insided hgroup which is wrapped inside an a element to provide hyperlink&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h3&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;hgroup&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hr&lt;/span&gt; &lt;span class="attr"&gt;color&lt;/span&gt;&lt;span class="kwrd"&gt;="orange"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;header&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;nav&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Nav Example&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ul&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="http://www.google.com"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Google&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="http://www.cnn.com"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;CNN&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="http://www.cnn.com"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;CNN&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ul&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hr&lt;/span&gt; &lt;span class="attr"&gt;color&lt;/span&gt;&lt;span class="kwrd"&gt;="orange"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;nav&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;section&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Canvas Example&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;canvas&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="myCanvas"&lt;/span&gt; &lt;span class="attr"&gt;width&lt;/span&gt;&lt;span class="kwrd"&gt;="200"&lt;/span&gt; &lt;span class="attr"&gt;height&lt;/span&gt;&lt;span class="kwrd"&gt;="100"&lt;/span&gt; &lt;span class="attr"&gt;style&lt;/span&gt;&lt;span class="kwrd"&gt;="border: 1px"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Your browser does not support canvas.&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;canvas&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hr&lt;/span&gt; &lt;span class="attr"&gt;color&lt;/span&gt;&lt;span class="kwrd"&gt;="orange"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;section&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;section&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Video Example&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;video&lt;/span&gt; &lt;span class="attr"&gt;src&lt;/span&gt;&lt;span class="kwrd"&gt;="Videos\movie.ogg"&lt;/span&gt; &lt;span class="attr"&gt;width&lt;/span&gt;&lt;span class="kwrd"&gt;="320"&lt;/span&gt; &lt;span class="attr"&gt;height&lt;/span&gt;&lt;span class="kwrd"&gt;="240"&lt;/span&gt; &lt;span class="attr"&gt;controls&lt;/span&gt;&lt;span class="kwrd"&gt;="controls"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Your browser does not support the video tag.&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;video&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hr&lt;/span&gt; &lt;span class="attr"&gt;color&lt;/span&gt;&lt;span class="kwrd"&gt;="orange"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;section&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;section&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Audio Example&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;audio&lt;/span&gt; &lt;span class="attr"&gt;src&lt;/span&gt;&lt;span class="kwrd"&gt;="Audios\HudHud.mp3"&lt;/span&gt; &lt;span class="attr"&gt;controls&lt;/span&gt;&lt;span class="kwrd"&gt;="controls"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Your browser does not support the video tag.&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;audio&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hr&lt;/span&gt; &lt;span class="attr"&gt;color&lt;/span&gt;&lt;span class="kwrd"&gt;="orange"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;section&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;section&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;aside&lt;/span&gt; &lt;span class="attr"&gt;style&lt;/span&gt;&lt;span class="kwrd"&gt;="font-size:larger;font-style:italic;color:red;float:right;width:300px;border:2px solid black"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            Aside example: This text is on the side because it is inside the aside tag. &lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;aside&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Mark Example&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;mark&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;This text is highlighted because it is inside the mark tag.&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;mark&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hr&lt;/span&gt; &lt;span class="attr"&gt;color&lt;/span&gt;&lt;span class="kwrd"&gt;="orange"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;section&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;details&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Details - Summary Example (Currently not implemented in chrome)&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;summary&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;This is summary&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;summary&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Lots of details here: Lots of details here: Lots of details here: Lots of details here: Lots of details here: Lots of details here: Lots of details here: Lots of details here: Lots of details here: &lt;br /&gt;            Lots of details here: Lots of details here: Lots of details here: Lots of details here: Lots of details here: Lots of details here: Lots of details here: Lots of details here: Lots of details here: &lt;br /&gt;            Lots of details here: Lots of details here: Lots of details here: Lots of details here: Lots of details here: Lots of details here: Lots of details here: Lots of details here:  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hr&lt;/span&gt; &lt;span class="attr"&gt;color&lt;/span&gt;&lt;span class="kwrd"&gt;="orange"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;details&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;section&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;datalist Example&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;label&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Who's greatest&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;input&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="text"&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="favCharacter"&lt;/span&gt; &lt;span class="attr"&gt;list&lt;/span&gt;&lt;span class="kwrd"&gt;="players"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;datalist&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="players"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;="Sachin Tendulkar"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;="Roger Federer"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;="Diego Maradona"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;datalist&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;label&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hr&lt;/span&gt; &lt;span class="attr"&gt;color&lt;/span&gt;&lt;span class="kwrd"&gt;="orange"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;section&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;section&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Input types examples&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;input&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="search"&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="search"&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="search"&lt;/span&gt; &lt;span class="attr"&gt;autofocus&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;br&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;input&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="url"&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="urlinput"&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="urlinput"&lt;/span&gt; &lt;span class="attr"&gt;placeholder&lt;/span&gt;&lt;span class="kwrd"&gt;="Choose URL"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;br&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;input&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="tel"&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="tel"&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="telinput"&lt;/span&gt; &lt;span class="attr"&gt;placeholder&lt;/span&gt;&lt;span class="kwrd"&gt;="Choose telephone number"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;br&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;input&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="color"&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="color1"&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="colorinput"&lt;/span&gt; &lt;span class="attr"&gt;placeholder&lt;/span&gt;&lt;span class="kwrd"&gt;="Choose color"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;br&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;input&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="number"&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="num1"&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="numinput"&lt;/span&gt; &lt;span class="attr"&gt;placeholder&lt;/span&gt;&lt;span class="kwrd"&gt;="Choose number"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;br&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;input&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="date"&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="date1"&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="dateinput"&lt;/span&gt; &lt;span class="attr"&gt;placeholder&lt;/span&gt;&lt;span class="kwrd"&gt;="Choose date"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;br&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hr&lt;/span&gt; &lt;span class="attr"&gt;color&lt;/span&gt;&lt;span class="kwrd"&gt;="orange"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;section&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;section&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Time Example&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;time&lt;/span&gt; &lt;span class="attr"&gt;datetime&lt;/span&gt;&lt;span class="kwrd"&gt;="2011-05-26T16:48:43"&lt;/span&gt; &lt;span class="attr"&gt;pubdate&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;May 26, 2011&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;time&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hr&lt;/span&gt; &lt;span class="attr"&gt;color&lt;/span&gt;&lt;span class="kwrd"&gt;="orange"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;section&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;footer&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&amp;amp;copy Footer example&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hr&lt;/span&gt; &lt;span class="attr"&gt;color&lt;/span&gt;&lt;span class="kwrd"&gt;="orange"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;footer&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;html&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-5454965638884715985?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/5454965638884715985'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/5454965638884715985'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/05/html-5.html' title='HTML 5 Basic Examples'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-2870624281460952345</id><published>2011-05-18T01:45:00.001-04:00</published><updated>2011-05-18T17:05:21.771-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Android'/><category scheme='http://www.blogger.com/atom/ns#' term='JAVA'/><title type='text'>Android - 2 (Eclipse project)</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Now that I understand some of the basics of Android development, I will start exploring more into the Android and Eclipse project and get a better idea about what the files in this project mean. Then I will try to create a simple app to sort of gel all the ideas together. Lets get started -&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Android Project - &lt;/b&gt;The android project consists of the following folders and files&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;src - &lt;/b&gt;contains your code packages (namespaces) and code files&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;gen - &lt;/b&gt;contains the files generated by Eclipse&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;res - &lt;/b&gt;contains the resources files&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Android Manifest - &lt;/b&gt;contains the configuration settings for the project&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Activity - &lt;/b&gt;An activity is a single focussed thing that a user can do. Since most of the activities interact with the user, Activity class creates a window in which we can place our UI components. The following diagram shows the life cycle of an activity. This diagram is taken from developer.android.com.&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-Zg1ZfIQb82E/TdPtLOV454I/AAAAAAAAB2Y/c3QH3uGT0vc/s1600/ActivityLifeCycle.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-Zg1ZfIQb82E/TdPtLOV454I/AAAAAAAAB2Y/c3QH3uGT0vc/s1600/ActivityLifeCycle.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Sample Application - &lt;/b&gt;We will try to build a simple app in Android which will allow us to insert some text in a textbox and on click of a button will log the text in the debug logger. We will also create a help button, which will allow us to go to a different activity.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;So, first up, I create a MainActivity and add a EditText and a button to the UI. Then in my MainActivity.java file in the onCreate of MainActivity, I write the following code -&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-jdTQu1Q5EbA/TdPtuWj72oI/AAAAAAAAB2c/QLjGBkC39wk/s1600/Code1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-jdTQu1Q5EbA/TdPtuWj72oI/AAAAAAAAB2c/QLjGBkC39wk/s1600/Code1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Here, in the MainActivity class I create an OnClickListener variable that takes care of the click event.&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Here, unlike .NET, we can not get the text value by doing something like EditText.Text. There are no such properties, so we have to use the findViewById method to find the view and then use its getText() method to get the text. Log.d takes a tag and a message.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Now to create another view, we use the New-Class feature to create HelpActivity.java. In the onCreate for this Activity, we create a TextView on the fly and set some Text into that as shown below -&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-PYxGWEnfWW4/TdPuMAmXa1I/AAAAAAAAB2g/Jd6fIXubWMg/s1600/Codde2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-PYxGWEnfWW4/TdPuMAmXa1I/AAAAAAAAB2g/Jd6fIXubWMg/s1600/Codde2.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;This creates the view, but we haven't hooked it up with the MainActivity yet. So, I create a Help button in the UI and add an OnClickListener to it in the MainActivity.java file. In the listener, we create a new Intent and then start the activity. An Intent is a way in android to suggest some kind of action or something that we want to do. In this case, we are telling Android to&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;We also need to hookup this HelpActivity in the Android Manifest file as well as shown below -&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-_5Wl_So6gws/TdPumu9WZcI/AAAAAAAAB2k/P8tOuQv8aWQ/s1600/Manifest.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="201" src="http://4.bp.blogspot.com/-_5Wl_So6gws/TdPumu9WZcI/AAAAAAAAB2k/P8tOuQv8aWQ/s640/Manifest.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;When you run the app, you see the following -&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-kS4NuvrAURY/TdPwreojQgI/AAAAAAAAB2s/XbKjkEywE24/s1600/App1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-kS4NuvrAURY/TdPwreojQgI/AAAAAAAAB2s/XbKjkEywE24/s1600/App1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;After clicking the button, you can navigate to the DDMS perspective in Eclipse and in the LogCat window, you can see the text logged as shown below.&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-tZMyLp0W9So/TdPw0XMF9PI/AAAAAAAAB2w/zyrC-LSxiL8/s1600/LogCat.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-tZMyLp0W9So/TdPw0XMF9PI/AAAAAAAAB2w/zyrC-LSxiL8/s1600/LogCat.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;After clicking the help button, you can see that the app navigates to a different view.&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-G7FagWyTvwA/TdPxaRQsT-I/AAAAAAAAB20/Qc5sXAAxDVM/s1600/App2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-G7FagWyTvwA/TdPxaRQsT-I/AAAAAAAAB20/Qc5sXAAxDVM/s1600/App2.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If you are wondering, why this app is called Calculator, thats because I plan to create a simple Calculator app but just for starters, I created this app.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-2870624281460952345?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/2870624281460952345'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/2870624281460952345'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/05/android-2-eclipse-project.html' title='Android - 2 (Eclipse project)'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/-Zg1ZfIQb82E/TdPtLOV454I/AAAAAAAAB2Y/c3QH3uGT0vc/s72-c/ActivityLifeCycle.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-8392624628507710850</id><published>2011-05-17T01:26:00.003-04:00</published><updated>2011-05-18T12:57:10.947-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Android'/><category scheme='http://www.blogger.com/atom/ns#' term='JAVA'/><title type='text'>Android - I</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog, I will look into the overview of Android and how we can use .NET to build applications for Android. I will continue to explore more until I am ready to create an android app and publish it in the android market.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Android is an open source, linux based, mobile operating system. The application layer for Android is written in Java.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Android Architecture&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Linux kernel&lt;/b&gt; - This forms the core. This core has lots of drivers that interact with the actual hardware of the device.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Libraries&lt;/b&gt; - On top of the core are the libraries mostly written in c and c++. One of the libraries is SQLLite which is the underlying DB used by Android.&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;There is also an Android runtime that consists of its own libraries.&amp;nbsp;Each app runs in its own VM.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Application Framework&lt;/b&gt; - On top of libraries lies the application framework. Most of our coding is done against this framework.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Applications &lt;/b&gt;- On top of the application framework lies the actual applications.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Application Life Cycle&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Anything that we do in an android app can be termed as a task. For ex. sending an email.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;A task is a set of activities.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;These activities get arranged in a stack (LIFO)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;All apps run in their own process. They also have their own Linux user id.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Android does the job of managing memory and killing least important ones. There is a chance that one of the applications that was in a stack, got killed because Android memory management kicked in. So we need to take care of this on our end to restore the killed task.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Coding for Android - &lt;/b&gt;To start coding for android, you need the following.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;JDK - Java Development Kit. JDK is a development environment for building applications.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Eclipse - is an open source JAVA IDE backed by Google.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Android SDK starter package - contains core SDK tools&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;ADT - Android Development Tools&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;This was a very basic introduction to working with Android. In the next blog, I will go a bit deeper to creating and understanding the nuances of building Android applications.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-8392624628507710850?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/8392624628507710850'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/8392624628507710850'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/05/android-i.html' title='Android - I'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-4484829679619228170</id><published>2011-03-31T14:36:00.000-04:00</published><updated>2011-03-31T14:36:55.523-04:00</updated><title type='text'>Out for a while</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Last few days, I haven't posted any new blog. That is partly due to laziness and mostly due to Cricket world cup. Also, now I am leaving for India for a month, so there will be no blogs in April as well. I hope to be back to studying and writing from May.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-4484829679619228170?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/4484829679619228170'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/4484829679619228170'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/03/out-for-while.html' title='Out for a while'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-1813290533122552839</id><published>2011-03-07T01:49:00.001-05:00</published><updated>2011-03-07T16:32:33.648-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Sets'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>C# Set Operations</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog, I will cover 2 data structures, namely, HashSet and SortedSet and will create small examples to show how we can achieve mathematical set operations of Union, Intersect, Minus etc with these data structures.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;HashSet&amp;lt;T&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;It uses a hashing algorithm to lookup items in a set and therefore is fast. Due to this, it takes up a bit of additional space.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Dictionary&amp;lt;TKey,TValue&amp;gt; should be used if there is a key available. However, if no such key is available then using HashSet&amp;lt;T&amp;gt; is better. In that sense, HashSet&amp;lt;T&amp;gt; is kind of similar to Dictionary&amp;lt;T,T&amp;gt; that is, where the key and value are the same.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In general sense, HashSet takes a constant amount of time to do Add, Remove or Contains operations, irrespective of how large or small the size might be. A List&amp;lt;T&amp;gt;, as the size increases, will take more time to perform these operations.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;It implements ISet&amp;lt;T&amp;gt;. ISet&amp;lt;T&amp;gt; is an interface that provides the methods to perform set operations such as UnionWith, IntersectWith, ExceptWith etc.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;HashSet also provides extension methods such as Union, Intersect, Except. The difference between these and methods with "With" is that methods with "With" modify the calling HashSet and return void. Methods without "With" return a separate IEnumerable&amp;lt;T&amp;gt;.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;SortedSet&amp;lt;T&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Is similar to HashSet&amp;lt;T&amp;gt; except that the items are kept in a sorted manner as and when they are added.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Most of the points mentioned above are true for SortedSet as well.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Similar to how Dictionary&amp;lt;TKey, TValue&amp;gt; has a SortedDictionary&amp;lt;TKey, TValue&amp;gt;.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Performs a bit slower than HashSet&amp;lt;T&amp;gt; as it spends some time in sorting.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Set Operations&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Union: &lt;/b&gt;{1,2,3,4} union {3,4,5} = {1,2,3,4,5}&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Intersect: &lt;/b&gt;{1,2,3,4} intersect {3,4,5} = {3,4}&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Minus (Except): &lt;/b&gt;{1,2,3,4} minus {3,4,5} = {1,2}&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Superset: &lt;/b&gt;{1,2,3,4} is superset of {3,4}&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Subset: &lt;/b&gt;{3,4} is subset of {1,2,3,4}&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;ProperSuperSet:&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;{1,2,3,4} is proper superset of {1,2,3,4} - FALSE&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;{1,2,3,4} is superset of {1,2,3,4} - TRUE&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;{1,2,3,4} is proper superset of {2,3} - TRUE&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;{1,2,3,4} is superset of {2,3} - TRUE&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;ProperSubSet:&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;{1,2,3,4} is proper subset of {1,2,3,4} - FALSE&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;{1,2,3,4} is subset of {1,2,3,4} - TRUE&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;{2,3} is proper subset of {1,2,3,4} - TRUE&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;{2,3} is subset of {1,2,3,4} - TRUE&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Let's take a look at the code listing below:&lt;/span&gt;&lt;/div&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; A08DataStructures&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Run()&lt;br /&gt;        {&lt;br /&gt;            HashSetExample();&lt;br /&gt;            SortedSetExample();&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// Set operations&lt;/span&gt;&lt;br /&gt;            IntersectExample();&lt;br /&gt;            UnionExample();&lt;br /&gt;            ExceptExample();&lt;br /&gt;            SupersetSubsetExample();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; IntersectExample()&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"\nIntersect Example"&lt;/span&gt;);&lt;br /&gt;            var hs1 = &lt;span class="kwrd"&gt;new&lt;/span&gt; HashSet&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;() { 1, 2, 3, 4 };&lt;br /&gt;            var hs2 = &lt;span class="kwrd"&gt;new&lt;/span&gt; HashSet&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;() { 3, 4, 5 };&lt;br /&gt;            hs1.IntersectWith(hs2);&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var i &lt;span class="kwrd"&gt;in&lt;/span&gt; hs1)&lt;br /&gt;                Console.WriteLine(i.ToString());&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; UnionExample()&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"\nUnion Example"&lt;/span&gt;);&lt;br /&gt;            var hs1 = &lt;span class="kwrd"&gt;new&lt;/span&gt; HashSet&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;() { 1, 2, 3, 4 };&lt;br /&gt;            var hs2 = &lt;span class="kwrd"&gt;new&lt;/span&gt; HashSet&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;() { 3, 4, 5 };&lt;br /&gt;            hs1.UnionWith(hs2);&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var i &lt;span class="kwrd"&gt;in&lt;/span&gt; hs1)&lt;br /&gt;                Console.WriteLine(i.ToString());&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ExceptExample()&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"\nExcept Example"&lt;/span&gt;);&lt;br /&gt;            var hs1 = &lt;span class="kwrd"&gt;new&lt;/span&gt; HashSet&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;() { 1, 2, 3, 4 };&lt;br /&gt;            var hs2 = &lt;span class="kwrd"&gt;new&lt;/span&gt; HashSet&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;() { 3, 4, 5 };&lt;br /&gt;            hs1.ExceptWith(hs2);&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var i &lt;span class="kwrd"&gt;in&lt;/span&gt; hs1)&lt;br /&gt;                Console.WriteLine(i.ToString());&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SupersetSubsetExample()&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"\nSuperSet SubSet Example"&lt;/span&gt;);&lt;br /&gt;            var hs1 = &lt;span class="kwrd"&gt;new&lt;/span&gt; HashSet&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;() { 1, 2, 3, 4 };&lt;br /&gt;            var hs2 = &lt;span class="kwrd"&gt;new&lt;/span&gt; HashSet&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;() { 2, 3 };&lt;br /&gt;&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"hs1 is PROPER SUPERSET of hs1: "&lt;/span&gt; + hs1.IsProperSupersetOf(hs1));&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"hs1 is SUPERSET of hs1: "&lt;/span&gt; + hs1.IsSupersetOf(hs1));&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"hs1 is PROPER SUPERSET of hs2: "&lt;/span&gt; + hs1.IsProperSupersetOf(hs2));&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"hs1 is SUPERSET of hs2: "&lt;/span&gt; + hs1.IsSupersetOf(hs2));&lt;br /&gt;&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"hs1 is PROPER SUBSET of hs1: "&lt;/span&gt; + hs1.IsProperSubsetOf(hs1));&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"hs1 is SUBSET of hs1: "&lt;/span&gt; + hs1.IsSubsetOf(hs1));&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"hs2 is PROPER SUBSET of hs1: "&lt;/span&gt; + hs2.IsProperSubsetOf(hs1));&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"hs2 is SUBSET of hs1: "&lt;/span&gt; + hs2.IsSubsetOf(hs1));&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SortedSetExample()&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"\nSorted Set Example"&lt;/span&gt;);&lt;br /&gt;            var hs = &lt;span class="kwrd"&gt;new&lt;/span&gt; HashSet&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;();&lt;br /&gt;            var ss = &lt;span class="kwrd"&gt;new&lt;/span&gt; SortedSet&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;();&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var sp &lt;span class="kwrd"&gt;in&lt;/span&gt; SportsPerson.LoadSportsPerson())&lt;br /&gt;            {&lt;br /&gt;                hs.Add(sp.Name);&lt;br /&gt;                ss.Add(sp.Name);&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"\nItems in Hash Set"&lt;/span&gt;);&lt;br /&gt;            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var s &lt;span class="kwrd"&gt;in&lt;/span&gt; hs)&lt;br /&gt;                Console.WriteLine(s);&lt;br /&gt;&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"\nItems in Sorted Set"&lt;/span&gt;);&lt;br /&gt;            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var s &lt;span class="kwrd"&gt;in&lt;/span&gt; ss)&lt;br /&gt;                Console.WriteLine(s);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; HashSetExample()&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"HashSet Example"&lt;/span&gt;);&lt;br /&gt;            var hs = &lt;span class="kwrd"&gt;new&lt;/span&gt; HashSet&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;();&lt;br /&gt;            var list = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;();&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var sp &lt;span class="kwrd"&gt;in&lt;/span&gt; SportsPerson.LoadMillionSportsPerson())&lt;br /&gt;            {&lt;br /&gt;                hs.Add(sp.Name);&lt;br /&gt;                list.Add(sp.Name);&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            Stopwatch watch = &lt;span class="kwrd"&gt;new&lt;/span&gt; Stopwatch();&lt;br /&gt;            watch.Start();&lt;br /&gt;            &lt;span class="rem"&gt;// find something in list&lt;/span&gt;&lt;br /&gt;            var contains = list.Contains(&lt;span class="str"&gt;"Name999999"&lt;/span&gt;);&lt;br /&gt;            watch.Stop();&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"Time taken to search in a list: "&lt;/span&gt; + watch.ElapsedMilliseconds);&lt;br /&gt;&lt;br /&gt;            watch.Restart();&lt;br /&gt;            &lt;span class="rem"&gt;//find something in hashset&lt;/span&gt;&lt;br /&gt;            contains = hs.Contains(&lt;span class="str"&gt;"Name999999"&lt;/span&gt;);&lt;br /&gt;            watch.Stop();&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"Time taken to search in a hash set: "&lt;/span&gt; + watch.ElapsedMilliseconds);&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;And here are the results -&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://lh3.googleusercontent.com/-si4DRZPTMbo/TXVOHl9DtTI/AAAAAAAAB10/DjoqYSG6FPo/s1600/1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="https://lh3.googleusercontent.com/-si4DRZPTMbo/TXVOHl9DtTI/AAAAAAAAB10/DjoqYSG6FPo/s1600/1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-1813290533122552839?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/1813290533122552839'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/1813290533122552839'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/03/c-set-operations.html' title='C# Set Operations'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='https://lh3.googleusercontent.com/-si4DRZPTMbo/TXVOHl9DtTI/AAAAAAAAB10/DjoqYSG6FPo/s72-c/1.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-7620474560342065315</id><published>2011-03-02T20:22:00.001-05:00</published><updated>2011-03-21T12:25:34.317-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Azure'/><title type='text'>Windows Azure - Part 2 - Publishing to Cloud</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog, I will reiterate steps to publish a simple solution to the cloud.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;First up, I created a simple website that has a predefined set of quotations and then on the default page, I randomly display a quote. This is very simple because here we are not concerned about the business logic but about how Azure publishing works.&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Steps to get ready for publishing from Visual Studio&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;First we need to create an account at &lt;a href="http://windows.azure.com/"&gt;windows.azure.com&lt;/a&gt;&amp;nbsp;I have a MSDN subscription so I already have an account.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Then on the management portal, we create a "new Hosted Service" and set some simple properties such as name and location.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Instead of choosing location, it is better to choose an affinity group. An affinity group means that all the servers/VMs will be located in the area that we select as an affinity group.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Once this is done, we create a certificate to ensure that only those who have this certificate are allowed to publish.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;We create this cert in VS by going to server explorer and then right-clicking the Windows Azure Compute tab and selecting "Add Deployment Environment".&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this tab, we right click again on "Windows Azure Accounts" and then clicking "New".&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;On this window, we can create a new cert and then in step 2 we can copy the path of this cert to the clipboard. (picture below)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Then we go back to the management portal and navigate to "Management Certificates" tab. Here we click the "Add Certificate" button.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Here we can copy the path that we copied in the previous step and finish adding the certificate.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Once the cert is added, in the management portal on the right side you will see that an subscription ID is generated.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Copy this subscription ID and go back to VS and paste the id in step 3. Give this cert some name and click OK.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Now go back to Management portal and click on Storage accounts and then click on New Storage account.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Create a new storage account by choosing the same affinity group as chosen above. Its not required but its a good practice to have your storage and app VMs to be near to each other.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Once this is done, we are all set up to publish our solution.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://lh5.googleusercontent.com/-go-sFExMXRc/TW7qCGg-7EI/AAAAAAAAB1Y/vSnHEMmS6yI/s1600/1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="https://lh5.googleusercontent.com/-go-sFExMXRc/TW7qCGg-7EI/AAAAAAAAB1Y/vSnHEMmS6yI/s1600/1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Publishing&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Right click on the windows azure project and click on publish.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;This will open up an window where you have to select the Credentials, Deployment environment and storage account. All of this, we just created in the steps above.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Once this is done, click on OK and publishing will start.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The publishing takes some time because Azure is actually getting all the instances ready for publishing and then publishing the code out to them.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;As publishing progresses, VS also shows a Windows Azure Activity Log - which displays status information about publishing. (shown above)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Once the publishing is done a website URL is provided for you to access your website.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;My first cloud app website is located at : &lt;a href="http://quotes.cloudapp.net/"&gt;http://quotes.cloudapp.net&lt;/a&gt;&amp;nbsp;(if you can not access the website, that means, I have brought it down because it costs money to keep it up all the time. So I bring it up only when I am doing some testing for few days. Sorry about that)&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="https://lh3.googleusercontent.com/-Zuys5DpFmo0/TW7tE6AdeCI/AAAAAAAAB1g/potKIQRGlKA/s1600/2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="157" src="https://lh3.googleusercontent.com/-Zuys5DpFmo0/TW7tE6AdeCI/AAAAAAAAB1g/potKIQRGlKA/s640/2.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-7620474560342065315?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/7620474560342065315'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/7620474560342065315'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/03/windows-azure-part-2-publishing-to.html' title='Windows Azure - Part 2 - Publishing to Cloud'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='https://lh5.googleusercontent.com/-go-sFExMXRc/TW7qCGg-7EI/AAAAAAAAB1Y/vSnHEMmS6yI/s72-c/1.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-5012033575980758495</id><published>2011-02-28T01:57:00.005-05:00</published><updated>2011-02-28T12:15:55.012-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='LINQ'/><title type='text'>LINQ Examples</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog, I will try to create samples of LINQ extension methods which are very helpful in accomplishing things which earlier would take 10-15 lines (or more) of code and were difficult to manage as well as to understand. I will also try to keep this blog as a running list of good LINQ extension methods.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;First up, let's see the whole code listing. For my data, I have created a simple SportsPerson class and have populated with arbitrary values.&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; A07LinqExamples&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Run()&lt;br /&gt;        {&lt;br /&gt;            ToLookupExample();&lt;br /&gt;&lt;br /&gt;            ToDictionaryExample();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ToDictionaryExample()&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"\n---------ToDictionaryExample-----------------"&lt;/span&gt;);&lt;br /&gt;            var sportsPersons = LoadSportsPerson();&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// get a dictionary with id as id and value as SportsPerson&lt;/span&gt;&lt;br /&gt;            var sportsPersonDict = sportsPersons.ToDictionary(sp =&amp;gt; sp.Id, sp =&amp;gt; sp);&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// get sportsperson with id 8&lt;/span&gt;&lt;br /&gt;            var val = sportsPersonDict[8];&lt;br /&gt;            Console.WriteLine(val.Name + &lt;span class="str"&gt;"("&lt;/span&gt; + val.Sport + &lt;span class="str"&gt;")"&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ToLookupExample()&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"\n---------ToLookupExample-----------------"&lt;/span&gt;);&lt;br /&gt;            var sportsPersons = LoadSportsPerson();&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// get a lookup with id as sport and value as list of SportsPerson&lt;/span&gt;&lt;br /&gt;            var sportsPersonsCategorized = sportsPersons.ToLookup(sp =&amp;gt; sp.Sport, sp =&amp;gt; sp);&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// get all sportsperson who play cricket&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var lkup &lt;span class="kwrd"&gt;in&lt;/span&gt; sportsPersonsCategorized[&lt;span class="str"&gt;"Cricket"&lt;/span&gt;])&lt;br /&gt;                Console.WriteLine(lkup.Name);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; IEnumerable&amp;lt;SportsPerson&amp;gt; LoadSportsPerson()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;SportsPerson&amp;gt;()&lt;br /&gt;                       {&lt;br /&gt;                           &lt;span class="kwrd"&gt;new&lt;/span&gt; SportsPerson() {Id = 1, Name = &lt;span class="str"&gt;"Sachin Tendulkar"&lt;/span&gt;, Sport = &lt;span class="str"&gt;"Cricket"&lt;/span&gt;},&lt;br /&gt;                           &lt;span class="kwrd"&gt;new&lt;/span&gt; SportsPerson() {Id = 10, Name = &lt;span class="str"&gt;"Roger Federer"&lt;/span&gt;, Sport = &lt;span class="str"&gt;"Tennis"&lt;/span&gt;},&lt;br /&gt;                           &lt;span class="kwrd"&gt;new&lt;/span&gt; SportsPerson() {Id = 8, Name = &lt;span class="str"&gt;"Rafael Nadal"&lt;/span&gt;, Sport = &lt;span class="str"&gt;"Tennis"&lt;/span&gt;},&lt;br /&gt;                           &lt;span class="kwrd"&gt;new&lt;/span&gt; SportsPerson() {Id = 6, Name = &lt;span class="str"&gt;"Pete Sampras"&lt;/span&gt;, Sport = &lt;span class="str"&gt;"Tennis"&lt;/span&gt;},&lt;br /&gt;                           &lt;span class="kwrd"&gt;new&lt;/span&gt; SportsPerson() {Id = 5, Name = &lt;span class="str"&gt;"Andre Agassi"&lt;/span&gt;, Sport = &lt;span class="str"&gt;"Tennis"&lt;/span&gt;},&lt;br /&gt;                           &lt;span class="kwrd"&gt;new&lt;/span&gt; SportsPerson() {Id = 4, Name = &lt;span class="str"&gt;"Brian Lara"&lt;/span&gt;, Sport = &lt;span class="str"&gt;"Cricket"&lt;/span&gt;},&lt;br /&gt;                           &lt;span class="kwrd"&gt;new&lt;/span&gt; SportsPerson() {Id = 7, Name = &lt;span class="str"&gt;"Glenn McGrath"&lt;/span&gt;, Sport = &lt;span class="str"&gt;"Cricket"&lt;/span&gt;},&lt;br /&gt;                           &lt;span class="kwrd"&gt;new&lt;/span&gt; SportsPerson() {Id = 3, Name = &lt;span class="str"&gt;"Michael Jordan"&lt;/span&gt;, Sport = &lt;span class="str"&gt;"Basketball"&lt;/span&gt;},&lt;br /&gt;                           &lt;span class="kwrd"&gt;new&lt;/span&gt; SportsPerson() {Id = 9, Name = &lt;span class="str"&gt;"Magic Johnson"&lt;/span&gt;, Sport = &lt;span class="str"&gt;"Basketball"&lt;/span&gt;},&lt;br /&gt;                           &lt;span class="kwrd"&gt;new&lt;/span&gt; SportsPerson() {Id = 2, Name = &lt;span class="str"&gt;"Steve Nash"&lt;/span&gt;, Sport = &lt;span class="str"&gt;"Basketball"&lt;/span&gt;}&lt;br /&gt;                       };&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SportsPerson&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Id { get; set; }&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Sport { get; set; }&lt;br /&gt;    }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;ToDictionary&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;ToDictionary creates a key-value pair from an IEnumerable where all the keys have to be unique. The inputs that you have to provide are obviously the key and the value.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;ToLookup&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Many a times there is a need to create a lookup structure with some kind of grouping. For example, in our case, we want all sports persons to be categorized based on the sports they play. Lookup provides a simple key-value pair approach where the key can be the sports category and value can be all the sportsperson that belong to that sport. This is very different than dictionary. In a dictionary the key has to be unique. In Lookup keys can be the same.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://lh6.googleusercontent.com/-MEqB3kERkIA/TWvXRgVYf4I/AAAAAAAAB1U/ohifBNDZQWw/s1600/1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="https://lh6.googleusercontent.com/-MEqB3kERkIA/TWvXRgVYf4I/AAAAAAAAB1U/ohifBNDZQWw/s1600/1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-5012033575980758495?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/5012033575980758495'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/5012033575980758495'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/02/linq-examples.html' title='LINQ Examples'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='https://lh6.googleusercontent.com/-MEqB3kERkIA/TWvXRgVYf4I/AAAAAAAAB1U/ohifBNDZQWw/s72-c/1.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-883815434046952715</id><published>2011-02-20T21:46:00.008-05:00</published><updated>2011-02-23T21:20:11.624-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ASCII'/><category scheme='http://www.blogger.com/atom/ns#' term='UTF-8'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Base-64'/><category scheme='http://www.blogger.com/atom/ns#' term='UTF-16'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='UTF-32'/><title type='text'>Encodings</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog post, I will explain how to play around with different types of encodings using C#. I will mainly concentrate on ASCII, UTF-8 and UTF-16.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Character encoding systems consists of a code that pairs each character from a given set into something else, such as numbers.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Different types of encodings&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;ASCII - works on general english characters. For example, in ASCII capital 'A' is represented by decimal 65 and small 'a' by 97.&lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;UNICODE - works on a broader set of characters. Its a more consistent encoding to represent text expressed by most of the world's writing systems. Unicode is not an encoding. Its a standard. It can be implemented by different character encodings. Most famous being UTF-8 and UTF-16. UTF stands for Unicode Transformation Format.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Difference between UTF-8 and UTF-16 and UTF-32&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;UTF-8 uses 1 byte at a minimum while UTF-16 uses 2 hence UTF-8 files are generally smaller in size than UTF-16 files.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;UTF-8 is backward compatible with ASCII while UTF-16 is not.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Both UTF-8 and UTF-16 are variable-length encodings. That is, its not guaranteed that UTF-8 will always use 1 byte and UTF-16 will always 2 bytes. UTF-16 can use 1 byte or 2 bytes.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;UTF-32 is fixed-length encoding. All characters representations take up 4 bytes of space. The advantage of this is that byte arrays are easily indexable. That is, you know that the second character in the array will always start with the 4th byte that is barray[3].&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Because every character takes 4 bytes, UTF-32 is space inefficient.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Base-64 Encoding&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Base-64 is an encoding scheme to represent binary data in ASCII string format by translating it into radix-64 representation. Base-64 is different from ASCII and Unicode in the sense that Base-64 is not used for representation but more for transportation. To convert a string into base-64 encoded string, we do the following-&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Let's take a string "cat".&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Converting it into ASCII chars will give us: 99, 97, 116&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Represent them in binary: 01100011 01100001 01110100&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Combine them:&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;011000110110000101110100&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Pack them in group of 6:&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;011000 110110 000101 110100&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Convert it back to numbers and then to string: "Y2F0"&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Code Examples&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Let's take a look at some code examples. First up, this is the code-&lt;/span&gt;&lt;/div&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; A05Encodings&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Run()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; str = &lt;span class="str"&gt;"水"&lt;/span&gt;;&lt;br /&gt;            &lt;span class="rem"&gt;//string str = "cat";&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"STRING: "&lt;/span&gt; + str);&lt;br /&gt;            &lt;br /&gt;            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] bytesAscii = ConvertStrToAscii(str);&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; strAscii = ConvertAsciiToStr(bytesAscii);&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] bytesUTF8 = ConvertStrToUTF8(str);&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; strUTF8 = ConvertUTF8ToStr(bytesUTF8);&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] bytesUnicode = ConvertStrToUnicode(str);&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; strUnicode = ConvertUnicodeToStr(bytesUnicode);&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] bytesUTF32 = ConvertStrToUTF32(str);&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; strUTF32 = ConvertUTF32ToStr(bytesUTF32);&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; base64string = ConvertStrToBase64EncodedString(str);&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; s = ConvertBase64EncodedStringToStr(base64string);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="rem"&gt;/* ASCII */&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] ConvertStrToAscii(&lt;span class="kwrd"&gt;string&lt;/span&gt; str)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine();&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"CONVERT STRING TO ASCII"&lt;/span&gt;);&lt;br /&gt;            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] bytes = Encoding.ASCII.GetBytes(str);&lt;br /&gt;            PrintBytes(bytes);&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; bytes;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ConvertAsciiToStr(&lt;span class="kwrd"&gt;byte&lt;/span&gt;[] bytes)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine();&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"CONVERT ASCII TO STRING"&lt;/span&gt;);&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; str = Encoding.ASCII.GetString(bytes);&lt;br /&gt;            Console.WriteLine(str);&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; str;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="rem"&gt;/* UTF-8 */&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] ConvertStrToUTF8(&lt;span class="kwrd"&gt;string&lt;/span&gt; str)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine();&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"CONVERT STRING TO UTF-8"&lt;/span&gt;);&lt;br /&gt;            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] bytes = Encoding.UTF8.GetBytes(str);&lt;br /&gt;            PrintBytes(bytes);&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; bytes;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ConvertUTF8ToStr(&lt;span class="kwrd"&gt;byte&lt;/span&gt;[] bytes)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine();&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"CONVERT UTF-8 TO STRING"&lt;/span&gt;);&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; str = Encoding.UTF8.GetString(bytes);&lt;br /&gt;            Console.WriteLine(str);&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; str;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="rem"&gt;/* UNICODE = UTF-16 */&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] ConvertStrToUnicode(&lt;span class="kwrd"&gt;string&lt;/span&gt; str)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine();&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"CONVERT STRING TO UNICODE=UTF-16"&lt;/span&gt;);&lt;br /&gt;            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] bytes = Encoding.Unicode.GetBytes(str);&lt;br /&gt;            PrintBytes(bytes);&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; bytes;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ConvertUnicodeToStr(&lt;span class="kwrd"&gt;byte&lt;/span&gt;[] bytes)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine();&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"CONVERT UNICODE=UTF-16 TO STRING"&lt;/span&gt;);&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; str = Encoding.Unicode.GetString(bytes);&lt;br /&gt;            Console.WriteLine(str);&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; str;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="rem"&gt;/* UTF-32 */&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] ConvertStrToUTF32(&lt;span class="kwrd"&gt;string&lt;/span&gt; str)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine();&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"CONVERT STRING TO UTF-32"&lt;/span&gt;);&lt;br /&gt;            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] bytes = Encoding.UTF32.GetBytes(str);&lt;br /&gt;            PrintBytes(bytes);&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; bytes;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ConvertUTF32ToStr(&lt;span class="kwrd"&gt;byte&lt;/span&gt;[] bytes)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine();&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"CONVERT UTF-32 TO STRING"&lt;/span&gt;);&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; str = Encoding.UTF32.GetString(bytes);&lt;br /&gt;            Console.WriteLine(str);&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; str;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="rem"&gt;/* Base 64 Encoding */&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ConvertStrToBase64EncodedString(&lt;span class="kwrd"&gt;string&lt;/span&gt; str)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine();&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"CONVERT STRING TO BASE-64 ENCODING"&lt;/span&gt;);&lt;br /&gt;            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] bytes = Encoding.ASCII.GetBytes(str);&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; base64str = Convert.ToBase64String(bytes);&lt;br /&gt;            Console.WriteLine(base64str);&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; base64str;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ConvertBase64EncodedStringToStr(&lt;span class="kwrd"&gt;string&lt;/span&gt; str)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine();&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"CONVERT STRING TO BASE-64 ENCODING"&lt;/span&gt;);&lt;br /&gt;            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] bytes = System.Convert.FromBase64String(str);&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; s = Encoding.ASCII.GetString(bytes);&lt;br /&gt;            Console.WriteLine(s);&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; s;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="rem"&gt;/* HELPERS */&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PrintBytes(&lt;span class="kwrd"&gt;byte&lt;/span&gt;[] bytes)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (&lt;span class="kwrd"&gt;byte&lt;/span&gt; b &lt;span class="kwrd"&gt;in&lt;/span&gt; bytes)&lt;br /&gt;            {&lt;br /&gt;                Console.Write(b);&lt;br /&gt;                Console.Write(&lt;span class="str"&gt;"  "&lt;/span&gt;);&lt;br /&gt;            }&lt;br /&gt;            Console.WriteLine();&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"LENGTH: "&lt;/span&gt; + bytes.Length);&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;When I run with the string as "cat". These are the results -&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-l3zBqjrvaWE/TWHN9wxXSiI/AAAAAAAAB1E/Iy6M2W6THGU/s1600/cat.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-l3zBqjrvaWE/TWHN9wxXSiI/AAAAAAAAB1E/Iy6M2W6THGU/s1600/cat.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;When I run with the string as "水". These are the results -&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-yQobHx-PgSg/TWHPctv4HaI/AAAAAAAAB1I/hGaFBqEt64c/s1600/chinese.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-yQobHx-PgSg/TWHPctv4HaI/AAAAAAAAB1I/hGaFBqEt64c/s1600/chinese.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this case, ASCII can not represent this character hence the results for ASCII, UTF-8 and Base-64 encoding are wrong since they are actually for string "?".&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-883815434046952715?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/883815434046952715'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/883815434046952715'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/02/encodings.html' title='Encodings'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-l3zBqjrvaWE/TWHN9wxXSiI/AAAAAAAAB1E/Iy6M2W6THGU/s72-c/cat.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-8324334389487020915</id><published>2011-02-20T15:50:00.006-05:00</published><updated>2011-02-20T21:57:20.944-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='CLOB'/><category scheme='http://www.blogger.com/atom/ns#' term='BLOB'/><category scheme='http://www.blogger.com/atom/ns#' term='XML'/><category scheme='http://www.blogger.com/atom/ns#' term='NCLOB'/><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Storing BLOBs, CLOBs and XML in SQL</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog post, I will explore how to use OPENROWSET in SQL to perform operations for BLOBS, CLOBS, external XML files etc.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Storing BLOBs&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;BLOBs are binary large objects. To store BLOBs we will use the OPENROWSET keyword in SQL. Let's take a look at the SQL first.&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;declare&lt;/span&gt; @BlobTable &lt;span class="kwrd"&gt;table&lt;/span&gt;(blob_path &lt;span class="kwrd"&gt;varchar&lt;/span&gt;(150), blob_storage varbinary(&lt;span class="kwrd"&gt;max&lt;/span&gt;))&lt;br /&gt;&lt;span class="kwrd"&gt;insert&lt;/span&gt; &lt;span class="kwrd"&gt;into&lt;/span&gt; @BlobTable (blob_path, blob_storage)&lt;br /&gt;&lt;span class="kwrd"&gt;select&lt;/span&gt; &lt;span class="str"&gt;'C:\abc.txt'&lt;/span&gt; ,&lt;br /&gt;* &lt;span class="kwrd"&gt;from&lt;/span&gt; &lt;span class="kwrd"&gt;openrowset&lt;/span&gt;(&lt;span class="kwrd"&gt;bulk&lt;/span&gt; &lt;span class="str"&gt;'C:\abc.txt'&lt;/span&gt;, single_blob) &lt;span class="kwrd"&gt;as&lt;/span&gt; document&lt;br /&gt;&lt;span class="kwrd"&gt;select&lt;/span&gt; * &lt;span class="kwrd"&gt;from&lt;/span&gt; @BlobTable&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;First we create a table to hold the blob object. This column is of type varbinary(max) and then we insert the file text into this column using OPENROWSET. The option single_blob returns the contents as single row, single column rowset of type varbinary(max).&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Storing CLOBs&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;CLOBs are character large objects. To store CLOBs we will again use the OPENROWSET keyword in SQL. The SQL statement looks like -&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;declare&lt;/span&gt; @ClobTable &lt;span class="kwrd"&gt;table&lt;/span&gt;(clob_path &lt;span class="kwrd"&gt;varchar&lt;/span&gt;(150), clob_storage &lt;span class="kwrd"&gt;varchar&lt;/span&gt;(&lt;span class="kwrd"&gt;max&lt;/span&gt;))&lt;br /&gt;&lt;span class="kwrd"&gt;insert&lt;/span&gt; &lt;span class="kwrd"&gt;into&lt;/span&gt; @ClobTable (clob_path, clob_storage)&lt;br /&gt;&lt;span class="kwrd"&gt;select&lt;/span&gt; &lt;span class="str"&gt;'C:\abc.txt'&lt;/span&gt; ,&lt;br /&gt;* &lt;span class="kwrd"&gt;from&lt;/span&gt; &lt;span class="kwrd"&gt;openrowset&lt;/span&gt;(&lt;span class="kwrd"&gt;bulk&lt;/span&gt; &lt;span class="str"&gt;'C:\abc.txt'&lt;/span&gt;, single_clob) &lt;span class="kwrd"&gt;as&lt;/span&gt; document&lt;br /&gt;&lt;span class="kwrd"&gt;select&lt;/span&gt; * &lt;span class="kwrd"&gt;from&lt;/span&gt; @ClobTable&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The only 2 differences between this statement and the one above is that here the clob_storage is defined as varchar(max) and the option provided to OPENROWSET is single_clob. The option single_clob returns the contents as single row, single column rowset of type varchar(max). It reads the file provided in ASCII.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Storing NCLOBs&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The only difference between storing CLOB and NCLOB is that the datatype used is nvarchar(max) and the file should be saved with UNICODE encoding. The SQL statement looks like:&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;declare&lt;/span&gt; @NClobTable &lt;span class="kwrd"&gt;table&lt;/span&gt;(clob_path &lt;span class="kwrd"&gt;varchar&lt;/span&gt;(150), clob_storage nvarchar(&lt;span class="kwrd"&gt;max&lt;/span&gt;))&lt;br /&gt;&lt;span class="kwrd"&gt;insert&lt;/span&gt; &lt;span class="kwrd"&gt;into&lt;/span&gt; @NClobTable (clob_path, clob_storage)&lt;br /&gt;&lt;span class="kwrd"&gt;select&lt;/span&gt; &lt;span class="str"&gt;'C:\abcUnicode.txt'&lt;/span&gt; ,&lt;br /&gt;* &lt;span class="kwrd"&gt;from&lt;/span&gt; &lt;span class="kwrd"&gt;openrowset&lt;/span&gt;(&lt;span class="kwrd"&gt;bulk&lt;/span&gt; &lt;span class="str"&gt;'C:\abcUnicode.txt'&lt;/span&gt;, single_nclob) &lt;span class="kwrd"&gt;as&lt;/span&gt; document&lt;br /&gt;&lt;span class="kwrd"&gt;select&lt;/span&gt; * &lt;span class="kwrd"&gt;from&lt;/span&gt; @NClobTable&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Inserting/Updating XML from file&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;MSDN website says that we should import xml data using the single_blob option only, rather than single_clob and single_nclob option as only single_blob option supports all windows encoding conversions. Let's take a look at an example&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;declare&lt;/span&gt; @New_varbinary varbinary(&lt;span class="kwrd"&gt;max&lt;/span&gt;)&lt;br /&gt;&lt;span class="kwrd"&gt;set&lt;/span&gt; @New_varbinary = (&lt;span class="kwrd"&gt;select&lt;/span&gt; * &lt;span class="kwrd"&gt;from&lt;/span&gt; &lt;span class="kwrd"&gt;openrowset&lt;/span&gt;(&lt;span class="kwrd"&gt;bulk&lt;/span&gt; &lt;span class="str"&gt;'C:\abc.xml'&lt;/span&gt;, single_blob) &lt;span class="kwrd"&gt;as&lt;/span&gt; document)&lt;br /&gt;&lt;span class="kwrd"&gt;update&lt;/span&gt; TableWithXmlColumn &lt;br /&gt;&lt;span class="kwrd"&gt;set&lt;/span&gt; TheXmlColumn = @New_varbinary&lt;br /&gt;&lt;span class="kwrd"&gt;where&lt;/span&gt; SomeId = 1653&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Difference between varbinary, varchar, nvarchar&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;nvarchar is used to store UNICODE data while varchar is used to store ASCII data. We should use nvarchar if the database requirements are for multilingual support. This will allow for extended set of characters. If the data stored not need any multilingual support than we should use varchar. The reason being nvarchar uses twice as much space as varchar to support extended character codes. These data types in SQL are categorized in different categories: varbinary belongs to binary strings category. varchar belongs to character strings category and nvarchar belongs to Unicode character strings.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-8324334389487020915?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/8324334389487020915'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/8324334389487020915'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/02/storing-blobs-clobs-and-xml-in-sql.html' title='Storing BLOBs, CLOBs and XML in SQL'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-3073213488289310170</id><published>2011-02-20T11:27:00.003-05:00</published><updated>2011-02-20T11:30:10.820-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Azure'/><title type='text'>Windows Azure - Part 1 - Overview</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog post, I will cover the basics of Windows Azure and cloud computing.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Cloud Computing: &lt;/b&gt;Cloud computing is about abstracting out the infrastructure needs for your application. So that you don't have to worry about scalability, server management, patches, updates etc. You also pay for the resources you use.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Windows Azure consists of 3 components -&lt;/span&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Compute - consists of the VMs that have high computing capacities&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Storage - for storing images/large files etc&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Management - an API to manage the needs for your application&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;SQL Azure provides all the SQL database capabilities in a highly scalable manner.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Architecture&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;At the first level, we have Loadbalancers which assist in scaling. The second layer is called Fabric. This consists of server instances that are running your application. They can have Web roles or Worker roles. The fabric layer talks to Storage layer via another set of Loadbalancers. The storage layer consists of tables, blobs etc. There is one another component which cuts across multiple layers. This is called Fabric Controller. This is the controller that manages the whole environment. If one server goes down, Fabric controller chooses another one so that the application is not down. This is the advantage that we get from cloud computing.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;So, I went ahead and added a windows azure project with one web role, one worker role.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Web Role:&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Web role provides a web front end for our application.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;WebRole.cs file provides the entry point for this web app. Here we can override the OnStart() etc methods.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;These methods are called even before the Global.asax methods.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Worker Role:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Worker role is similar to windows service in the sense that it is running in the background and it can listen to events or it can poll and execute certain tasks.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;WorkerRole.cs file provides the entry point to this worker process.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Here you can override the OnStart(), Run() methods etc.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The Run() method consists of a while (true) statement. It is in this while loop that we can write our code for the worker process to execute.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Windows Azure Project:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;This project provides the cloud settings for the roles mentioned above.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Here you can control the number of instances, size of VM, settings, certificates, Virtual networks, storage etc.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The difference between these settings file and web.config is that you can make changes to these settings file and they will be picked up by the web role or worker role automatically. In case of web.config, you need to deploy the new web.config and restart IIS or redeploy the app for the changes to take effect.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;When you run this app, a package is created and then deployed. The website starts to run but the point to notice here is that the website is not running in your IIS or localhost but in another environment called developer fabric. If you see on the right lower side of your screen, you will see that 2 small icons for Compute emulator and Storage emulator have appeared. If you look at these emulators you can see the trace logs, the state in which instances are and can do the monitoring jobs.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-3073213488289310170?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/3073213488289310170'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/3073213488289310170'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/02/windows-azure-part-1-overview.html' title='Windows Azure - Part 1 - Overview'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-1961412921371488220</id><published>2011-02-16T22:19:00.002-05:00</published><updated>2011-02-16T22:21:03.114-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='DLR'/><title type='text'>Dynamic keyword in C#</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog post, I will try to understand the dynamic keyword that was introduced in C# 4.0. These notes are from Alexandra Rusina's article.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Usually dynamic languages such as JavaScript, Ruby, Python etc don't perform compile time checks therefore no compile time errors. C# and Java are considered to be statically typed languages that's why we get the compilation errors and intellisense. To enhance interoperability between dynamic and static languages, a keyword dynamic was added in C#. This keyword tells the compiler to turn off the compile time checking.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;object work the same way, but to perform explicit type operations you have to explicitly cast the object to that specified type first.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;var keyword also works similarly but the difference is that you can not change the type of var at runtime.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Reflection&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Earlier to read objects from different systems, we had to rely on reflection to read the objects and its members and properties. This kind of code was tough to read. Dynamic makes it a bit easier.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Dynamic Method Bags&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Dynamic method bags are objects that are created at run time and they can add or remove properties and methods.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Please see the following code which provides examples for the topics discussed above.&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TV&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Show(&lt;span class="kwrd"&gt;string&lt;/span&gt; str)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(str);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; A04DynamicKeyword&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Run()&lt;br /&gt;        {&lt;br /&gt;            dynamic d = &lt;span class="str"&gt;"sachin tendulkar"&lt;/span&gt;;&lt;br /&gt;            Console.WriteLine(d.GetType());&lt;br /&gt;            &lt;br /&gt;            &lt;span class="rem"&gt;// this will compile because its dynamic so type information is not known&lt;/span&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// but at runtime this will fail&lt;/span&gt;&lt;br /&gt;            &lt;span class="rem"&gt;//d++;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            d = 100;&lt;br /&gt;            Console.WriteLine(d.GetType());&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// this will work&lt;/span&gt;&lt;br /&gt;            d++;&lt;br /&gt;            Console.WriteLine(d);&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;object&lt;/span&gt; obj = &lt;span class="str"&gt;"sachin tendulkar"&lt;/span&gt;;&lt;br /&gt;            Console.WriteLine(obj.GetType());&lt;br /&gt;&lt;br /&gt;            obj = 100;&lt;br /&gt;            Console.WriteLine(obj.GetType());&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// for object explicit cast has to be used&lt;/span&gt;&lt;br /&gt;            obj = (&lt;span class="kwrd"&gt;int&lt;/span&gt;) obj + 1;&lt;br /&gt;            Console.WriteLine(obj);&lt;br /&gt;&lt;br /&gt;            var v = 1000;&lt;br /&gt;            Console.Write(v.GetType());&lt;br /&gt;&lt;br /&gt;            v++;&lt;br /&gt;            Console.WriteLine(v);&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// this will throw compiler error&lt;/span&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// we can not change the type of var once its fixed&lt;/span&gt;&lt;br /&gt;            &lt;span class="rem"&gt;//v = "sachin tendulkar";&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;//reflection was tough to code and not intuitive&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;object&lt;/span&gt; tvObj = GetTV();&lt;br /&gt;            Type type = tvObj.GetType();&lt;br /&gt;            type.InvokeMember(&lt;span class="str"&gt;"Show"&lt;/span&gt;, BindingFlags.InvokeMethod, &lt;span class="kwrd"&gt;null&lt;/span&gt;, tvObj, &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt;[] {&lt;span class="str"&gt;"Show from Object"&lt;/span&gt;});&lt;br /&gt;            &lt;br /&gt;            &lt;span class="rem"&gt;// dynamic is easy and intuitive&lt;/span&gt;&lt;br /&gt;            dynamic tv = GetTV();&lt;br /&gt;            tv.Show(&lt;span class="str"&gt;"Dynamic Show"&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// Dynamic Class&lt;/span&gt;&lt;br /&gt;            dynamic expandoObj = &lt;span class="kwrd"&gt;new&lt;/span&gt; ExpandoObject();&lt;br /&gt;            expandoObj.FlyProperty = &lt;span class="str"&gt;"This property is added on fly"&lt;/span&gt;;&lt;br /&gt;            expandoObj.SomeMethod = (Action) (() =&amp;gt; Console.WriteLine(expandoObj.FlyProperty));&lt;br /&gt;            expandoObj.SomeMethod();&lt;br /&gt;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; TV GetTV()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; TV();&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;And here are the results&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-4AxaxH5b2EE/TVyS_B9iQOI/AAAAAAAAB0o/9JaxHdtxOGs/s1600/1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-4AxaxH5b2EE/TVyS_B9iQOI/AAAAAAAAB0o/9JaxHdtxOGs/s1600/1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-1961412921371488220?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/1961412921371488220'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/1961412921371488220'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/02/dynamic-keyword-in-c.html' title='Dynamic keyword in C#'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/-4AxaxH5b2EE/TVyS_B9iQOI/AAAAAAAAB0o/9JaxHdtxOGs/s72-c/1.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-4713888767068948107</id><published>2011-02-03T01:45:00.008-05:00</published><updated>2011-02-23T21:21:12.182-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Design Patterns'/><title type='text'>Singleton Pattern</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog post, I will try to create an example demonstrating the usage of singleton pattern.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;When:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;for situations where you only need one and only one object to exist&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;class needs to be accessible to clients&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;class should not require any parameters for its object creation&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;if creating an object is expensive, singleton can be useful&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;How:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this example, we will try to create a LoadBalancer. LoadBalancer monitors the memory, processor resources etc and then decides which server to assign a particular task to. We won't create all those details as they are not necessary for this demonstration. Instead, we will use the C# Random class to make that decision. Why we need singleton pattern in this case? Because we don't want multiple instances of LoadBalancer. There should only be one LoadBalancer who makes the decision about multiple servers. Let's take a look at the consumer code first:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;        {&lt;br /&gt;            SingletonPatternExample();&lt;br /&gt;&lt;br /&gt;            Console.ReadLine();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SingletonPatternExample()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 20; i++)&lt;br /&gt;            {&lt;br /&gt;                LoadBalancer lb = LoadBalancer.GetLoadBalancer();&lt;br /&gt;                Console.WriteLine(&lt;span class="str"&gt;"Task "&lt;/span&gt; + i + &lt;span class="str"&gt;" is assigned to "&lt;/span&gt; + lb.Server);&lt;br /&gt;                System.Threading.Thread.Sleep(1000);&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this code, I just have a simple for loop to imitate tasks coming in. For every task, we get the LoadBalancer object and assign the Server to it randomly. For this example, you can argue that we could have achieved the same result by declaring the LoadBalancer object outside the for loop. That is true, but that is not the point of this demo. The point here is that singleton pattern is proactively stopping any new instances being created. Let's take a look at the LoadBalancer class now.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;sealed&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; LoadBalancer&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="rem"&gt;// Static members are lazily initialized. &lt;/span&gt;&lt;br /&gt;        &lt;span class="rem"&gt;// .NET guarantees thread safety for static initialization &lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; LoadBalancer instance = &lt;span class="kwrd"&gt;new&lt;/span&gt; LoadBalancer();&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; ArrayList servers = &lt;span class="kwrd"&gt;new&lt;/span&gt; ArrayList();&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; Random random = &lt;span class="kwrd"&gt;new&lt;/span&gt; Random();&lt;br /&gt;&lt;br /&gt;        &lt;span class="rem"&gt;// Note: constructor is private.&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; LoadBalancer()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="rem"&gt;// List of available servers &lt;/span&gt;&lt;br /&gt;            servers.Add(&lt;span class="str"&gt;"Server 1"&lt;/span&gt;);&lt;br /&gt;            servers.Add(&lt;span class="str"&gt;"Server 2"&lt;/span&gt;);&lt;br /&gt;            servers.Add(&lt;span class="str"&gt;"Server 3"&lt;/span&gt;);&lt;br /&gt;            servers.Add(&lt;span class="str"&gt;"Server 4"&lt;/span&gt;);&lt;br /&gt;            servers.Add(&lt;span class="str"&gt;"Server 5"&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; LoadBalancer GetLoadBalancer()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; instance;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="rem"&gt;// Simple, but effective load balancer &lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Server&lt;br /&gt;        {&lt;br /&gt;            get&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;int&lt;/span&gt; r = random.Next(servers.Count);&lt;br /&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; servers[r].ToString();&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    } &lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The class is sealed so that it can not be inherited. Then we define a LoadBalancer instance as private and as static. If you don't define it a static then its not thread-safe. The class also has an ArrayList of servers and a Random object. The class also has a private constructor which just adds the servers to the ArrayList. Private constructor makes sure that class can not be initialized outside the scope. The GetLoadBalancer() method returns the one and only instance. There is no other way to get hold of this instance. And lastly, the simple Server property does the main work of finding out a Server and then notifying it to the caller.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Here are the results:&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-aXHXMP2ZjNY/TVyVsiypx4I/AAAAAAAAB0s/K2kjtixXVEo/s1600/1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-aXHXMP2ZjNY/TVyVsiypx4I/AAAAAAAAB0s/K2kjtixXVEo/s1600/1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-4713888767068948107?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/4713888767068948107'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/4713888767068948107'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/02/singleton-pattern.html' title='Singleton Pattern'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-aXHXMP2ZjNY/TVyVsiypx4I/AAAAAAAAB0s/K2kjtixXVEo/s72-c/1.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-8992025831797701374</id><published>2011-02-01T01:22:00.012-05:00</published><updated>2011-02-02T16:09:55.251-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Design Patterns'/><title type='text'>Factory Pattern</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog post, I will try to create a simple program which will demonstrate the utility and significance of the factory pattern.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;When:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;when many classes are implementing an interface and you are unsure about which concrete implementation to return&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;when you want to separate creation logic from the object's main goal like when creating an object if you want to assign values to many properties and you want to separate this kinda logic from object's main functionality&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;if there are many if-else or switch statements for deciding which object to create&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Why:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;to encapsulate the creation of object&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;to separate the creation of object from the decision about which object to create&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;to allow adding new objects without breaking open closed principle (classes should be open for extension, closed for modification)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;to allow (if needed) storing which objects to create in a separate place like database or configuration&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;How:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Let's create an example to see how factory pattern works. Let's say that we have a consumer which wants to perform search. The consumer doesn't care about which actual search engine does the search, as long as it can perform the search. From a factory perspective, we have multiple search providers and we want some kind of configuration to switch the providers. Let's take a look at the consumer code first.&lt;/span&gt;&lt;/div&gt;&lt;pre class="csharpcode"&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt; Program&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;        {&lt;br /&gt;            ISearchEngineFactory factory = LoadFactory();&lt;br /&gt;&lt;br /&gt;            ISearchEngine engine = factory.CreateMapSearchEngine();&lt;br /&gt;            engine.Search();&lt;br /&gt;&lt;br /&gt;            engine = factory.CreateNormalSearchEngine();&lt;br /&gt;            engine.Search();&lt;br /&gt;&lt;br /&gt;            Console.ReadLine();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; ISearchEngineFactory LoadFactory()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; factoryName = Properties.Settings.Default.SearchFactory;&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; Assembly.GetExecutingAssembly().CreateInstance(factoryName) &lt;span class="kwrd"&gt;as&lt;/span&gt; ISearchEngineFactory;&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this code, first we create a factory by calling the LoadFactory() method. This method looks into the settings file to see which factory to load and then by using reflection creates an instance of that factory. The ISearchEngineFactory defines two methods as shown below: one for normal search and the other for map search.&lt;/span&gt;&lt;/div&gt;&lt;pre class="csharpcode"&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt; &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="kwrd"&gt;interface&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt; ISearchEngineFactory&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;        ISearchEngine CreateNormalSearchEngine();&lt;br /&gt;&lt;br /&gt;        ISearchEngine CreateMapSearchEngine();&lt;br /&gt;    }&lt;/pre&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this example, we are defining two search engines: Google and Bing. So, first up, we create 2 factories for them as shown below:&lt;/span&gt;&lt;/div&gt;&lt;pre class="csharpcode"&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt; &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt; GoogleFactory : ISearchEngineFactory&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; ISearchEngine CreateNormalSearchEngine()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Google() &lt;span class="kwrd"&gt;as&lt;/span&gt; ISearchEngine;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; ISearchEngine CreateMapSearchEngine()&lt;br /&gt;        {&lt;br /&gt;            Google g = &lt;span class="kwrd"&gt;new&lt;/span&gt; Google {Context = &lt;span class="str"&gt;"Maps"&lt;/span&gt;};&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; g &lt;span class="kwrd"&gt;as&lt;/span&gt; ISearchEngine;&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;div&gt;&lt;pre class="csharpcode"&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt; &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt; BingFactory : ISearchEngineFactory&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; ISearchEngine CreateNormalSearchEngine()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Bing() &lt;span class="kwrd"&gt;as&lt;/span&gt; ISearchEngine;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; ISearchEngine CreateMapSearchEngine()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; BingMaps() &lt;span class="kwrd"&gt;as&lt;/span&gt; ISearchEngine;&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this example, we are also demonstrating, how the actual object creation can be different. The Bing engine returns Bing object for normal searches and BingMaps object for map searches. The Google engine for normal search, just returns the Google object with context as null. For maps search, it returns the normal google object with Context set as "Maps". All of these engines, implement the ISearchEngine interface which has a Search method and a context property as shown below.&lt;/span&gt;&lt;/div&gt;&lt;pre class="csharpcode"&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt; &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="kwrd"&gt;interface&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt; ISearchEngine&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;        &lt;span class="kwrd"&gt;void&lt;/span&gt; Search();&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;string&lt;/span&gt; Context { get; set; }&lt;br /&gt;    }&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt; &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt; Bing : ISearchEngine&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Search()&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"You searched on Bing"&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Context { get; set; }&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt; &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt; BingMaps : ISearchEngine&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Search()&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"You searched on Bing Maps"&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Context { get; set; }&lt;br /&gt;    }&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt; &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt; Google : ISearchEngine&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Search()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; str = &lt;span class="str"&gt;"You searched on Google"&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;string&lt;/span&gt;.IsNullOrEmpty(Context))&lt;br /&gt;                Console.WriteLine(str);&lt;br /&gt;            &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;br /&gt;                Console.WriteLine(str + &lt;span class="str"&gt;" "&lt;/span&gt; + Context);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Context { get; set; }&lt;br /&gt;    }&lt;/pre&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;When you run this app, with search provider set as Google, you see the following output.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TUmOWGpYAmI/AAAAAAAAB0Y/BPoiWR6phxY/s1600/settingsgoogle.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TUmOWGpYAmI/AAAAAAAAB0Y/BPoiWR6phxY/s1600/settingsgoogle.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TUmOWGpYAmI/AAAAAAAAB0Y/BPoiWR6phxY/s1600/settingsgoogle.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;/a&gt;&lt;a href="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TUmOVO3pswI/AAAAAAAAB0Q/QdLDDzgKQ-8/s1600/google.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TUmOVO3pswI/AAAAAAAAB0Q/QdLDDzgKQ-8/s1600/google.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;And if you go to settings page and change the settings configuration to use Bing as the search provider, the output is as below:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TUmOVaeZElI/AAAAAAAAB0U/r1cFFg9sTUM/s1600/settingsbing.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TUmOVaeZElI/AAAAAAAAB0U/r1cFFg9sTUM/s1600/settingsbing.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TUmOUpa9NsI/AAAAAAAAB0M/mC1qkbfX2Tk/s1600/bing.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TUmOUpa9NsI/AAAAAAAAB0M/mC1qkbfX2Tk/s1600/bing.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Once we have set up the factory pattern in our code, we can easily add newer factories and newer search engine classes without affecting any of the other classes, hence respecting the Open-Closed principle. Once that is done, you can easily change the configuration to point to say Yahoo factory and the factory pattern will take care of correct object creations. &lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-8992025831797701374?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/8992025831797701374'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/8992025831797701374'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/02/factory-pattern.html' title='Factory Pattern'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TUmOWGpYAmI/AAAAAAAAB0Y/BPoiWR6phxY/s72-c/settingsgoogle.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-623862641647225475</id><published>2011-01-21T01:23:00.001-05:00</published><updated>2011-01-21T16:19:24.516-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><title type='text'>Migration from 32bit to 64bit Projects in .NET</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog post, I am going to explain the solution for a few issues that I had working with 32bit and 64 bit Operating systems and what we should do to create applications that would work without any code changes in any environments.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If you go to project properties and then navigate to Build tab, you will see a dropdown for Platform target. By default, this value is set to "Any CPU". You can change it to specifically target x86 (32bit) and x64 (64bit) environments.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;So, first I created 3 projects. Lets call them -&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;A - Console app. This will be the startup project and hence the calling assembly.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;B - Class library. A just calls a method in a class in B.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;C - Class library. B just calls a method in a class in C.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;So when we run the app, it will work on both 32bit and 64bit OSs because by default the target was set to "Any CPU". So if all this works then why this blog?&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this example, we had the code for all three DLLs. Sometimes, our code refers to other DLLs, on which we have no control. Recently in one of the projects I was working, we were referencing SQL CE v3.0 DLLs which worked only on 32bit environment. This case can be recreated by changing B's target platform to x86 and running the program on a 64bit machine. What will happen in this case is -&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;A is "AnyCPU" and machine is 64bit so A would be running in 64bit environment&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;A will call a method in a class inside B.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;B is "x86" so it will not run in 64bit.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;So now, a 64bit DLL is calling a 32bit DLL. This is not allowed and an error will be thrown.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;What is the resolution? We have 3 -&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If you have the code for B, you can change the platform target for B from x86 to "AnyCPU"&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If you don't have code for B, then you can try to search for a 64bit version of B's assembly&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If this also doesn't work, you can simply change the calling assembly i.e. A's platform target to "x86" and the app will work.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-623862641647225475?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/623862641647225475'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/623862641647225475'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/01/migration-from-32bit-to-64bit-projects.html' title='Migration from 32bit to 64bit Projects in .NET'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-6387807961765759749</id><published>2011-01-16T15:47:00.002-05:00</published><updated>2011-02-21T09:07:06.413-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='HTML Agility Pack'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='LINQ'/><category scheme='http://www.blogger.com/atom/ns#' term='HTML'/><title type='text'>Reading a web page</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Many times there is a need to read a web page programatically. That is, get the html through code and parse through it to search what you want. Codeplex has a DLL named HTML Agility Pack (HAP) that comes to our rescue.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Download the DLL and then add a reference to it in your application. The DLL allows us to access the html code from the url provided and then exposes a DOM for us to navigate through. With HAP you can read, search, modify and save the web pages. Of course, modifications and save will only result in a local file and not persist the changes on the web page itself.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog, we will create two methods: one to get all the meta tags on the web page and the other to get all the links on the page. The code is shown below:&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; btnGetMetaTags_Click(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            tbResult.Text = &lt;span class="kwrd"&gt;string&lt;/span&gt;.Empty;&lt;br /&gt;            var webGet = &lt;span class="kwrd"&gt;new&lt;/span&gt; HtmlWeb();&lt;br /&gt;            var document = webGet.Load(tbUrl.Text);&lt;br /&gt;&lt;br /&gt;            var metaTags = document.DocumentNode.Descendants(&lt;span class="str"&gt;"meta"&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (metaTags != &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var tag &lt;span class="kwrd"&gt;in&lt;/span&gt; metaTags)&lt;br /&gt;                {&lt;br /&gt;                    &lt;span class="kwrd"&gt;if&lt;/span&gt; (tag.Attributes[&lt;span class="str"&gt;"name"&lt;/span&gt;] != &lt;span class="kwrd"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; tag.Attributes[&lt;span class="str"&gt;"content"&lt;/span&gt;] != &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;br /&gt;                    {&lt;br /&gt;                        tbResult.Text += &lt;span class="str"&gt;"Name: "&lt;/span&gt; + tag.Attributes[&lt;span class="str"&gt;"name"&lt;/span&gt;].Value + Environment.NewLine;&lt;br /&gt;                        tbResult.Text += &lt;span class="str"&gt;"Content: "&lt;/span&gt; + tag.Attributes[&lt;span class="str"&gt;"content"&lt;/span&gt;].Value + Environment.NewLine;&lt;br /&gt;                        tbResult.Text += Environment.NewLine;&lt;br /&gt;                    }&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; btnGetAllLinks_Click(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            tbResult.Text = &lt;span class="kwrd"&gt;string&lt;/span&gt;.Empty;&lt;br /&gt;            var webGet = &lt;span class="kwrd"&gt;new&lt;/span&gt; HtmlWeb();&lt;br /&gt;            var document = webGet.Load(tbUrl.Text);&lt;br /&gt;&lt;br /&gt;            var linksOnPage = document.DocumentNode.Descendants(&lt;span class="str"&gt;"a"&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var lnk &lt;span class="kwrd"&gt;in&lt;/span&gt; linksOnPage)&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;if&lt;/span&gt; (lnk.Attributes[&lt;span class="str"&gt;"href"&lt;/span&gt;] != &lt;span class="kwrd"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; lnk.InnerText.Trim().Length &amp;gt; 0)&lt;br /&gt;                {&lt;br /&gt;                    tbResult.Text += &lt;span class="str"&gt;"URL: "&lt;/span&gt; + lnk.Attributes[&lt;span class="str"&gt;"href"&lt;/span&gt;].Value + Environment.NewLine;&lt;br /&gt;                    tbResult.Text += &lt;span class="str"&gt;"InnerText: "&lt;/span&gt; + lnk.InnerText.Trim() + Environment.NewLine;&lt;br /&gt;                    tbResult.Text += Environment.NewLine;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In the code, first we create and load a HtmlWeb document from the supplied url. Then we access its DocumentNode property and use LINQ to further access all the descendants.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Once you run the application and click on "Get Meta Tags" button, you get the following result:&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_a_Uk_rwmZ7g/TTNYg6nHl3I/AAAAAAAAB0A/sbCXr1ApVXI/s1600/1.jpg" imageanchor="1"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/_a_Uk_rwmZ7g/TTNYg6nHl3I/AAAAAAAAB0A/sbCXr1ApVXI/s1600/1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;When you click on the "Get all links on the page" button, you get the following result:&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_a_Uk_rwmZ7g/TTNYhJL-UBI/AAAAAAAAB0E/UafNMx911L8/s1600/2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/_a_Uk_rwmZ7g/TTNYhJL-UBI/AAAAAAAAB0E/UafNMx911L8/s1600/2.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;You can also modify the results in a similar fashion and then save them by calling the Save method of the document.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-6387807961765759749?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/6387807961765759749'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/6387807961765759749'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/01/reading-web-page.html' title='Reading a web page'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_a_Uk_rwmZ7g/TTNYg6nHl3I/AAAAAAAAB0A/sbCXr1ApVXI/s72-c/1.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-4421885122540109948</id><published>2011-01-06T21:20:00.008-05:00</published><updated>2011-01-06T22:01:31.767-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='MVVM'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Windows Phone 7'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Saving Application State in Windows Phone 7</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In the last post, I created a small app to store the page state. In this blog post, &amp;nbsp;I will try to create an app that stores some counters across the application lifetime. We will store two counters: 1. How many times this app was used 2. Last datetime this app was used.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;We will create a simple app using the MVVM pattern. First up, lets take a look at the ViewModel called CounterViewModel.&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CounterViewModel : INotifyPropertyChanged&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;event&lt;/span&gt; PropertyChangedEventHandler PropertyChanged;&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; OnPropertyChanged(&lt;span class="kwrd"&gt;string&lt;/span&gt; propertyName)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (PropertyChanged != &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;br /&gt;                PropertyChanged(&lt;span class="kwrd"&gt;this&lt;/span&gt;, &lt;span class="kwrd"&gt;new&lt;/span&gt; PropertyChangedEventArgs(propertyName));&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; m_AppCounter;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; AppCounter&lt;br /&gt;        {&lt;br /&gt;            get&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; m_AppCounter;&lt;br /&gt;            }&lt;br /&gt;            set&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;if&lt;/span&gt; (m_AppCounter != &lt;span class="kwrd"&gt;value&lt;/span&gt;)&lt;br /&gt;                {&lt;br /&gt;                    m_AppCounter = &lt;span class="kwrd"&gt;value&lt;/span&gt;;&lt;br /&gt;                    OnPropertyChanged(&lt;span class="str"&gt;"AppCounter"&lt;/span&gt;);&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; DateTime m_LastVisit;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; DateTime LastVisit&lt;br /&gt;        {&lt;br /&gt;            get&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; m_LastVisit;&lt;br /&gt;            }&lt;br /&gt;            set&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;if&lt;/span&gt; (m_LastVisit != &lt;span class="kwrd"&gt;value&lt;/span&gt;)&lt;br /&gt;                {&lt;br /&gt;                    m_LastVisit = &lt;span class="kwrd"&gt;value&lt;/span&gt;;&lt;br /&gt;                    OnPropertyChanged(&lt;span class="str"&gt;"LastVisit"&lt;/span&gt;);&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; DateTime SessionStartTime { get; set; }&lt;br /&gt;    }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The ViewModel implements the INotifyPropertyChanged interface to update UI whenever properties change. AppCounter is to store the number of times this app is used. LastVisit is to display the time this app was last used. SessionStartTime is a variable to store current time when this app was launched/activated. When we close/deactivate the app, we change the LastVisit to store SessionStartTime. Now, lets take a look at the App.Xaml where we supply values to these variables:&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; CounterViewModel CounterViewModel { get; set; }&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Application_Launching(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, LaunchingEventArgs e)&lt;br /&gt;{&lt;br /&gt;       CounterViewModel = CounterRepository.GetCounters();&lt;br /&gt;       CounterViewModel.AppCounter++;&lt;br /&gt;       CounterViewModel.SessionStartTime = DateTime.Now;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Application_Activated(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, ActivatedEventArgs e)&lt;br /&gt;{&lt;br /&gt;    CounterViewModel.AppCounter++;&lt;br /&gt;    CounterViewModel.SessionStartTime = DateTime.Now;&lt;br /&gt;    CounterViewModel = CounterRepository.GetCounters();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Application_Deactivated(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, DeactivatedEventArgs e)&lt;br /&gt;{&lt;br /&gt;    CounterViewModel.LastVisit = CounterViewModel.SessionStartTime;&lt;br /&gt;    CounterRepository.SaveCounters(CounterViewModel);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Application_Closing(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, ClosingEventArgs e)&lt;br /&gt;{&lt;br /&gt;    CounterViewModel.LastVisit = CounterViewModel.SessionStartTime;&lt;br /&gt;    CounterRepository.SaveCounters(CounterViewModel);&lt;br /&gt;}&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;First, we add a static variable for the ViewModel in the app.xaml class. This static object will store our counters. In the Launching/Activated method, we get the counters from our store (we will see that in a bit). We increase the app counter and store the current time in SessionStartTime.&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In the Closing/Deactivated, we assign the LastVisit variable to store SessionStartTime and then we save the counters. We should be careful about what code we put in Launching/Activated methods, as there is a time limit of 10 secs. If within 10 seconds these methods don't finish execution, an exception is thrown.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The CounterRepository class code is shown below:&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CounterRepository&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; CounterViewModel GetCounters()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (PhoneApplicationService.Current != &lt;span class="kwrd"&gt;null&lt;/span&gt;&lt;br /&gt;                &amp;amp;&amp;amp; PhoneApplicationService.Current.State.ContainsKey(&lt;span class="str"&gt;"counterViewModel"&lt;/span&gt;)&lt;br /&gt;                &amp;amp;&amp;amp; PhoneApplicationService.Current.State[&lt;span class="str"&gt;"counterViewModel"&lt;/span&gt;] &lt;span class="kwrd"&gt;is&lt;/span&gt; CounterViewModel)&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; PhoneApplicationService.Current.State[&lt;span class="str"&gt;"counterViewModel"&lt;/span&gt;] &lt;span class="kwrd"&gt;as&lt;/span&gt; CounterViewModel;&lt;br /&gt;            }&lt;br /&gt;            &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;br /&gt;            {&lt;br /&gt;                IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();&lt;br /&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;if&lt;/span&gt; (store.DirectoryExists(&lt;span class="str"&gt;"data"&lt;/span&gt;) &amp;amp;&amp;amp; store.FileExists(&lt;span class="str"&gt;"data\\counters.xml"&lt;/span&gt;))&lt;br /&gt;                {&lt;br /&gt;                    &lt;span class="kwrd"&gt;using&lt;/span&gt; (var file = store.OpenFile(&lt;span class="str"&gt;"data\\counters.xml"&lt;/span&gt;, FileMode.Open))&lt;br /&gt;                    {&lt;br /&gt;                        var serializer = &lt;span class="kwrd"&gt;new&lt;/span&gt; XmlSerializer(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(CounterViewModel));&lt;br /&gt;                        var result = serializer.Deserialize(file);&lt;br /&gt;                        &lt;span class="kwrd"&gt;return&lt;/span&gt; result &lt;span class="kwrd"&gt;as&lt;/span&gt; CounterViewModel;&lt;br /&gt;                    }&lt;br /&gt;                }&lt;br /&gt;                &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;br /&gt;                {&lt;br /&gt;                    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; CounterViewModel()&lt;br /&gt;                    {&lt;br /&gt;                        AppCounter = 0&lt;br /&gt;                    };&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SaveCounters(CounterViewModel cvm)&lt;br /&gt;        {&lt;br /&gt;            SaveToPhoneApplicationService(cvm);&lt;br /&gt;            SaveToIsolatedStorage(cvm);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SaveToPhoneApplicationService(CounterViewModel cvm)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (PhoneApplicationService.Current != &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;br /&gt;                PhoneApplicationService.Current.State[&lt;span class="str"&gt;"counterViewModel"&lt;/span&gt;] = cvm;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SaveToIsolatedStorage(CounterViewModel cvm)&lt;br /&gt;        {&lt;br /&gt;            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (!store.DirectoryExists(&lt;span class="str"&gt;"data"&lt;/span&gt;))&lt;br /&gt;                store.CreateDirectory(&lt;span class="str"&gt;"data"&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (store.FileExists(&lt;span class="str"&gt;"data\\counters.xml"&lt;/span&gt;))&lt;br /&gt;                store.DeleteFile(&lt;span class="str"&gt;"data\\counters.xml"&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;using&lt;/span&gt; (var file = store.CreateFile(&lt;span class="str"&gt;"data\\counters.xml"&lt;/span&gt;))&lt;br /&gt;            {&lt;br /&gt;                var serializer = &lt;span class="kwrd"&gt;new&lt;/span&gt; XmlSerializer(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(CounterViewModel));&lt;br /&gt;                serializer.Serialize(file, cvm);&lt;br /&gt;&lt;br /&gt;                file.Flush();&lt;br /&gt;                file.Close();&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In the SaveCounters method, we take care of both tombstoning and closing the application cases. In case, user moves away from your app by opening a browser, application is not closed but deactivated. This is called tombstoning. In case, the user presses a back button, the application is closed. In case of tombstoning, we have the option of using PhoneApplicationService. This class provides access to various aspects of Application lifetime. In case the application is closed, PhoneApplicationService will not exist. So we use the concept of IsolatedStorage to store the variables outside of the application in phone's memory store. We create a folder to store the data. Then we take the ViewModel and serialize to get an xml file, which is then stored into the folder.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In the GetCounters method, we do the reverse. We first check if the counters are available in the PhoneApplicationService. If they are not, we fetch the counters from IsolatedStorage.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Lastly, lets take a look at the XAML file, which is pretty simple.&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;="ContentPanel"&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Row&lt;/span&gt;&lt;span class="kwrd"&gt;="1"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Grid.RowDefinitions&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;RowDefinition&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;="Auto"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;RowDefinition&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;="Auto"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Grid.RowDefinitions&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Grid.ColumnDefinitions&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ColumnDefinition&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ColumnDefinition&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Grid.ColumnDefinitions&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TextBlock&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Row&lt;/span&gt;&lt;span class="kwrd"&gt;="0"&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Column&lt;/span&gt;&lt;span class="kwrd"&gt;="0"&lt;/span&gt; &lt;span class="attr"&gt;FontSize&lt;/span&gt;&lt;span class="kwrd"&gt;="24"&lt;/span&gt; &lt;span class="attr"&gt;Text&lt;/span&gt;&lt;span class="kwrd"&gt;="App Counter: "&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TextBlock&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Row&lt;/span&gt;&lt;span class="kwrd"&gt;="0"&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Column&lt;/span&gt;&lt;span class="kwrd"&gt;="1"&lt;/span&gt; &lt;span class="attr"&gt;FontSize&lt;/span&gt;&lt;span class="kwrd"&gt;="24"&lt;/span&gt; &lt;span class="attr"&gt;Text&lt;/span&gt;&lt;span class="kwrd"&gt;="{Binding Path=AppCounter}"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TextBlock&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Row&lt;/span&gt;&lt;span class="kwrd"&gt;="1"&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Column&lt;/span&gt;&lt;span class="kwrd"&gt;="0"&lt;/span&gt; &lt;span class="attr"&gt;FontSize&lt;/span&gt;&lt;span class="kwrd"&gt;="24"&lt;/span&gt; &lt;span class="attr"&gt;Text&lt;/span&gt;&lt;span class="kwrd"&gt;="Last Visit: "&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TextBlock&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Row&lt;/span&gt;&lt;span class="kwrd"&gt;="1"&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Column&lt;/span&gt;&lt;span class="kwrd"&gt;="1"&lt;/span&gt; &lt;span class="attr"&gt;FontSize&lt;/span&gt;&lt;span class="kwrd"&gt;="24"&lt;/span&gt; &lt;span class="attr"&gt;Text&lt;/span&gt;&lt;span class="kwrd"&gt;="{Binding Path=LastVisit}"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In the cs file, we just set the DataContext property to the ViewModel. And then in the XAML, we bind the TextBlocks to display these values.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;When we start the app, you see the count as 1 and datetime as default datetime. This can be easily changed to probably show no last visit date. This scenario is displayed in image 1. Then, click the hardware search button to depict the Deactivated case. Image 2 shows the search screen that comes up. Then click the hardware back button to go back to the app and activate it. As you can see in image 3, the counter has increased and last visit datetime has changed. In this case, these values were stored in the PhoneApplicationService. Then, click on the hardware back button again to depict the closing case. The application is closed and you see image 4. Once you click the AppCounter app, you again see the results as in image 5.&lt;/span&gt;&lt;br /&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;td style="text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_a_Uk_rwmZ7g/TSZ5j7RAISI/AAAAAAAABv4/GecwAjlRO8c/s1600/1.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="320" src="http://4.bp.blogspot.com/_a_Uk_rwmZ7g/TSZ5j7RAISI/AAAAAAAABv4/GecwAjlRO8c/s320/1.jpg" width="175" /&gt;&lt;/a&gt;&lt;/td&gt; &lt;td style="text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_a_Uk_rwmZ7g/TSZ5kX02HVI/AAAAAAAABv8/l17P--UgiDs/s1600/2.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="320" src="http://1.bp.blogspot.com/_a_Uk_rwmZ7g/TSZ5kX02HVI/AAAAAAAABv8/l17P--UgiDs/s320/2.jpg" width="174" /&gt;&lt;/a&gt;&lt;/td&gt; &lt;td style="text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_a_Uk_rwmZ7g/TSZ5k2X8u5I/AAAAAAAABwA/ORgTnJJi1sE/s1600/3.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="320" src="http://4.bp.blogspot.com/_a_Uk_rwmZ7g/TSZ5k2X8u5I/AAAAAAAABwA/ORgTnJJi1sE/s320/3.jpg" width="174" /&gt;&lt;/a&gt;&lt;/td&gt; &lt;td style="text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_a_Uk_rwmZ7g/TSZ5lfqEieI/AAAAAAAABwE/c8XnBzjFhzQ/s1600/4.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="320" src="http://4.bp.blogspot.com/_a_Uk_rwmZ7g/TSZ5lfqEieI/AAAAAAAABwE/c8XnBzjFhzQ/s320/4.jpg" width="174" /&gt;&lt;/a&gt;&lt;/td&gt; &lt;td style="text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TSZ5mJl3q3I/AAAAAAAABwI/aIq_UVS91qM/s1600/5.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="320" src="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TSZ5mJl3q3I/AAAAAAAABwI/aIq_UVS91qM/s320/5.jpg" width="174" /&gt;&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="tr-caption" style="text-align: center;"&gt;1&lt;/td&gt; &lt;td class="tr-caption" style="text-align: center;"&gt;2&lt;/td&gt; &lt;td class="tr-caption" style="text-align: center;"&gt;3&lt;/td&gt; &lt;td class="tr-caption" style="text-align: center;"&gt;4&lt;/td&gt; &lt;td class="tr-caption" style="text-align: center;"&gt;5&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-4421885122540109948?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/4421885122540109948'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/4421885122540109948'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/01/saving-application-state-in-windows.html' title='Saving Application State in Windows Phone 7'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_a_Uk_rwmZ7g/TSZ5j7RAISI/AAAAAAAABv4/GecwAjlRO8c/s72-c/1.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-5312236176720020914</id><published>2011-01-04T04:36:00.011-05:00</published><updated>2011-01-05T10:19:48.917-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Windows Phone 7'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Saving Page State in Windows Phone 7</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog post, I will try to create a sample app in which we store the page state. By default, once you move away from your windows phone app, the page state is not stored. We need to take measures to store the page state when deactivating and restore the page state when activating.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Read on to see the code snippets and explanation -&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Let's look at the XAML first -&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;="ContentPanel"&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Row&lt;/span&gt;&lt;span class="kwrd"&gt;="1"&lt;/span&gt; &lt;span class="attr"&gt;Margin&lt;/span&gt;&lt;span class="kwrd"&gt;="12,0,12,0"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;StackPanel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;         &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TextBlock&lt;/span&gt; &lt;span class="attr"&gt;Text&lt;/span&gt;&lt;span class="kwrd"&gt;="Move the slider"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;         &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Slider&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;="sl"&lt;/span&gt; &lt;span class="attr"&gt;Maximum&lt;/span&gt;&lt;span class="kwrd"&gt;="100"&lt;/span&gt; &lt;span class="attr"&gt;Minimum&lt;/span&gt;&lt;span class="kwrd"&gt;="0"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;         &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TextBox&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;="tb"&lt;/span&gt; &lt;span class="attr"&gt;Text&lt;/span&gt;&lt;span class="kwrd"&gt;="{Binding ElementName=sl, Path=Value, Mode=TwoWay}"&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;TextBox&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;StackPanel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this XAML, I just have one slider and a textbox bound to slider value, so that it gives us a visual representation of the slider value. The Mode is TwoWay so I can update the value in textbox and slider will position itself accordingly.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Let's take a look at the .cs file now -&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MainPage : PhoneApplicationPage&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;bool&lt;/span&gt; isNewInstance = &lt;span class="kwrd"&gt;false&lt;/span&gt;;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; MainPage()&lt;br /&gt;        {&lt;br /&gt;            InitializeComponent();&lt;br /&gt;            isNewInstance = &lt;span class="kwrd"&gt;true&lt;/span&gt;;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;base&lt;/span&gt;.OnNavigatedFrom(e);&lt;br /&gt;&lt;br /&gt;            PageStateSaver.PreserveSliderState(&lt;span class="kwrd"&gt;this&lt;/span&gt;.State, &lt;span class="kwrd"&gt;this&lt;/span&gt;.sl);&lt;br /&gt;&lt;br /&gt;            isNewInstance = &lt;span class="kwrd"&gt;false&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;this&lt;/span&gt;.State[&lt;span class="str"&gt;"PreservingPageState"&lt;/span&gt;] = &lt;span class="kwrd"&gt;true&lt;/span&gt;;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;base&lt;/span&gt;.OnNavigatedTo(e);&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (isNewInstance &amp;amp;&amp;amp; &lt;span class="kwrd"&gt;this&lt;/span&gt;.State.ContainsKey(&lt;span class="str"&gt;"PreservingPageState"&lt;/span&gt;))&lt;br /&gt;            {&lt;br /&gt;                PageStateSaver.RestoreSliderState(&lt;span class="kwrd"&gt;this&lt;/span&gt;.State, &lt;span class="kwrd"&gt;this&lt;/span&gt;.sl, 0.0);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this code, we have overridden two events. OnNavigatedFrom is called when you are navigating away from the application. In this method, we store the state of slider. OnNavigatedTo is called when we are navigating back to the application. In this method, we restore the state of the slider. PageStateSaver is a static class with these helper methods. Let's take a look at the class code-&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; PageStateSaver&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PreserveSliderState(System.Collections.Generic.IDictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt; state, Slider slider)&lt;br /&gt;        {&lt;br /&gt;            state[slider.Name + &lt;span class="str"&gt;"_Value"&lt;/span&gt;] = slider.Value;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; RestoreSliderState(System.Collections.Generic.IDictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt; state, Slider slider, &lt;span class="kwrd"&gt;double&lt;/span&gt; defaultVal)&lt;br /&gt;        {&lt;br /&gt;            slider.Value = TryGetValue&amp;lt;Double&amp;gt;(state, slider.Name + &lt;span class="str"&gt;"_Value"&lt;/span&gt;, defaultVal);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; T TryGetValue&amp;lt;T&amp;gt;(IDictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt; state, &lt;span class="kwrd"&gt;string&lt;/span&gt; name, T defaultValue)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (state.ContainsKey(name))&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;if&lt;/span&gt; (state[name] != &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;br /&gt;                    &lt;span class="kwrd"&gt;return&lt;/span&gt; (T)state[name];&lt;br /&gt;            }&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; defaultValue;&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In PreserveSliderState method, we are passing the page's state and the control itself. As you can see, page's state is nothing but a dictionary of string keys and object values. We create our own key and save the slider value object. In RestoreSliderState, we do the opposite. I just copied the TryGetValue code from MSDN, so that when we had multiple controls, we can use the same generic method.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Let's see the results. Image 1 below shows the screen when you startup.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_a_Uk_rwmZ7g/TSOjGkWJL1I/AAAAAAAABvc/3UMCsOLwsGY/s1600/2.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"&gt;&lt;img border="0" height="320" src="http://2.bp.blogspot.com/_a_Uk_rwmZ7g/TSOjGkWJL1I/AAAAAAAABvc/3UMCsOLwsGY/s320/2.jpg" width="169" /&gt;&lt;/a&gt;&lt;a href="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TSOdXV0FeII/AAAAAAAABvY/MHkfjJoNEY0/s1600/1.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" src="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TSOdXV0FeII/AAAAAAAABvY/MHkfjJoNEY0/s320/1.jpg" width="169" /&gt;&lt;/a&gt;&lt;a href="http://2.bp.blogspot.com/_a_Uk_rwmZ7g/TSOjGkWJL1I/AAAAAAAABvc/3UMCsOLwsGY/s1600/2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" src="http://2.bp.blogspot.com/_a_Uk_rwmZ7g/TSOjGkWJL1I/AAAAAAAABvc/3UMCsOLwsGY/s320/2.jpg" width="169" /&gt;&lt;/a&gt;&lt;a href="http://2.bp.blogspot.com/_a_Uk_rwmZ7g/TSOmKQ4cbTI/AAAAAAAABvg/v30_J1kDsmc/s1600/3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" src="http://2.bp.blogspot.com/_a_Uk_rwmZ7g/TSOmKQ4cbTI/AAAAAAAABvg/v30_J1kDsmc/s320/3.jpg" width="166" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Move the slider or put a number between 0 and 100 in the textbox and tab out to move out of textbox. This will change the slider value. I changed the value to 50 as shown in the image 2 (BTW, if you press pg up in the emulator, you can use the keyboard to type in values)&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Now click on the search button below. This will call the OnNavigatedFrom method and the state will be stored. The search screen will look something similar to image 3.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Now press the back button. When you press the back button, OnNavigatedTo method is called and the state will be restored. The screen will now look exactly as the second image (image 4).&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If you comment the OnNavigatedTo and OnNavigatedFrom methods and try this scenario again, you will see that the page state is not stored.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;I also found a useful DLL to look at the counters by following steps in the link&amp;nbsp;&lt;/span&gt;&lt;a href="http://blogs.msdn.com/b/mikeormond/archive/2010/12/16/monitoring-memory-usage-on-windows-phone-7.aspx"&gt;here&lt;/a&gt;&amp;nbsp;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-5312236176720020914?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/5312236176720020914'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/5312236176720020914'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/01/saving-page-state-in-windows-phone-7.html' title='Saving Page State in Windows Phone 7'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_a_Uk_rwmZ7g/TSOjGkWJL1I/AAAAAAAABvc/3UMCsOLwsGY/s72-c/2.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-245981110742379603</id><published>2011-01-03T23:20:00.000-05:00</published><updated>2011-01-07T23:21:31.530-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Windows Phone 7'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>IValueConverter Example</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;WPF and Silverlight extensively use XAML and data binding. Sometimes, such scenarios come up where we need to bind a control to a property but the property value needs to be converted to something which makes sense for the control. That's where IValueConverter comes in.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;We will try to create a Windows Phone app where if you enter a number between 1 to 7, a textbox will show the weekday names. 1 is Monday and 7 is Sunday. Let's take a look at the xaml first.&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;phone:PhoneApplicationPage.Resources&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;src:DayNumConverter&lt;/span&gt; &lt;span class="attr"&gt;x:Key&lt;/span&gt;&lt;span class="kwrd"&gt;="DayConverterKey"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;phone:PhoneApplicationPage.Resources&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;="LayoutRoot"&lt;/span&gt; &lt;span class="attr"&gt;Background&lt;/span&gt;&lt;span class="kwrd"&gt;="Transparent"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Grid.RowDefinitions&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;RowDefinition&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;="Auto"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;RowDefinition&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;="*"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Grid.RowDefinitions&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;StackPanel&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;="TitlePanel"&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Row&lt;/span&gt;&lt;span class="kwrd"&gt;="0"&lt;/span&gt; &lt;span class="attr"&gt;Margin&lt;/span&gt;&lt;span class="kwrd"&gt;="12,17,0,28"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TextBlock&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;="ApplicationTitle"&lt;/span&gt; &lt;span class="attr"&gt;Text&lt;/span&gt;&lt;span class="kwrd"&gt;="MY APPLICATION"&lt;/span&gt; &lt;span class="attr"&gt;Style&lt;/span&gt;&lt;span class="kwrd"&gt;="{StaticResource PhoneTextNormalStyle}"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TextBlock&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;="PageTitle"&lt;/span&gt; &lt;span class="attr"&gt;Text&lt;/span&gt;&lt;span class="kwrd"&gt;="page name"&lt;/span&gt; &lt;span class="attr"&gt;Margin&lt;/span&gt;&lt;span class="kwrd"&gt;="9,-7,0,0"&lt;/span&gt; &lt;span class="attr"&gt;Style&lt;/span&gt;&lt;span class="kwrd"&gt;="{StaticResource PhoneTextTitle1Style}"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;StackPanel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;="ContentPanel"&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Row&lt;/span&gt;&lt;span class="kwrd"&gt;="1"&lt;/span&gt; &lt;span class="attr"&gt;Margin&lt;/span&gt;&lt;span class="kwrd"&gt;="12,0,12,0"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;StackPanel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TextBlock&lt;/span&gt; &lt;span class="attr"&gt;Text&lt;/span&gt;&lt;span class="kwrd"&gt;="Enter a number in the range of 1 to 7"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TextBox&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;="tbDayNum"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TextBlock&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;="tb"&lt;/span&gt; &lt;span class="attr"&gt;Text&lt;/span&gt;&lt;span class="kwrd"&gt;="{Binding Path=Text, ElementName=tbDayNum, Converter={StaticResource DayConverterKey}}"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;StackPanel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Here, first up, we have created a static resource and assigned it a key. Then we bind the textblock's text property to textbox's text property and apply a converter to it. The code for converter &amp;nbsp;class is shown below:&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; DayNumConverter : IValueConverter&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; Convert(&lt;span class="kwrd"&gt;object&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt;, Type targetType, &lt;span class="kwrd"&gt;object&lt;/span&gt; parameter, System.Globalization.CultureInfo culture)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;value&lt;/span&gt; == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;br /&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"No Day Selected"&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; str = &lt;span class="kwrd"&gt;value&lt;/span&gt;.ToString();&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;string&lt;/span&gt;.IsNullOrEmpty(str))&lt;br /&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"No Day Selected"&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;int&lt;/span&gt; num = 0;&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (Int32.TryParse(str, &lt;span class="kwrd"&gt;out&lt;/span&gt; num))&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;switch&lt;/span&gt; (num)&lt;br /&gt;                {&lt;br /&gt;                    &lt;span class="kwrd"&gt;case&lt;/span&gt; 1:&lt;br /&gt;                        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"Monday"&lt;/span&gt;;&lt;br /&gt;                    &lt;span class="kwrd"&gt;case&lt;/span&gt; 2:&lt;br /&gt;                        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"Tuesday"&lt;/span&gt;;&lt;br /&gt;                    &lt;span class="kwrd"&gt;case&lt;/span&gt; 3:&lt;br /&gt;                        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"Wednesday"&lt;/span&gt;;&lt;br /&gt;                    &lt;span class="kwrd"&gt;case&lt;/span&gt; 4:&lt;br /&gt;                        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"Thursday"&lt;/span&gt;;&lt;br /&gt;                    &lt;span class="kwrd"&gt;case&lt;/span&gt; 5:&lt;br /&gt;                        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"Friday"&lt;/span&gt;;&lt;br /&gt;                    &lt;span class="kwrd"&gt;case&lt;/span&gt; 6:&lt;br /&gt;                        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"Saturday"&lt;/span&gt;;&lt;br /&gt;                    &lt;span class="kwrd"&gt;case&lt;/span&gt; 7:&lt;br /&gt;                        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"Sunday"&lt;/span&gt;;&lt;br /&gt;                    &lt;span class="kwrd"&gt;default&lt;/span&gt;:&lt;br /&gt;                        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"Not a valid number"&lt;/span&gt;;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;            &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"Not a valid number"&lt;/span&gt;;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; ConvertBack(&lt;span class="kwrd"&gt;object&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt;, Type targetType, &lt;span class="kwrd"&gt;object&lt;/span&gt; parameter, System.Globalization.CultureInfo culture)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NotImplementedException();&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;This class does nothing but implements the IValueConverter interface. This means that everytime binding will take place, the value will be passed to this converter class before binding it to control's value. If you run the app you will see the following results:&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TSflhzSj-RI/AAAAAAAABzg/RICBXGq8oa8/s1600/3.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"&gt;&lt;img border="0" height="320" src="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TSflhzSj-RI/AAAAAAAABzg/RICBXGq8oa8/s320/3.jpg" style="cursor: move;" width="168" /&gt;&lt;/a&gt;&lt;a href="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TSflgHM2CII/AAAAAAAABzY/yM1LOnK8z8I/s1600/1.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" src="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TSflgHM2CII/AAAAAAAABzY/yM1LOnK8z8I/s320/1.jpg" width="171" /&gt;&lt;/a&gt;&lt;a href="http://1.bp.blogspot.com/_a_Uk_rwmZ7g/TSflguNDZZI/AAAAAAAABzc/F760JtfyCfA/s1600/2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" src="http://1.bp.blogspot.com/_a_Uk_rwmZ7g/TSflguNDZZI/AAAAAAAABzc/F760JtfyCfA/s320/2.jpg" width="168" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-245981110742379603?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/245981110742379603'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/245981110742379603'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/01/ivalueconverter-example.html' title='IValueConverter Example'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TSflhzSj-RI/AAAAAAAABzg/RICBXGq8oa8/s72-c/3.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-178647574202627738</id><published>2011-01-03T21:33:00.009-05:00</published><updated>2011-01-07T20:57:48.134-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Windows Phone 7'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Windows Phone 7 - Overview</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Happy New Year 2011!!&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog post, I will take a look at Windows Phone 7 and how to develop an application for this platform. Why should we develop for Windows Phone 7 -&lt;/span&gt;&lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;There is a huge market for mobile apps&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Consumers expect all of their apps (even business apps) to have mobile components&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;A Windows Phone 7 has 6 hubs. A hub is nothing but a kind of grouping for items that are related. The 6 hubs are -&lt;/span&gt;&lt;/div&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;People&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Office&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Pictures&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Music + Video&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Marketplace&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Games&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Framework Components&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;At the core of the framework is common base class library. On top of it is Application Object which provides common application features such as application management, session etc. On top of this are 2 components - Silverlight and XNA. As you know, XNA is mostly for games development and Silverlight for mostly apps development. On top of this resides a set of windows phone frameworks -&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;PhoneApplicationFrame - core of UI&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;PhoneApplicationPage - core of UI&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;PushNotification - enables your app to receive notification even if they are not running. Currently, windows phone doesn't allow multitasking but PushNotification can still work because it doesn't require the app to be running.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;WebBrowserControl - to access web, so that users don't have to close the app to browse web&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Sensors&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Camera&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;DeviceIntegration&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Launchers and Choosers - enables you to interact with some of the phone's native applications like media player.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Windows Phone Controls - Textbox, images, media element etc&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;WMAppManifest.xml file&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;This manifest defines the capabilities for an application. By using the capability detection tool, this list can be condensed to just keep the capabilities required by the app.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In here we also specify the default page to load in the DefaultTask element.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;It also defines other properties such as background image and icon for the application.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;You can not define the splash screen image in the manifest as the splash screen image loads before the manifest.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Some other properties that will come up in the market place (author, description, title etc) can be defined in the App element.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;"&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Application Events&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;These are defined in the App.xaml&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Application_Launching/Closing - when the application is launched/closed.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Application_Deactivated - When app get tombstoned (user has moved away say by opening a browser or something else)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Application_Activated - When you return back to the app&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="line-height: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Activated, Deactivated don't get called while Launching, Closing respectively.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Panorama Application&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Idea behind panorama applications is that in this UI is a long horizontal space in which controls are placed. This space is bigger than the screen size and you can continously go farther and farther on the right side as you make choices.&lt;/span&gt;&lt;/li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;li&gt;The guidance suggests that you should have minimum of 3 items in the panorama control.&lt;/li&gt;&lt;li&gt;Also, the guidance suggests that you should not use an application bar with panorama control.&lt;/li&gt;&lt;li&gt;Panorama is a control which can then contain multiple PanoramaItem. Each PanoramaItem can then contain other controls that you want to show as part of the item.&lt;/li&gt;&lt;li&gt;By default, Panorama application projects implement Model-View-ViewModel pattern.&lt;/li&gt;&lt;/span&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Pivot Application&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Idea behind pivot applications is that in this UI the controls keep on rotating like in a circle.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;li&gt;Pivot is a control which can then contain multiple PivotItem. Each PivotItem can then contain other controls.&lt;/li&gt;&lt;li&gt;Pivots can have application bars.&lt;/li&gt;&lt;li&gt;By default, pivot application project implements Model-View-ViewModel pattern.&lt;/li&gt;&lt;/span&gt;&lt;/ul&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Application States&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Launching&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Running&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Closing&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Deactivated&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Activated&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Other Notes&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Tombstoning -&amp;nbsp;&lt;/b&gt;Tombstoning happens when the OS terminates the app process when the user navigates away from the app. The OS maintains the state of the app and if user navigates back to the app, the OS passes the state data to the app process&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Persistent Data - &lt;/b&gt;is data that is shared by all instances of an app. For ex. app settings.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Transient Data - &lt;/b&gt;is data that describes the state of a single instance of the application. Its stored in the state dictionary. State dictionary is provided by Phone Application Service.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Application bar is a place where we can keep common buttons for our application&lt;/span&gt;&lt;/li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;li&gt;If you change the SupportedOrientations property in MainPage.xaml or any other page to “PortraitOrLandscape”, we can tilt the phone and the page will change automatically to landscape or portrait.&lt;/li&gt;&lt;/span&gt;&lt;/ul&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;div style="font-family: 'Times New Roman'; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Overview&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;ul style="font-family: 'Times New Roman';"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Frame:&amp;nbsp;&lt;/b&gt;Only a single frame is available to the application. No exceptions.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Besides other things Frames host pages.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If you want an application bar for the entire application, you should put it in the frame.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If you want an application bar specific to the page, you should put it in the page.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;System Tray:&amp;nbsp;&lt;/b&gt;is a reserved area by the system to display system level details. It can be made visible/invisible by changing the SystemTray.IsVisible property in the page xaml file.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Orientation:&amp;nbsp;&lt;/b&gt;Windows phone supports both portrait and landscape orientation. You can change the orientation by changing the SupportedOrientations and Orientation property in the page xaml file. If you change the SupportedOrientations property to "PortraitOrLandscape", when you tilt the phone, the page will change accordingly.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/span&gt;&lt;ul&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt; &lt;/span&gt;&lt;/ul&gt;&lt;div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If you download the windows phone 7 developer tools, you can start creating windows phone 7 projects. It also comes with an emulator which gives you a visual feel for your application.&amp;nbsp;In the next blog post, I will create a sample windows phone 7 application.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-178647574202627738?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/178647574202627738'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/178647574202627738'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/01/windows-phone-7-overview.html' title='Windows Phone 7 - Overview'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-8123774117726788912</id><published>2011-01-02T02:16:00.001-05:00</published><updated>2011-02-28T11:09:34.976-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Extension Methods'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='LINQ'/><title type='text'>Extension Methods in LINQ</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog, I will create a sample of a very useful feature provided by LINQ. These are called extensions and can be very handy to write helpful methods.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;As the name suggests, extension methods allows us to extend the functionality of classes. This is very useful when we don't have the code for such classes.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Let's see an example. It's a very common scenario where we need to convert a string value to an integer. We can simply use the int.TryParse() to achieve this result but sometimes, the string value returned might be "12.0" and in such cases int.TryParse() will not be able to convert the string to integer and will throw an error. Let's say for the sake of this example, even if the string returns "12.4" we don't care about the decimal part and we want the result to be returned as integer. So in case of "12.4" the result should be 12 and in case of "12.6" the result should be 13 and in case the string is invalid as in "abc" the result should be 0. Out of the box this functionality is not provided. So we can write an extension method and use it in all other modules wherever this might be needed.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Let's take a look at the code:&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; HelperExtensions&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; ToIntWithRounding(&lt;span class="kwrd"&gt;this&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; input)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;string&lt;/span&gt;.IsNullOrEmpty(input))&lt;br /&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; 0;&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;string&lt;/span&gt;.IsNullOrWhiteSpace(input))&lt;br /&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; 0;&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;double&lt;/span&gt; d = 0;&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;double&lt;/span&gt;.TryParse(input, &lt;span class="kwrd"&gt;out&lt;/span&gt; d))&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt;)System.Math.Round(d, MidpointRounding.AwayFromZero);&lt;br /&gt;            }&lt;br /&gt;            &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; 0;&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Then somewhere I call this method as shown below:&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;var strings = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;() { &lt;span class="kwrd"&gt;null&lt;/span&gt;, &lt;span class="str"&gt;"     "&lt;/span&gt;, &lt;span class="str"&gt;"12"&lt;/span&gt;, &lt;span class="str"&gt;"12.4"&lt;/span&gt;, &lt;span class="str"&gt;"12.6"&lt;/span&gt;, &lt;span class="str"&gt;"abc"&lt;/span&gt; };&lt;br /&gt;            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (&lt;span class="kwrd"&gt;string&lt;/span&gt; s &lt;span class="kwrd"&gt;in&lt;/span&gt; strings)&lt;br /&gt;                Console.WriteLine(&lt;span class="str"&gt;"\""&lt;/span&gt; + s + &lt;span class="str"&gt;"\""&lt;/span&gt; + &lt;span class="str"&gt;"\t\t"&lt;/span&gt; + s.ToIntWithRounding().ToString());&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;And here is the output:&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://lh4.googleusercontent.com/-psgLyNth6DQ/TWvGK52Bb-I/AAAAAAAAB1M/EeE5Ln4xGKQ/s1600/1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="https://lh4.googleusercontent.com/-psgLyNth6DQ/TWvGK52Bb-I/AAAAAAAAB1M/EeE5Ln4xGKQ/s1600/1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Points to note:&lt;/span&gt;&lt;br /&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The class in which this static extension method is declared should be static as well.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The extension method itself has to be static&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The first argument of extension method should be a "this" object where "this" is the object on which you want the extension method to be available on. In our example, we did it on the string object.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Intellisense works with extension methods as shown in the picture below:&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://lh5.googleusercontent.com/-WhoAeTILXYw/TWvHU-euQrI/AAAAAAAAB1Q/Do51OITmZO4/s1600/2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="https://lh5.googleusercontent.com/-WhoAeTILXYw/TWvHU-euQrI/AAAAAAAAB1Q/Do51OITmZO4/s1600/2.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-8123774117726788912?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/8123774117726788912'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/8123774117726788912'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/02/extension-methods-in-linq.html' title='Extension Methods in LINQ'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='https://lh4.googleusercontent.com/-psgLyNth6DQ/TWvGK52Bb-I/AAAAAAAAB1M/EeE5Ln4xGKQ/s72-c/1.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-1411472817817856665</id><published>2010-12-24T14:21:00.004-05:00</published><updated>2010-12-25T12:23:47.286-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Stopwatch</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Many a times I had to measure the time a method, a module or a functionality is taking. The only way I knew till now was to do it the DateTime way. Now, there is a new class to help us out with that: Stopwatch&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Earlier, I would have to get the current time before start, current time after finish, then use the TimeSpan class to subtract after and before times, to finally get the elapsed time. All this has been made a little easier by the introduction of Stopwatch class.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Stopwatch class is in System.Diagnostics namespace. Lets take a look at the code:&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; StopwatchExample()&lt;br /&gt;        {&lt;br /&gt;            Stopwatch sp = &lt;span class="kwrd"&gt;new&lt;/span&gt; Stopwatch();&lt;br /&gt;            sp.Start();&lt;br /&gt;            LongRunningMethod1();&lt;br /&gt;            sp.Stop();&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"Total time taken by LongRunningMethod1: "&lt;/span&gt; + sp.ElapsedMilliseconds);&lt;br /&gt;&lt;br /&gt;            sp.Start();&lt;br /&gt;            LongRunningMethod2();&lt;br /&gt;            sp.Stop();&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"Total time taken by LongRunningMethod1 AND 2: "&lt;/span&gt; + sp.ElapsedMilliseconds);&lt;br /&gt;&lt;br /&gt;            sp.Restart();&lt;br /&gt;            LongRunningMethod3();&lt;br /&gt;            sp.Stop();&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"Total time taken by ONLY LongRunningMethod3: "&lt;/span&gt; + sp.ElapsedMilliseconds);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LongRunningMethod3()&lt;br /&gt;        {&lt;br /&gt;            Thread.Sleep(3000);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LongRunningMethod2()&lt;br /&gt;        {&lt;br /&gt;            Thread.Sleep(2000);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LongRunningMethod1()&lt;br /&gt;        {&lt;br /&gt;            Thread.Sleep(1000);&lt;br /&gt;        }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;And here is the output:&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TRTxxlDJZkI/AAAAAAAABuU/oTinw0OA6aI/s1600/1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TRTxxlDJZkI/AAAAAAAABuU/oTinw0OA6aI/s1600/1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;So, first up we create an instance of Stopwatch class. Then we call simple Start and Stop methods to use the stopwatch. The object has ElapsedMilliseconds and Elapsed properties to measure the elapsed time. You can "Start" or "Restart" the stopwatch anytime. "Start" means stopwatch will start from the same time it was stopped last time. "Restart" will reset the stopwatch to 0, and then start counting the time.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-1411472817817856665?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/1411472817817856665'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/1411472817817856665'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2010/12/stopwatch.html' title='Stopwatch'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TRTxxlDJZkI/AAAAAAAABuU/oTinw0OA6aI/s72-c/1.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-4355857563153137737</id><published>2010-12-24T01:42:00.001-05:00</published><updated>2011-08-15T09:12:42.349-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Helpful SQL Commands</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;SQL server provides a rich set of commands to do operations other than the normal CRUD operations. In this blog, I will try to maintain a list of commands that are frequently used and required.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;sp_helpdb - &lt;/b&gt;returns a table with list of databases, their sizes, owners, created date, status and their compatibility level. Compability level is used to determine how certain features in this version will work. Usually, compatibility level 100 means SQL server 2008, 90 means SQL server 2005 etc.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;sp_helpdb &amp;lt;dbname&amp;gt; - &lt;/b&gt;If a database name is provided then besides the information given by sp_helpdb, this command also provides information about file allocations for the specified dbname. This table shows the name, filename, filegroup, its size, max size, growth and usage.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;select * from sys.&amp;lt;items&amp;gt; - &lt;/b&gt;This is a very helpful command to get a lot of information about a lot of items. By running this command, you can get information about databases, tables, columns, views, filegroups, logins, database files etc.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = &amp;lt;tablename&amp;gt; - &lt;/b&gt;Information_schema provides a great way to get information about schemas. With this command you can find out all the information about all the columns in table tablename.&lt;b&gt;&amp;nbsp;&lt;/b&gt;Using Information_schema you can also find information about Tables, Columns, Views, Constraints etc.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Version information - &lt;/b&gt;&lt;i&gt;select @@version &lt;/i&gt;will give you all the information about the version of the SQL Server where you ran this query. It will also tell you about the edition, bit (32/64) etc.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-4355857563153137737?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/4355857563153137737'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/4355857563153137737'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2011/02/helpful-sql-commands.html' title='Helpful SQL Commands'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-2579506429369282986</id><published>2010-12-23T05:00:00.004-05:00</published><updated>2010-12-24T14:25:27.984-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='PLINQ'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Simple PLINQ example</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;I love LINQ. From the onset, I liked the idea of having a simple and similar way of accessing any kind of data, be it objects, be it SQL data or xml or any other structured form. MSDN says that PLINQ is a parallel implementation of LINQ to objects. In this blog, I am providing a simple sample about one of the operators for PLINQ.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Advantage of using PLINQ, as with any other multi-threading scenario, is performance. First up, lets see the code to achieve that performance gain -&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SimpleExample()&lt;br /&gt;        {&lt;br /&gt;            var people = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;();&lt;br /&gt;            Load(people);&lt;br /&gt;            var namesStartingWithN = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;();&lt;br /&gt;&lt;br /&gt;            List&amp;lt;&lt;span class="kwrd"&gt;long&lt;/span&gt;&amp;gt; times = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;long&lt;/span&gt;&amp;gt;();&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 10; i++)&lt;br /&gt;            {&lt;br /&gt;                System.Diagnostics.Stopwatch sp = &lt;span class="kwrd"&gt;new&lt;/span&gt; Stopwatch();&lt;br /&gt;                sp.Start();&lt;br /&gt;                namesStartingWithN = people&lt;br /&gt;                    &lt;span class="rem"&gt;// run in parallel&lt;/span&gt;&lt;br /&gt;                    .AsParallel()&lt;br /&gt;                    .Where(p =&amp;gt; p.StartsWith(&lt;span class="str"&gt;"N"&lt;/span&gt;))&lt;br /&gt;                    .ToList();&lt;br /&gt;&lt;br /&gt;                sp.Stop();&lt;br /&gt;                &lt;span class="kwrd"&gt;long&lt;/span&gt; et = sp.ElapsedMilliseconds;&lt;br /&gt;                times.Add(et);&lt;br /&gt;                Console.WriteLine(&lt;span class="str"&gt;"Total Time: "&lt;/span&gt; + et);&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            Console.WriteLine();&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"Average: "&lt;/span&gt; + times.Average());&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Load(List&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; people)&lt;br /&gt;        {&lt;br /&gt;            Random rand = &lt;span class="kwrd"&gt;new&lt;/span&gt; Random();&lt;br /&gt;            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 1000000; i++)&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;int&lt;/span&gt; r = rand.Next(1, 5);&lt;br /&gt;                &lt;span class="kwrd"&gt;string&lt;/span&gt; name = &lt;span class="kwrd"&gt;string&lt;/span&gt;.Empty;&lt;br /&gt;                &lt;span class="kwrd"&gt;switch&lt;/span&gt; (r)&lt;br /&gt;                {&lt;br /&gt;                    &lt;span class="kwrd"&gt;case&lt;/span&gt; 1:&lt;br /&gt;                        name = &lt;span class="str"&gt;"Michael"&lt;/span&gt; + i;&lt;br /&gt;                        &lt;span class="kwrd"&gt;break&lt;/span&gt;;&lt;br /&gt;                    &lt;span class="kwrd"&gt;case&lt;/span&gt; 2:&lt;br /&gt;                        name = &lt;span class="str"&gt;"Sachin"&lt;/span&gt; + i;&lt;br /&gt;                        &lt;span class="kwrd"&gt;break&lt;/span&gt;;&lt;br /&gt;                    &lt;span class="kwrd"&gt;case&lt;/span&gt; 3:&lt;br /&gt;                        name = &lt;span class="str"&gt;"Roger"&lt;/span&gt; + i;&lt;br /&gt;                        &lt;span class="kwrd"&gt;break&lt;/span&gt;;&lt;br /&gt;                    &lt;span class="kwrd"&gt;case&lt;/span&gt; 4:&lt;br /&gt;                        name = &lt;span class="str"&gt;"Nadal"&lt;/span&gt; + i;&lt;br /&gt;                        &lt;span class="kwrd"&gt;break&lt;/span&gt;;&lt;br /&gt;                    &lt;span class="kwrd"&gt;case&lt;/span&gt; 5:&lt;br /&gt;                    &lt;span class="kwrd"&gt;default&lt;/span&gt;:&lt;br /&gt;                        name = &lt;span class="str"&gt;"Pete"&lt;/span&gt; + i;&lt;br /&gt;                        &lt;span class="kwrd"&gt;break&lt;/span&gt;;&lt;br /&gt;                }&lt;br /&gt;&lt;br /&gt;                people.Add(name);&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In the Load method, I just create a large number of string objects which represent different names. Then, in the SimpleExample method, my goal is to find all the names that start with "N". This can be done by a simple LINQ query. But if you observe the LINQ query, I have also added "AsParallel()" operator. This instructs the compiler to run it in parallel. The outer for loop (which runs 10 times) is just for calculating the average time it takes with and without "AsParallel()".&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Without "AsParallel()"&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TROgYA7FfgI/AAAAAAAABuE/MYWletzl_uo/s1600/1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TROgYA7FfgI/AAAAAAAABuE/MYWletzl_uo/s1600/1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;With "AsParallel()"&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TROgeznKojI/AAAAAAAABuI/mX8J9sEEDEw/s1600/2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TROgeznKojI/AAAAAAAABuI/mX8J9sEEDEw/s1600/2.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The results might differ from one machine configuration to other, but, its pretty evident that, there is a good performance gain.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Sometimes, we need to get results in a particular order. In this example, it was not necessary however if you need to return results in order, you can call the "AsOrdered()" operator after the "AsParallel()" operator.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The other important aspect about "AsParallel()" is that we can not change all our existing/new queries to have "AsParallel()" to gain performance. The reason for this is, if used wrongly, there is a good chance of race condition. We should analyze the usage of "AsParallel()" accordingly. Code below is a good example of race condition -&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; RaceCondition()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 10; i++)&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;int&lt;/span&gt; sum = 0;&lt;br /&gt;                &lt;span class="kwrd"&gt;try&lt;/span&gt;&lt;br /&gt;                {&lt;br /&gt;                    Enumerable.Range(1, 100000)&lt;br /&gt;                        &lt;span class="rem"&gt;// This statement causes race condition&lt;/span&gt;&lt;br /&gt;                        .AsParallel()               &lt;br /&gt;                        .Select(n =&amp;gt;&lt;br /&gt;                        {&lt;br /&gt;                            sum += n;&lt;br /&gt;                            &lt;span class="kwrd"&gt;return&lt;/span&gt; sum;&lt;br /&gt;                        })&lt;br /&gt;                        .ToList();&lt;br /&gt;                }&lt;br /&gt;                &lt;span class="kwrd"&gt;catch&lt;/span&gt; (AggregateException ex)&lt;br /&gt;                {&lt;br /&gt;                    &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Exception e &lt;span class="kwrd"&gt;in&lt;/span&gt; ex.InnerExceptions)&lt;br /&gt;                    {&lt;br /&gt;                        Console.WriteLine(e.Message);&lt;br /&gt;                    }&lt;br /&gt;                }&lt;br /&gt;                Console.WriteLine(sum);&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Without "AsParallel()"&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_a_Uk_rwmZ7g/TROmNSeA8JI/AAAAAAAABuM/zruLe3VdLoA/s1600/3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/_a_Uk_rwmZ7g/TROmNSeA8JI/AAAAAAAABuM/zruLe3VdLoA/s1600/3.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;With "AsParallel()"&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_a_Uk_rwmZ7g/TROmYXKdLCI/AAAAAAAABuQ/BgiFStiWZsw/s1600/4.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/_a_Uk_rwmZ7g/TROmYXKdLCI/AAAAAAAABuQ/BgiFStiWZsw/s1600/4.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;As you can see the sum which was correct when we didn't use "AsParallel()" is now incorrect and inconsistent.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-2579506429369282986?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/2579506429369282986'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/2579506429369282986'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2010/12/simple-plinq-example.html' title='Simple PLINQ example'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TROgYA7FfgI/AAAAAAAABuE/MYWletzl_uo/s72-c/1.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-1847706674625721063</id><published>2010-12-21T21:07:00.004-05:00</published><updated>2010-12-21T21:10:18.574-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Data Warehouse'/><category scheme='http://www.blogger.com/atom/ns#' term='Business Intelligence'/><title type='text'>Data WareHousing and Business Intelligence</title><content type='html'>&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog, I look at some of the theoretical concepts behind Data Warehousing and how Data Warehousing helps in achieving Business Intelligence.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Data Warehouse (DW)&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Times New Roman'; font-weight: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;/div&gt;&lt;ul&gt;&lt;li style="font-family: 'Times New Roman'; font-weight: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Is a big store which stores ALL of your data.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="font-family: 'Times New Roman'; font-weight: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;By All data it means that it can have anything ranging from DBs to Excel sheets to Outlook servers storing emails.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="font-family: 'Times New Roman'; font-weight: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;DW and production DB are two different things. While production DB is ideally suited to support production apps, DW is suited for -&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;ul style="font-family: 'Times New Roman'; font-weight: normal;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;making reporting easier&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;combining that data across multiple systems&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;reducing production load as production environments are not responsible to produce outputs such as reports and BI&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;providing long term storage of data.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li style="font-family: 'Times New Roman'; font-weight: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Production DBs are not created for this purpose. Usually they follow the model of on-line transaction processing (OLTP). They are designed to be worked on single records with optimized and normalized data. This means, that duplications have been removed. So getting all data for a record might involve lots of joins. Hence they are not suitable for reporting. The other problem is doing such operations on production DBs can slow the production system down.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="font-family: 'Times New Roman'; font-weight: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;How DW solves this problem?&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;ul style="font-family: 'Times New Roman'; font-weight: normal;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;DW uses a different mechanism than OLTP. It uses OLAP - online analytical processing. In OLAP, number of tables are reduced thus reducing the number of joins and increasing the simplicity to retrieve that data.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In DW we actually denormalize the data so that the structure is simple to work with. This results in duplication of data, however, in recent times the cost of data storage has gone down and that additional cost is worth more when it comes to the gains in reporting.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;When we denormalize the tables, we use a concept of surrogate key. This is a primary key to this new table which doesn't really have a meaning.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Fact: &lt;/b&gt;Fact tables are used to mark some events with respect to time. Facts joins dimensions. Facts also answer the question of "how much". For example, Fact table will contain the answer to who did the transaction, what kind of transaction was done, where it was done and when it was done (Just the ids for this "w" questions). Fact table might also contain the amount ("how much") of that transaction.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Dimension:&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Dimension contains the actual values of who, what, where and when of the event. They hold the values that describe the facts. For example, who might point to Customer table, where might point to Country table, what might point to some lookup table and when might point to some history table. There are various types of dimensions -&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Static Data: This is the data that never changes. For example WeekDays. A Sunday will always be Sunday. Such type of data is usually called a type 0 dimension.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;A type 1 dimension is that kind of data in which case history is not required to be saved. Its simply overwritten.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;A type 2 dimension is that kind of data in which case history is required to be saved. In this case we usually add 2 columns to the dimension table which contain the information of "from date" and "thru date". Initially, thru date is null, indicating that this record is current. When a new update record is inserted, we change the thru date to some date for the old record. Now the thru date of the new record becomes null, indicating that this is the current record.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;A type 4 dimension is that kind of data in which you want to keep the history and values separate.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;There might be cases where within 1 table you might have few different columns that have different type dimensions.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Usually, developers should never decide the type dimension of a column. It should always be decided by business as they know the data better.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;A conformed dimension is a table which consolidates primary keys. For example, if we have 3 different systems each assigning different key to the same item, we first consolidate these 3 keys in a conformed dimension table. This is also a case where surrogate keys become helpful.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;div style="font-family: 'Times New Roman'; font-weight: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Business Intelligence (BI)&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style="font-family: 'Times New Roman'; font-weight: normal;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Transforming all the data that is there in the data warehouse into something meaningful is business intelligence.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;One main example is to aggregate the data to produce reports which requires writing tools which can sift through large amounts of data.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Another example would be to analyze the data by looking for patterns and trends.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;We might also want to do data mining on our data to come up with analysis that might not be that obvious from the data itself. For example, a customer buying a tennis racket is 95% likely to buy tennis balls, however, he is only 2% likely to buy a tennis net.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Constructing a DW/BI solution&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Constructing a DW/BI solution involves 4 steps. Also, in the braces you see some of the MS tools that help us in doing that-&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Creating a DW&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;ETL - Extract - Transform - Load (SQL Service Integration Services SSIS)&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In ETL, Extraction means extracting data from the data stores&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Transformation means transforming the data (denormalization etc as mentioned above) to match the table design in DW&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Load means loading the data in DW&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Analytics - Aggregation - Trending - Correlations (SQL Server Analysis Services)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Reporting (SQL Server Reporting Services)&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-1847706674625721063?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/1847706674625721063'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/1847706674625721063'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2010/12/data-warehousing-and-business.html' title='Data WareHousing and Business Intelligence'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-6729815546873402945</id><published>2010-12-20T20:09:00.001-05:00</published><updated>2010-12-20T20:11:56.415-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SSL'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='IIS'/><category scheme='http://www.blogger.com/atom/ns#' term='HTTPS'/><category scheme='http://www.blogger.com/atom/ns#' term='Certificates'/><title type='text'>Certificates</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog, I will examine the basics of cryptography and usability of IIS Certificates. I will also describe symmetric key cryptography, public-private key cryptography, digital signing and SSL.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Symmetric Key Cryptography&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In symmetric key cryptography, PartyA generates a key and somehow shares the key with PartyB.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Now, all the communication between the two parties is encrypted using this key.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Since no one else knows the key, they can not decrypt the data.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Public-Private Key Pair Cryptography&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;PartyA generates a public-private key pair. PartyA never shares the private key with anyone. The public key is available to all.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Two kinds of communications can be done through this key pair-&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;PartyA sends a message to PartyB:&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;PartyA puts a signature on the message using the private key. Its almost impossible to create the same signature on the same message since no one else knows the private key.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;PartyB has the knowledge of Public key. So, it does a computation of checksum on the incoming message and verifies the signature. This ensures that the message actually came from PartyA and it has not been tempered. This is explained below in "Digital Signing and Verification" below.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;PartyB sends a message to PartyA:&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;PartyB encrypts the message using the public key. Nobody can decrypt it because no one knows the private key.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;PartyA is able to decrypt it using its private key.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Self signed certificate = Name of the entity (PartyA) + public key + signed using private key. If it is signed by some Certificate Authority (CA) then that means that it is signed using the private key of CA.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In short certificate is a name bound to a key.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Digital Signing and Verification&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Digital Signing&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Take Data(D) and run a hash on it giving a hash result(HR).&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Encrypt this HR using private key of the signer, resulting in HRe.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;This HRe is called signature.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Signer also has a certificate(C) which contains the public key of the signer.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;D + HRe + C = digitally signed data&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Verification&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Run Hash on Data(D) to get hash result HR.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Take the signature (HRe) and decrypt using public key of the signer, resulting in HR.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If the HR in above 2 steps is equal that proves that the message came from the signer and the data hasn't been tempered with.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Usually Data(D) is the certificate itself. This process guarantees that the certificate came from the right source.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Certificates&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If you look at a website's certificates you can find out a lot of information. Such as -&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Issued By (Certificate Authority)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Issued To&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Valid From etc&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In the details tab, you see a lot of fields and their values.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The orange fields, such as, version, issuer, public key etc are header fields and they are always there.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The green fields may or may not be there. These fields are called extensions.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Some of the extension fields are marked with exclamation mark - they mean that if you don't know how to process them - you should not use the certificate.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;You can use certutil.exe - a command line utility to do some operations on the certificates. For example, "certutil -dump abc.cer" will display all the contents of the certificate on the console. You can download a website's certificate by using "copy to file" feature for the certificate.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Private keys are located usually inside ProgramData\Microsoft\Crypto\RSA. But this depends on OS, configuration and other details.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;IE uses the MS Crypto API to store the certificates but FireFox has its own store. You can access that by going to Tools-&amp;gt;Options-&amp;gt;Advanced-&amp;gt;Encryption tab and then View Certificates button.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Usually certificates are chained. You can identify the parent using "Issuer" and "Subject" fields. If the "Issuer" and "Subject" are one and the same, that means its a root level certificate. Usually a CA first issues a root certificate to itself. Then it uses this certificate to issue other certificates for other CAs.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Certificate Revocation List&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;When you try to access a website which uses a certificate, the browser gets the certificate from that website and sees the Serial Number in the certificate.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;This serial number is then sent to a URL, which is usually the URL of CA. This URL is located in the "CRL Distribution Points" header value.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If you go to this URL, you can download the *.crl file which contains a list of all certificates that have been revoked.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If the serial number you received is in this list, then usually the browser lets you know that this certificate is not valid and what action you would like to take.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Usually, it is a good practice to not take any further action as the certificate can not be trusted anymore.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Since the revocation list can become really large, another method is sometimes used. Its a separate service (called OCSP) where you call the service and it only replies with Yes or No stating whether the certificate is good or not.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;SSL Handshake&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Browser issues secure session request (usually, https://something)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Server sends X.509 certificate containing server's public key.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Browser authenticates certificate against list of known CAs. If CA is unknown, browser can ask for user action to proceed. &lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Browser generates a random symmetric key and encrypts it using server's public key and sends to server.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Browser and server now both know the symmetric key and rest of session proceeds using this symmetric key.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;So, How does it all add up?&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;CA signs a certificate with its private key and gives it to a website. The certificate now contains the public key amongst other information.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;When a browser requests some transaction on the website, the browser inspects the certificate and using the logic mentioned above (public key and checksum) it finds out whether the certificate was issued by the trusted CA. So just for this purpose, the browser doesn't need to go to CA all the time.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The only thing that requires CA access by the browser is when it wants to validate whether the certificate has been revoked or not as mentioned above.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If all is good, it shows a green signal suggesting the validity of certificate. Otherwise, it displays a warning or a red signal suggesting that the certificate couldn't be validated and warns user for further action.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;For future data transfers, SSL Handshake establishes a symmetric key known only to browser and server.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-6729815546873402945?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/6729815546873402945'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/6729815546873402945'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2010/12/certificates.html' title='Certificates'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-4380660959417729732</id><published>2010-12-05T02:56:00.001-05:00</published><updated>2010-12-13T22:03:52.357-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Authentication'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='WCF RIA Services'/><title type='text'>WCF RIA Services - Authentication/Authorization</title><content type='html'>&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog, I continue to research on the Authentication/Authorization piece of WCF RIA Services. If you haven't read the overview, I encourage you to do so in my previous blog post titled "WCF RIA Services Overview". For Authentication/Authorization read on...&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;AuthenticationDomainService&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The framework provides an AuthenticationDomainService on the server side. AuthenticationDomainService derives from AuthenticationBase which derives from DomainService. It provides mechanisms for authentication, authorization and user profiles.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;AuthenticationBase provides default implementations for Login, Logout, GetUser, UpdateUser functions.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;User&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;A User class is also generated on the server side.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;It derives from UserBase which has Name and collection of Roles.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;UserBase also implements IPrincipal and IIdentity.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The architecture is designed in this way so that we can easily add our custom properties to this user class. This way both the authentication needs and our biz needs are met. Once built, these properties also propagate to the client side.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;A corresponding User proxy is also created on the client side which also implements the IPrincipal and IIdentity.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;On server side you can access the user by accessing ServiceContext.User property.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;On client side you can do so by accessing WebContext.User property.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;AuthenticationDomainContext&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;After building the solution, a corresponding AuthenticationDomainContext gets created.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;It derives from AuthenticationDomainContextBase which derives from DomainContext.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;AuthenticationService&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;We can use the functions provided by AuthenticationDomainContext but RIA provides one more level of abstraction in the form of AuthenticationService on the client side.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;This class provides methods such as Login, Logout, LoadUser, SaveUser etc. It also provides properties to access the User and its properties such as IsBusy, IsLoggingIn etc.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;WebAuthenticationService inherits from AuthenticationService.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Two more classes, FormsAuthentication and WindowsAuthentication inherit from WebAuthenticationService. These classes are concrete.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The WindowsAuthentication class does nothing more but to prohibit the Login and Logout methods due to the nature of authentication.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;WebContext&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The client also generates a WebContext class when built. This class can also pretty much do everything that the FormsAuthentication and WindowsAuthentication can do but it provides a better abstraction level. What this means is that it is a more generic way to do authentication.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The WebContext class can also be added to the application level storage namely ApplicationLifetimeObjects (in app.xaml), making it easy to be accessible from anywhere in the client app.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;It also provides the access to the current user from anywhere in the client app.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Once the WebContext is setup, we can use its properties to bind in our XAMLs to effectively enable/disable functionalities based on authentication.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Transport Security&lt;/b&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;To expose a service over HTTPS, you just need to apply the RequiresSecureEndpoint=true inside the EnableClientAccess attribute on top of the domain service.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The generated domain context will now call a different constructor to use HTTPS.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If your website is HTTP and you are making service calls through HTTPS, you will need to update the clientaccesspolicy to allow for cross domain/scheme calls.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Authorization&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;You can use RequiresAuthentication and RequiresRole attributes on top of services or operation scopes to make them secure.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Once the solution is built, these attributes propagate to the client side as well.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;We can also do custom authorization by creating custom authorization classes.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Custom Authentication&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Most of the times in business application, the membership framework provided by ASP.NET would not be enough.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Moreover, the framework provided user, roles entities might not tie up with our business requirements. In that case, we need to use custom authentication.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;To implement custom authentication, we need to override a few methods in the AuthenticationDomainService.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The methods to override are:&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;ValidateUser&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;GetAuthenticatedUser&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;UpdateUserCore&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Once these methods are implemented, we are actually bypassing the system provided by ASP.NET membership and are using our own system.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog, we examined the authentication/authorization framework provided by WCF RIA services. It can be used out-of-box and it can be customized as well. One of the great advantages of WCF RIA services is that client generation is taken care of by the RIA architecture. This means that validation, attributes, authentication, authorization and other things are all in sync with each other.&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-4380660959417729732?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/4380660959417729732'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/4380660959417729732'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2010/12/wcf-ria-services-authenticationauthoriz.html' title='WCF RIA Services - Authentication/Authorization'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-8836625321158099635</id><published>2010-12-05T01:41:00.000-05:00</published><updated>2010-12-06T17:16:25.777-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='WCF RIA Services'/><category scheme='http://www.blogger.com/atom/ns#' term='Silverlight'/><title type='text'>WCF RIA Services Overview</title><content type='html'>&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;WCF RIA Services makes it easy to concentrate on the data and business logic rather than worrying about creating multiple layers. Besides that it also simplifies querying, updating, validation and authentication among other features.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Design Pattern&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;div style="font-family: 'Times New Roman';"&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;WCF RIA service design pattern has three main aspects. These reside between the DAL and UI Views.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Data Model&lt;/b&gt; - After the DAL is set up, RIA creates entity and metadata on the server side and entity proxies and metadata on the client side. A metadata file is added on the server side allows us to add, besides other things, validation rules to the entities.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;DomainService&lt;/b&gt; - on the server side. Provides a way to add CRUD and custom logic. RIA architecture defines a stateless server.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;DomainContext&lt;/b&gt; - on the client side. Provides a way to add methods to load and invoke server side functionalities. RIA architecture defines a stateful structure. This is to enable data caching on the client side.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;EntityContainer - &lt;/b&gt;on the client side. To hold the entities since client is stateful. EntityContainer enables the client side data caching.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Most of the client side stuff is added automatically when we add server side items and build the solution.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Steps to get up and running&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Create simple Silverlight application and enable WCF RIA services&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Add your DAL (through Entity Framework or LINQ to SQL etc)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Add DomainService on the server side.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Build the solution&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Build adds a DomainContext and entity structure with metadata on the client side.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Use this DomainContext to create queries. Queries are of type EntityQuery.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The service is located at http://localhost:xxxx/services/&amp;lt;appname&amp;gt;-web-&amp;lt;domainservicename&amp;gt;.svc&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Updating/Retrieving Data&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;To update data, use the DomainContext's SubmitChanges(), RejectChanges() methods.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;To retrieve data, use the DomainContext's Load method to load queries.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Load method provides an important option called LoadBehavior which is explained further below.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Validation&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;For validation, it provides attributes such as Required, Range etc which can be put on data entities on the server side and they will propagate to the client side.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Query operations on the domain service&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Queries in the Domain service follow some simple rules based on WCF, such as, they can return/take only primitive types. There can not be any overloads. These functions need to be public etc.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;If needed, we can decorate these functions in domain service by Query attribute. It provides us options to further constrain the results. For ex.&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;IsComposable - if set to false, doesn't allow the client to put any more filtering on the client side. The query has to be used as it is and no further composition is possible.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;ResultLimit - to limit the no. of instances that can be retrieved from the server.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;HasSideEffects - to indicate that the query should be invoked via HTTP Post. HTTP Get is the default.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Asynchronous Loading&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Calls from client are asynchronous so that the UI is not stuck. The callbacks can be implemented using various ways.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;As soon as the client call is made, the function returns a LoadOperation object.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;We can handle the callback by providing a LoadOperation.Completed event or providing a lambda expression/delegate inline.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Client Side Data Caching&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Once some data is loaded on client side, its cached by the Domain Context to improve performance.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Since data is cached, RIA provides a way to handle scenarios where the data on server side has changed. This is done by the LoadBehavior enum of the Load method. It has 3 options -&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;KeepCurrent - objects in the client cache are kept as it is. This is default.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;RefreshCurrent - objects in the client cache are replaced by the objects on the server side when load is called.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;MergeIntoCurrent - only updates the objects that have not been modified since the last time the cache was loaded. So all the client side objects which were modified remain the same. All client side unmodified objects are refreshed from the server.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-8836625321158099635?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/8836625321158099635'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/8836625321158099635'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2010/12/wcf-ria-services.html' title='WCF RIA Services Overview'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-959951398157549789</id><published>2010-12-05T01:28:00.006-05:00</published><updated>2010-12-06T11:47:49.097-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='IronRuby'/><category scheme='http://www.blogger.com/atom/ns#' term='DLR'/><title type='text'>Iron Ruby DLR Sample</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Recently, I created a very simple Iron Ruby sample for understanding how it works and what is the concept of DLR (Dynamic Language Runtime). DLR is built on top of CLR (Common Language Runtime). In .NET you can create apps which use IronRuby dynamic code and run them with the help of DLR.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;You will need to download IronRuby from&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;a href="http://ironruby.net/Download"&gt;http://ironruby.net/Download&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this sample, I just created a script.rb file which has some simple calls to one of the functions of my class. Then I created a console app. The code in my console app looks like this:&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Program&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;        {&lt;br /&gt;            var worker = &lt;span class="kwrd"&gt;new&lt;/span&gt; CSharpWorker();&lt;br /&gt;&lt;br /&gt;            var runtime = IronRuby.Ruby.CreateRuntime();&lt;br /&gt;            var engine = runtime.GetEngine(&lt;span class="str"&gt;"Ruby"&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;            var scope = engine.CreateScope();&lt;br /&gt;            scope.SetVariable(&lt;span class="str"&gt;"worker"&lt;/span&gt;, worker);&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; fileName = &lt;span class="str"&gt;@"C:\Demo\script.rb"&lt;/span&gt;;&lt;br /&gt;            var script = engine.CreateScriptSourceFromFile(fileName);&lt;br /&gt;            script.Execute(scope);&lt;br /&gt;&lt;br /&gt;            Console.ReadLine();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CSharpWorker&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SaySomething(&lt;span class="kwrd"&gt;string&lt;/span&gt; str)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(str);&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;This code creates a runtime with Ruby engine and sets the scope of the script file to worker object. Once the execute is called, the code in script.rb file gets called. Code in script.rb file is a simple line: worker.SaySomething("Hi There")&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;When the script file runs, it outputs "Hi There". We can now change the script.rb file without the need of building the console exe. &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;We can create complex classes in the script.rb file and execute the same using this simple technique.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-959951398157549789?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/959951398157549789'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/959951398157549789'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2010/12/iron-ruby-dlr-sample.html' title='Iron Ruby DLR Sample'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-1020567417212411831</id><published>2010-09-15T12:18:00.000-04:00</published><updated>2011-12-18T12:28:30.948-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='HTTP'/><title type='text'>HTTP</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;In this blog, I will document some basic concepts about HTTP. HTTP stands for Hyper Text Transfer Protocol and is a way to communicate between a server and a client. I will also look into some of the Headers associated with HTTP.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;HTTP belongs to the Application layer of internet protocol suite along with SMTP, DNS, Telnet etc. Each communication contains a request from the browser and a response from the server.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Request and Response usually have 3 parts. The first line is either a request line or a status line (in case of response). Second line contains the headers and the third line contains the body. Let's examine the first line.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;b style="font-family: Verdana, sans-serif;"&gt;Request Methods&lt;/b&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;HEAD&lt;/b&gt; - similar to GET but it only sends the headers without the response body.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;GET&lt;/b&gt; - requests a static or a dynamic resource.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;POST&lt;/b&gt; - to submit data to the server. The data to be submitted is put in the body of the request.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;PUT and DELETE&lt;/b&gt; - to upload and delete specified resource&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;TRACE&lt;/b&gt; - server sends the received request so that client can see if intermediate servers made any modifications to the request.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;OPTIONS&lt;/b&gt; - returns HTTP methods that are supported on the requested URL.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;CONNECT&lt;/b&gt; - to facilitate HTTPS encrypted communication over an unencrypted HTTP proxy.&amp;nbsp;In this mechanism, the client asks an HTTP Proxy server to forward the TCP connection to the desired destination using the "CONNECT" HTTP method. The server then proceeds to make the connection on behalf of the client. Once the connection has been established by the server, the Proxy server continues to proxy the TCP stream to and from the client. Note that only the initial connection request is HTTP - after that, the server simply proxies the established TCP connection.&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;This mechanism is how a client behind an HTTP proxy can access websites using SSL (i.e. HTTPS).&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;PATCH&lt;/b&gt; - to facilitate partial modifications to a resource.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Let's now take a look at some of the more important request and response headers.&amp;nbsp;&lt;/span&gt;&lt;br/&gt;&lt;br /&gt;&lt;b style="font-family: Verdana, sans-serif;"&gt;Request Headers&lt;/b&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Accept - &lt;/b&gt;acceptable content types. Similarly Accept-charset, language, encoding means acceptable charsets, languages and encodings by the browser.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Content-type, Content-length and Content-MD5 - &lt;/b&gt;denotes type, length and base-64 encoded binary MD5 sum of the content of the request&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Authorization - &lt;/b&gt;authentication credentials for HTTP authentication&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Cache-control - &lt;/b&gt;to control caching.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Connection -&amp;nbsp;&lt;/b&gt;preferable&amp;nbsp;connection.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Cookie - &lt;/b&gt;an HTTP cookie sent previously by server with Set-Cookie header.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Max-forwards - &lt;/b&gt;max number of hops the message can be forwarded through proxies and gateways.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Host - &lt;/b&gt;domain name of the server.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Referer - &lt;/b&gt;address of the previous web page from which the currently requested page was followed.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;User-agent - &lt;/b&gt;tells the browser from which request is coming.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Via - &lt;/b&gt;informs the server of proxies through which the request was sent.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Date - &lt;/b&gt;date and time when the request was sent.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;b style="font-family: Verdana, sans-serif;"&gt;Response Headers&lt;/b&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Cache-control - &lt;/b&gt;to control caching.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Connection - &lt;/b&gt;options that are&amp;nbsp;desired for this connection&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Content-encoding, language, length, MD5, type - &lt;/b&gt;describes the metadata about the content.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Expires - &lt;/b&gt;date and time at which the response becomes stale.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Server - &lt;/b&gt;name of the server&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Set-Cookie - &lt;/b&gt;sets the cookie which will be stored by the browser and will be sent back. The cookie can be used for authentication, setting user preference, identifying user session, contents of shopping cart etc.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Via - &lt;/b&gt;informs the client about the proxies through which the request was sent.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;WWW-Authenticate - &lt;/b&gt;indicates the authentication scheme that should be used to access the requested entity. For ex, basic or digest.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;There are some non-standard headers as well. Usually they start with an x-.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;b style="font-family: Verdana, sans-serif;"&gt;Response Status Codes&lt;/b&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;1xx - &lt;/b&gt;Informational. For ex.&lt;/span&gt;&lt;br /&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;122 - requested URI is too long (&amp;gt; 2083 chars)&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;2xx - &lt;/b&gt;Success&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;200 - OK&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;204 - No Content&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;3xx - &lt;/b&gt;Redirection&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;4xx - &lt;/b&gt;Client Error&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;400 - Bad Request&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;401 - Unauthorized&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;403 - forbidden&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;404 - not found&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;5xx - &lt;/b&gt;Server Error&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;500 - internal server error&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;503 - service unavailable&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;504 - gateway timeout&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-1020567417212411831?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/1020567417212411831'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/1020567417212411831'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2010/09/http.html' title='HTTP'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-8260409831923758898</id><published>2010-08-13T20:07:00.016-04:00</published><updated>2010-12-13T20:10:12.429-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>INotifyPropertyChanged and ObservableCollection</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In case of binding scenarios, earlier, developers had to write code to make sure that UI and internal objects are all in sync with each other. With INotifyPropertyChanged interface and ObservableCollection&amp;lt;T&amp;gt;, it has become simple to keep those layers in sync.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;INotifyPropertyChanged&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;This interface can be implemented to notify to the client that some property has changed. Its mostly used in the case of databinding scenarios.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Its usage is demonstrated below:&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; A01NotifyPropertyChanged : Window&lt;br /&gt;    {&lt;br /&gt;        Person p;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; A01NotifyPropertyChanged()&lt;br /&gt;        {&lt;br /&gt;            InitializeComponent();&lt;br /&gt;            p = &lt;span class="kwrd"&gt;new&lt;/span&gt; Person { Age = 29 };&lt;br /&gt;            DataContext = p;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; changeItemBtn_Click(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            p.Age += 1;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Person : INotifyPropertyChanged&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; m_age;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Age &lt;br /&gt;        {&lt;br /&gt;            get&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; m_age;&lt;br /&gt;            }&lt;br /&gt;            set&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;value&lt;/span&gt; != m_age)&lt;br /&gt;                {&lt;br /&gt;                    m_age = &lt;span class="kwrd"&gt;value&lt;/span&gt;;&lt;br /&gt;                    &lt;span class="kwrd"&gt;if&lt;/span&gt; (PropertyChanged != &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;br /&gt;                        PropertyChanged(&lt;span class="kwrd"&gt;this&lt;/span&gt;, &lt;span class="kwrd"&gt;new&lt;/span&gt; PropertyChangedEventArgs(&lt;span class="str"&gt;"Age"&lt;/span&gt;));&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;event&lt;/span&gt; PropertyChangedEventHandler PropertyChanged;&lt;br /&gt;    }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The corresponding XAML file looks like this:&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;StackPanel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TextBox&lt;/span&gt; &lt;span class="attr"&gt;Text&lt;/span&gt;&lt;span class="kwrd"&gt;="{Binding Age}"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Button&lt;/span&gt; &lt;span class="attr"&gt;Content&lt;/span&gt;&lt;span class="kwrd"&gt;="Increase By 1"&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="changeItemBtn"&lt;/span&gt; &lt;span class="attr"&gt;Click&lt;/span&gt;&lt;span class="kwrd"&gt;="changeItemBtn_Click"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;StackPanel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this code, every time the button is clicked, the age is increased by 1. However notice that we have not written any code to update the UI. Since the Person class implements the INotifyPropertyChanged event, the UI, which in this case is the textbox, automatically gets updated.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;ObservableCollection&amp;lt;T&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Similar to INotifyPropertyChanged, there is another interface named INotifyCollectionChanged, which does the same thing but on collections.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;However, instead of implementing INotifyCollectionChanged, we are already provided with ObservableCollection&amp;lt;T&amp;gt; which implements both INotifyCollectionChanged and INotifyPropertyChanged.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The purpose of this collection is to notify the client about additions, updates and deletes to the collection. &lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Its usage is demonstrated below:&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre class="csharpcode"&gt;ObservableCollection&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; tasks = &lt;span class="kwrd"&gt;new&lt;/span&gt; ObservableCollection&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;();&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; A04ObservableCollection()&lt;br /&gt;        {&lt;br /&gt;            InitializeComponent();&lt;br /&gt;            lv.ItemsSource = tasks;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; btn_Click(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt;(!&lt;span class="kwrd"&gt;string&lt;/span&gt;.IsNullOrEmpty(tb.Text.Trim()))&lt;br /&gt;                tasks.Add(tb.Text);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; btnRemove_Click(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; selectedName = lv.SelectedItem &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;;&lt;br /&gt;            tasks.Remove(selectedName);&lt;br /&gt;        }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The corresponding XAML file looks like this-&lt;/span&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;StackPanel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Label&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="lbl"&lt;/span&gt; &lt;span class="attr"&gt;Content&lt;/span&gt;&lt;span class="kwrd"&gt;="To Do List:"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TextBox&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="tb"&lt;/span&gt; &lt;span class="attr"&gt;Margin&lt;/span&gt;&lt;span class="kwrd"&gt;="3"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ListView&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="lv"&lt;/span&gt; &lt;span class="attr"&gt;Margin&lt;/span&gt;&lt;span class="kwrd"&gt;="3"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Button&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="btn"&lt;/span&gt; &lt;span class="attr"&gt;Margin&lt;/span&gt;&lt;span class="kwrd"&gt;="3"&lt;/span&gt; &lt;span class="attr"&gt;Content&lt;/span&gt;&lt;span class="kwrd"&gt;="Add Task to List"&lt;/span&gt; &lt;span class="attr"&gt;Click&lt;/span&gt;&lt;span class="kwrd"&gt;="btn_Click"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Button&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="btnRemove"&lt;/span&gt; &lt;span class="attr"&gt;Margin&lt;/span&gt;&lt;span class="kwrd"&gt;="3"&lt;/span&gt; &lt;span class="attr"&gt;Content&lt;/span&gt;&lt;span class="kwrd"&gt;="Remove Selected Task"&lt;/span&gt; &lt;span class="attr"&gt;Click&lt;/span&gt;&lt;span class="kwrd"&gt;="btnRemove_Click"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;StackPanel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this example-&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;We have an ObservableCollection called tasks and all we have done to bind it to UI is to assign the ItemsSource property of listview to the ObservableCollection.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Everytime an item is added, changed or deleted, ObservableCollection notifies the consumer automatically. This ensures that the UI gets updated automatically.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-8260409831923758898?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/8260409831923758898'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/8260409831923758898'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2010/08/inotifypropertychanged-and.html' title='INotifyPropertyChanged and ObservableCollection'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-4985068880593483863</id><published>2010-08-02T02:14:00.000-04:00</published><updated>2011-11-09T00:12:16.855-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='DOS'/><category scheme='http://www.blogger.com/atom/ns#' term='Powershell'/><title type='text'>Useful DOS/Powershell Commands</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this blog, I wanted to document some of the nice DOS/powershell commands that I frequently use.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;nslookup - &lt;/b&gt;query the domain name service (DNS) to get information about domain names and ip addresses. For ex. nslookup www.microsoft.com or nslookup xxx.xxx.xxx.xxx&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;tracert - &lt;/b&gt;tracks the path a packet of information will take from your computer to the destination. It also gives the timing information. For ex. tracert www.microsoft.com&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;pathping - &lt;/b&gt;similar to tracert but it also pings each hop and gets additional information about timing, packets lost percentage etc. For ex. pathping www.microsoft.com&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Most of the commands in powershell are a combination of "verb-noun" components. Common verbs are Get, Set, Out, Start, Stop, Restart, Add. Common nouns are Help, Command, Service, Computer, Location, ChildItems. Some of the more useful commands are described below:&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;get-alias&lt;br /&gt;all commands in powershell have aliases. For ex. dir or ls (from linux based systems) both point to the same alias get-childitems. get-alias gets the aliases for all the commands.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;get-childitem&lt;br /&gt;similar to dir or ls in linux&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;get-command&lt;br /&gt;lists all the commands&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;get-command -verb "get"&lt;br /&gt;lists all commands with the verb get in it&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;get-command -noun "service"&lt;br /&gt;lists all commands with the noun service in it&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;get-help dir&lt;br /&gt;displays help for dir command&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;get-help dir -examples&lt;br /&gt;displays help for dir with examples&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;get-help dir -detailed&lt;br /&gt;displays a more detailed help&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;get-help dir -full&lt;br /&gt;displays the full help&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;get-command | where-object {$_.Name.Contains("Cult")}&lt;br /&gt;This example demonstrates pipelining. The output of get-command is fed into where-object command. where-object loops through each of the objects provided by get-command. $_ represents the "this" object in the .net context. So, this command will list all the commands whose name contains the word "Cult" in it.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;get-command -verb "write" | sort-object length&lt;br /&gt;sorts the input of get-command based on its length.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;dir | select-object name, lastwritetime&lt;br /&gt;select-object selects just the name and lastwritetime attributes from the input. The input is provided by the dir command.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;clear-host&lt;br /&gt;same as cls in cmd.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-4985068880593483863?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/4985068880593483863'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/4985068880593483863'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2010/08/useful-dospowershell-commands.html' title='Useful DOS/Powershell Commands'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-2895413271280702405</id><published>2010-06-03T20:10:00.002-04:00</published><updated>2011-06-04T00:57:34.192-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Excel'/><title type='text'>Some Excel Formulas</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Most of the people who use computers, have in some way or form used excel. Its always handy to know few important excel formulas that can help us write 1-liner code that otherwise in normal programming languages like C, C++ or even C# would have taken 10 or more lines of code. In this blog post, I will keep a list of helpful excel formulas.&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;If&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;syntax: if(condition, true value, false value)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;example: =if(A3&amp;gt;B3,"A3 is larger", "B3 is larger")&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;offset&lt;/b&gt;&amp;nbsp;- returns a reference to a range which is offset from provided cell/range&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;syntax: offset(from cell/range, rows up/down, column left/right, num of rows, num of cols)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;example:&amp;nbsp;=SUM(OFFSET(C7,2,3,4,5))&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this example, we start from C7 and then go 2 rows down because it is positive. If it was negative, we would have gone up. Then we go 3 columns to the right because it is positive. If it was negative, we would have gone left. Now from this cell, we select 4 rows and 5 columns and that becomes our result range. Then, we call SUM on this range.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;match - &lt;/b&gt;searches for a value in an array and returns the relative position&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;syntax: MATCH(value to search, array to search in, match type)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;example:&amp;nbsp;=MATCH(B13,OFFSET(C12,1,1,4,1),0)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this example, match will search the value in cell B13 in the array range provided by offset function. Match type 0 means it will return the relative position of the first value that is equal to the value in cell B13. Match type -1 would mean that it will find the smallest value that is greater than or equal to value in B13. Match type 1 would mean that it will find the largest value that is less than or equal to the value in B13. 1 is default.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;index -&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;syntax: index(range, row, col)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;example:=INDEX(D18:G20,2,3)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this example, the index function will return the value in row 2 and col 3 in the range D18:G20 i.e value in F19 cell.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;vlookup -&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;syntax: vlookup(value in first col, range, col to return value from, boolean match)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;example:&amp;nbsp;=VLOOKUP(3,D22:G27,4,FALSE)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this example, vlookup will search the cell with value 3 in the first column of range D22:G27. Then for that row, it will go to the 4th column and return the value. boolean match is to control the case whether an exact match for 3 is found or not. True means that if 3 is not found in the first col then return the next largest value which is less than 3. False means find the exact match = 3 in the first column.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-2895413271280702405?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/2895413271280702405'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/2895413271280702405'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2010/06/some-excel-formulas.html' title='Some Excel Formulas'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-7408971371792660768</id><published>2010-04-14T22:20:00.002-04:00</published><updated>2010-12-13T20:10:33.360-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Events</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;As I mentioned in my delegates article, it took me quite a lot of time to understand the importance of delegates and events. For some time, I was confused as to when would I need events. I understood the button click events but was not able to come up with a concrete non-UI scenario in which I would like to use events. In this blog post, I will share a scenario which made it very easy for me to understand events. I hope this makes it simple for you too -&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Let's say our scenario is that there is a Person class and the Person class has an age property and we want something to happen when the Person's age changes. To put the responsibility on the consumer class to call a function everytime the age changes would be an error-prone approach. So we don't want to take that route. Let's say that on top of that, we still want the consumer class to be able to define what that change action should be. So basically, we are saying that, Person class should define the event, however, the implementation would be taken care by the consumer class. How can we achieve this?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; A02Events&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DoSomething()&lt;br /&gt;        {&lt;br /&gt;            Person p = &lt;span class="kwrd"&gt;new&lt;/span&gt; Person { Name = &lt;span class="str"&gt;"Peter"&lt;/span&gt;, Age = 20 };&lt;br /&gt;            p.AgeChanged += &lt;span class="kwrd"&gt;new&lt;/span&gt; Action&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;(p_AgeChanged);&lt;br /&gt;&lt;br /&gt;            p.Age = 30;&lt;br /&gt;            System.Threading.Thread.Sleep(1000);&lt;br /&gt;            p.Age = 31;&lt;br /&gt;            System.Threading.Thread.Sleep(1000);&lt;br /&gt;            p.Age = 32;&lt;br /&gt;            System.Threading.Thread.Sleep(1000);&lt;br /&gt;            p.Age = 33;&lt;br /&gt;        &lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; p_AgeChanged(&lt;span class="kwrd"&gt;int&lt;/span&gt; arg)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"Age changed to "&lt;/span&gt; + arg);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Person&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; m_age;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Age&lt;br /&gt;        {&lt;br /&gt;            get { &lt;span class="kwrd"&gt;return&lt;/span&gt; m_age; }&lt;br /&gt;            set &lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;if&lt;/span&gt; (m_age != &lt;span class="kwrd"&gt;value&lt;/span&gt;)&lt;br /&gt;                {&lt;br /&gt;                    m_age = &lt;span class="kwrd"&gt;value&lt;/span&gt;;&lt;br /&gt;                    &lt;span class="kwrd"&gt;if&lt;/span&gt;(AgeChanged !=&lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;br /&gt;                        AgeChanged(&lt;span class="kwrd"&gt;value&lt;/span&gt;);&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;event&lt;/span&gt; Action&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; AgeChanged;&lt;br /&gt;    }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In this example -&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In the Person class we declare an event. Its an Action&amp;lt;int&amp;gt; delegate, meaning that it will take int as input and will return void.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In the A02Events class, we say that for this event call the method p_AgeChanged, everytime age changes. This call is made in the setter for Age property inside Person class.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Then, just for demonstration purposes, Peter ages very fast, almost at the rate of 1 year/1 sec. Everytime, the age is changed p_AgeChanged event is fired.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The output looks like -&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_a_Uk_rwmZ7g/TQaOE_PKwiI/AAAAAAAABt8/MH_VoJUqMnM/s1600/1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/_a_Uk_rwmZ7g/TQaOE_PKwiI/AAAAAAAABt8/MH_VoJUqMnM/s1600/1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-7408971371792660768?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/7408971371792660768'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/7408971371792660768'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2010/12/events.html' title='Events'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_a_Uk_rwmZ7g/TQaOE_PKwiI/AAAAAAAABt8/MH_VoJUqMnM/s72-c/1.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-5303591023160423693</id><published>2010-04-13T20:46:00.003-04:00</published><updated>2010-12-13T20:10:54.554-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Action and Func Delegates</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Earlier in .NET, we had to declare our own delegates definitions. This was cumbersome and more importantly not very intuitive. It took me a lot of time to understand the concepts of delegates. The newer versions of .NET frameworks come up with two handy delegates that are very easy to use -&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Action Delegate&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;This delegate allows us to encapsulate a method that is not going to return anything, i.e its return type is void.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The numerous overloads provided range from 0 to 16 and they are all generic, meaning they can all have different types based on your needs.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Since it covers till 16 input arguments, I feel that it is good enough to cover most of the cases. If your function needs more than 16 inputs, its probably a good time to refactor the function.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Func Delegate&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The difference between this delegate and Action delegate is that Func allows for return types.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;It also provides numerous overloads similar to Action delegate. The last type defined is considered to be the return type.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Similar to Action delegates, I think that this is good enough to cover most of the scenarios.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; A03ActionAndFunc&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Run()&lt;br /&gt;        {&lt;br /&gt;            Action&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; someMethod1;&lt;br /&gt;            Action&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;int&lt;/span&gt;, &lt;span class="kwrd"&gt;double&lt;/span&gt;&amp;gt; someMethod2;&lt;br /&gt;            Func&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; someMethod3;&lt;br /&gt;            Func&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;int&lt;/span&gt;, &lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; someMethod4;&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// based on business logic, we can decide which actual method to call&lt;/span&gt;&lt;br /&gt;            someMethod1 = Example1;&lt;br /&gt;            someMethod2 = Example2;&lt;br /&gt;            someMethod3 = Example3;&lt;br /&gt;            someMethod4 = Example4;&lt;br /&gt;&lt;br /&gt;            someMethod1(&lt;span class="str"&gt;"someMethod1"&lt;/span&gt;, 5);&lt;br /&gt;            someMethod2(&lt;span class="str"&gt;"someMethod2"&lt;/span&gt;, 5, 5.6);&lt;br /&gt;&lt;br /&gt;            Console.WriteLine(someMethod3(&lt;span class="str"&gt;"someMethod3"&lt;/span&gt;));&lt;br /&gt;            Console.WriteLine(someMethod4(&lt;span class="str"&gt;"someMethod4"&lt;/span&gt;, 5));&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Example1(&lt;span class="kwrd"&gt;string&lt;/span&gt; s, &lt;span class="kwrd"&gt;int&lt;/span&gt; i)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"Args: "&lt;/span&gt; + s + &lt;span class="str"&gt;", "&lt;/span&gt; + i);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Example2(&lt;span class="kwrd"&gt;string&lt;/span&gt; s, &lt;span class="kwrd"&gt;int&lt;/span&gt; i, &lt;span class="kwrd"&gt;double&lt;/span&gt; d)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"Args: "&lt;/span&gt; + s + &lt;span class="str"&gt;", "&lt;/span&gt; + i + &lt;span class="str"&gt;", "&lt;/span&gt; + d);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Example3(&lt;span class="kwrd"&gt;string&lt;/span&gt; s)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; s.ToUpper();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Example4(&lt;span class="kwrd"&gt;string&lt;/span&gt; s, &lt;span class="kwrd"&gt;int&lt;/span&gt; i)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; s.ToUpper() + &lt;span class="str"&gt;", "&lt;/span&gt; + i;&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;In the above example -&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The definition of someMethod3 suggests that it will take 1 input of type string and it will output type string.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The definition of someMethod4 suggests that it will take 2 inputs. One of type string and the other of type int. The output type will be string.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The output looks like:&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TQaJgfJqv0I/AAAAAAAABt4/5MVFNhZ2u8M/s1600/1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TQaJgfJqv0I/AAAAAAAABt4/5MVFNhZ2u8M/s1600/1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-5303591023160423693?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/5303591023160423693'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/5303591023160423693'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2010/04/action-and-func-delegates.html' title='Action and Func Delegates'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_a_Uk_rwmZ7g/TQaJgfJqv0I/AAAAAAAABt4/5MVFNhZ2u8M/s72-c/1.jpg' height='72' width='72'/></entry><entry><id>tag:blogger.com,1999:blog-7299750660882548845.post-4653502220673969642</id><published>2010-01-05T02:38:00.004-05:00</published><updated>2010-12-13T22:04:17.253-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Authentication'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET'/><title type='text'>ASP.NET Authentication</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Authentication: Who are you? Default: Windows&lt;br /&gt;Authorization: What are you allowed to do? Default: Anonymous&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;Authorization&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Represented via Authorization element in web.config&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;For IIS6&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;can have a combination of allow and deny entries&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;order of these entries matter as they are evaluated based on the order they are entered. The closest entry wins.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;anonymous users are represented by ? and * represents all users&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;with the entries, you can also define verbs such as get and post to configure if the user is authorized to do gets and posts.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;For IIS7 and forward&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;can have combination of add and remove entries. The add entries can have accessType defined as allow or deny.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;order doesn't matter. The rule is that deny always takes precedence over allow.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Since order doesn't matter, we should remember to use &amp;lt;clear/&amp;gt; to clear all entries that might exist in top level config files.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;IIS7 authorization will take care of not just managed resources such as aspx files but also the unmanaged resources in your app such as .pdf, .jpg etc. So it is better to use IIS7 authentication/authorization model than ASP.NETs authentication/authorization model&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;IPrincipal and IIdentity&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Once a user is authenticated, you can access him by HttpContext.User property or by Page.User property. Both these properties expose the IPrincipal.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;IPrincipal contains IIdentity and a function called IsInRole to identify whether the user belongs to a role or not.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;IIdentity contains the Name of the user, his AuthenticationType and whether he IsAuthenticated or not.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;FormsAuthentication&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;When not all your users can have windows accounts, windows authentication is not a good approach.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;It is cookie-based. What that means is that once you are authenticated, further requests are done based on the cookie. All the details are managed by ASP.NET.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Cookies are secured by encryption using secret key.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Once the cookie reaches the server, an HTTPModule named FormsAuthenticationModule decrypts and validates the cookie into HttpContext.User. This module also turns the outgoing 401 request into 302 redirect to login page.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Once you authenticate the user by some database lookup for his username and password, you can call the FormsAuthentication.RedirectFromLoginPage method to redirect him to your default page in the application.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The method takes the username and a boolean value indicating whether to create a persistent cookie on the hard drive or not.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;To log out, just call the FormsAuthentication.SignOut() method and then redirect them to the default page of your app. This will inturn, redirect them to login page and if they login correctly, it will go to the default page.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;An attacker might extract the cookie from the web request and store it on his machine and then use it to mimic the real user. To avoid this, we should try to set the RequireSSL option to true in the forms element inside authentication element. This would mean that the whole request stream is on SSL and then the cookie can't be extracted.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;You can also set the timeout option in the forms element inside authentication element. This will ensure that the cookie expires after a while.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;It is better to user Roles instead of users.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Roles have to be set in code by the developer. You can do so in the Global.asax file by handling the AuthenticateRequest event. It is fired when the security module has established the identity of the user.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;With the help of identity, you can find out which roles this user belongs to and then you can create a new GenericPrincipal with identity and roles.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;You can then assign this GenericPrincipal to the Context.User property.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Once you set up the roles, you can use the Location element in the configuration element in web.config to identify the resource and what all roles it can have as shown below:&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;location&lt;/span&gt; &lt;span class="attr"&gt;path&lt;/span&gt;&lt;span class="kwrd"&gt;="AdminsAndPlayersOnly.aspx"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;system.web&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;authorization&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;allow&lt;/span&gt; &lt;span class="attr"&gt;roles&lt;/span&gt;&lt;span class="kwrd"&gt;="Players"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;allow&lt;/span&gt; &lt;span class="attr"&gt;roles&lt;/span&gt;&lt;span class="kwrd"&gt;="Admin"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;deny&lt;/span&gt; &lt;span class="attr"&gt;users&lt;/span&gt;&lt;span class="kwrd"&gt;="*"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;authorization&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;system.web&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;location&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;&lt;b&gt;ASP.NET Membership&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Membership and role providers provide an inbuilt implementation of general login features such as usernames, passwords and roles database, password resetting, security question features etc. Its designed for rapid action development but does not provide strong authentication.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Membership is all about forms authentication.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Membership also provides out of box controls to login, logout, password recovery, reset password etc. &lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;Membership and role use default providers but they can be customized to use different providers.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: Verdana, sans-serif;"&gt;The login code can be overridden to do something different if you don't want the default behavior.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7299750660882548845-4653502220673969642?l=www.akhildeshpande.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/4653502220673969642'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7299750660882548845/posts/default/4653502220673969642'/><link rel='alternate' type='text/html' href='http://www.akhildeshpande.com/2010/12/aspnet-authentication.html' title='ASP.NET Authentication'/><author><name>Akhil Deshpande</name><uri>http://www.blogger.com/profile/09591161863079979432</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry></feed>
