<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www.pluralsight.com/community/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">Experiments In Writing</title><subtitle type="html">by Scott Allen</subtitle><id>http://www.pluralsight.com/community/blogs/scottallen/atom.aspx</id><link rel="alternate" type="text/html" href="http://www.pluralsight.com/community/blogs/scottallen/default.aspx" /><link rel="self" type="application/atom+xml" href="http://www.pluralsight.com/community/blogs/scottallen/atom.aspx" /><generator uri="http://communityserver.org" version="4.0.30619.63">Community Server</generator><updated>2007-10-03T12:43:00Z</updated><entry><title>Visual Studio SP1 and The Metification of REST</title><link rel="alternate" type="text/html" href="/community/blogs/scottallen/archive/2008/08/11/visual-studio-sp1-and-the-metification-of-rest.aspx" /><id>/community/blogs/scottallen/archive/2008/08/11/visual-studio-sp1-and-the-metification-of-rest.aspx</id><published>2008-08-12T02:39:00Z</published><updated>2008-08-12T02:39:00Z</updated><content type="html">&lt;p&gt;&lt;strong&gt;Metification&lt;/strong&gt; – verb&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;The act of adding metadata to a web service in order to facilitate tooling and discovery. &lt;/li&gt;    &lt;li&gt;The act of adding complexity to a web service in order to achieve tight coupling. &lt;/li&gt; &lt;/ol&gt;  &lt;blockquote&gt;   &lt;p&gt;Pick one. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Service Pack 1 for Visual Studio 2008 has just arrived with new features, including version 1.0 of &lt;a href="http://astoria.mslivelabs.com/" target="_blank"&gt;ADO.NET Data Services&lt;/a&gt; (a.k.a Astoria). From the description (highlighting is mine):&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;ADO.NET Data Services … consists of a combination of patterns and libraries that enables any data store to be exposed as a flexible data service, naturally integrating with the Web, that can be consumed by Web clients within a corporate network or across the Internet. ADO.NET Data Services uses URIs to point to pieces of data and &lt;strong&gt;simple, well-known formats&lt;/strong&gt; to represent that data, such as JSON and ATOM/APP. This results in data being exposed to Web clients as a &lt;strong&gt;REST-style resource collection&lt;/strong&gt;, &lt;strong&gt;addressable with URIs that agents can interact with using standard HTTP verbs such as GET, POST, or DELETE&lt;/strong&gt;. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Compared to the traditional SOAP approach, the &lt;a href="http://www.google.com/search?hl=en&amp;amp;q=REST" target="_blank"&gt;REST&lt;/a&gt;-style is a different model for exposing functionality over a web service. Instead of defining messages and exposing operations that act on those messages, you expose resources and act on the resources using common HTTP verbs. I’ve lately been thinking of SOAP based web services as “verb oriented” (exposing GetOrder and UpdateCustomer), while REST style web services are “noun oriented” (exposing Orders and Customers). Both models have advantages and disadvantages, but I’ve felt that REST partners well with rich, Internet applications that need to retrieve a variety of resources&amp;#160; using the same filtering and paging parameters. Modeling a heap of GetThisByThat operations using SOAP is tedious.&amp;#160; &lt;/p&gt;  &lt;p&gt;Noun and verbs aren’t the only difference between REST and SOAP. One of the primary strengths of REST is its inherent simplicity. The simplicity not only facilitates broad interoperability, but encourages an acceptance of REST from many who feel overwhelmed by the complexities of &lt;a href="http://www.google.com/search?hl=en&amp;amp;q=ws+death+star" target="_blank"&gt;WS-*&lt;/a&gt;. There are no tools required for REST - all you need is the ability to send an HTTP request and read the response. WS-*, on the other hand, is great when you need a digitally signed message including double-secret user credentials routed through an asynchronous and distributed, two-phase commit transaction with an extended buyer protection. Not everyone needs that flexibility, but you still pay the price for the flexibility when using the tooling and the API, and when configuring the service. &lt;/p&gt;  &lt;p&gt;Although we could continue talking about differences in REST and SOAP, I wanted to talk about metadata, and Astoria. &lt;/p&gt;  &lt;h2&gt;&lt;/h2&gt;  &lt;h3&gt;Metafication&lt;/h3&gt;  &lt;p&gt;REST proponents, as a rule of thumb, shun metadata – but not all forms of metadata. Metadata in prose or written documentation is fine. Metadata in a self-describing response format is fine. However, metadata for tooling is seen by many as pure evil. Part of the complexity in WS-* is in the quirky and convoluted folds of metadata formats like WSDL and XML Schema. REST has seen some attempts at standardized metadata (&lt;a href="http://www.25hoursaday.com/weblog/2007/06/04/WhatsWrongWithWADL.aspx" target="_blank"&gt;WADL&lt;/a&gt;, WSDL 2.0, XSD), but still resists all attempts for the most part.&amp;#160; &lt;/p&gt;  &lt;p&gt;I like metadata. Maybe I’ve been in the .NET ecosystem for so long that I expect tooling, but I still remember the first time I tried to write a program for the &lt;a href="http://www.flickr.com/services/api/" target="_blank"&gt;Flickr web service&lt;/a&gt; (which is technically just POX). I was shocked when I coudn’t find a WSDL file. Then I was surprised at how easy it was to craft the correct URL for an HTTP request, and shred apart the XML response to find photographs. It was so easy that ... well, it was just too easy. It reminded me of writing data access code from scratch. Data access code is so predictable and repetitive that we have tools, frameworks, and code generators to take care of the job. But those tools, frameworks, and code generators rely on metadata defined by a database schema, so their job is relatively straightforward. REST is a bit different, unless you are working with Astoria on the server and a CLR client. &lt;/p&gt;  &lt;p&gt;Let’s say you have some DTOs for employees, orders, and other objects you want to send over the wire. You’ll need to decorate them with enough information for the service to understand the primary key.&lt;/p&gt;  &lt;pre class="code"&gt;[&lt;span style="color:#2b91af;"&gt;DataServiceKey&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)]
&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Employee
&lt;/span&gt;{
   
    &lt;span style="color:blue;"&gt;public int &lt;/span&gt;ID { &lt;span style="color:blue;"&gt;get&lt;/span&gt;; &lt;span style="color:blue;"&gt;set&lt;/span&gt;; }
    &lt;span style="color:blue;"&gt;public string &lt;/span&gt;Name { &lt;span style="color:blue;"&gt;get&lt;/span&gt;; &lt;span style="color:blue;"&gt;set&lt;/span&gt;; }
}
[&lt;span style="color:#2b91af;"&gt;DataServiceKey&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)]
&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Order
&lt;/span&gt;{
    // …
}&lt;/pre&gt;

&lt;p&gt;Next, define a class with public IQueryable&amp;lt;T&amp;gt; properties for each “entity set” (Employees and Orders). IQueryable&amp;lt;T&amp;gt; is easy to conjure up, and the class below represents a read-only data source with some fake in-memory data. If you need create, update, and delete functionality the class will need to implement IUpdateable, too. Sean Wildermuth has a &lt;a href="http://wildermuth.com/2008/07/01/Implementing_IUpdatable_(Part_1)" target="_blank"&gt;three series blog post&lt;/a&gt; about IUpdateable that he wrote when implementing IUpdateable for the NHibernate LINQ project. &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;AcmeData &lt;/span&gt;&lt;span style="color:green;"&gt;
&lt;/span&gt;{
    public &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IQueryable&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Employee&lt;/span&gt;&amp;gt; Employees
    {
        &lt;span style="color:blue;"&gt;get 
        &lt;/span&gt;{   &lt;span style="color:blue;"&gt;return new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Employee&lt;/span&gt;&amp;gt;
            {
                &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Employee&lt;/span&gt;() &lt;span style="color:green;"&gt;/* ... */&lt;/span&gt;,
                &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Employee&lt;/span&gt;() &lt;span style="color:green;"&gt;/* ... */&lt;/span&gt;,
                &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Employee&lt;/span&gt;() &lt;span style="color:green;"&gt;/* ... */
                // ...
            &lt;/span&gt;}.AsQueryable();        
        }
    }

&lt;span style="color:blue;"&gt;    public &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IQueryable&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Order&lt;/span&gt;&amp;gt; Orders
    {
        &lt;span style="color:green;"&gt;// ...
    &lt;/span&gt;}
    &lt;span style="color:green;"&gt;// ...
&lt;/span&gt;}&lt;/pre&gt;

&lt;p&gt;Then you need an .svc file…&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;%&lt;span style="color:#a31515;"&gt;@ &lt;/span&gt;&lt;span style="color:red;"&gt;ServiceHost Language&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:blue;"&gt;C#&lt;/span&gt;&amp;quot; 
    &lt;span style="color:red;"&gt;Factory&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:blue;"&gt;System.Data.Services.DataServiceHostFactory, 
             System.Data.Services, 
             Version=3.5.0.0, Culture=neutral, 
             PublicKeyToken=b77a5c561934e089&lt;/span&gt;&amp;quot; 
    &lt;span style="color:red;"&gt;Service&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:blue;"&gt;AcmeDataService&lt;/span&gt;&amp;quot; %&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;… and you’ll also need a code-behind file for the .svc (which is all setup for you using an ADO.NET data service template, you just add some configuration):&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;AcmeDataService &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;DataService&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;AcmeData&lt;/span&gt;&amp;gt;
{        
    &lt;span style="color:blue;"&gt;public static void &lt;/span&gt;InitializeService(&lt;span style="color:#2b91af;"&gt;IDataServiceConfiguration &lt;/span&gt;config)
    {
        config.SetEntitySetAccessRule(&lt;span style="color:#a31515;"&gt;&amp;quot;Employees&amp;quot;&lt;/span&gt;, &lt;span style="color:#2b91af;"&gt;EntitySetRights&lt;/span&gt;.AllRead);
        &lt;span style="color:green;"&gt;// ... more rules
    &lt;/span&gt;}
}&lt;/pre&gt;

&lt;p&gt;At this point you can start testing the service using a web browser and looking at, for example, http://localhost/AcmeDataService.svc/Employees. What is more interesting is looking at http://localhost/AcmeDataService.svc/$metadata, because there you’ll find service metadata, which is where the magic starts. &lt;/p&gt;

&lt;p&gt;To consume the service, right-click on a project in Visual Studio and select “Add Service Reference…”. Yes – the same “Add Service Reference” command you might have seen in the hit motion picture “SOAP and WSDL – an XML Love Story”. This feature blurs the lines between REST and WS-*. Enter the root URL to the service and Visual Studio will generate a proxy – but not the type of proxy you receive when using SOAP based web services. This proxy will derive from DataServiceContext class and you can use it like so:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;employees = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;AcmeData&lt;/span&gt;(serviceRoot)
                    .Employees
                    .Where(e =&amp;gt; e.Name == &lt;span style="color:#a31515;"&gt;&amp;quot;Scott&amp;quot;&lt;/span&gt;)
                    .OrderBy(e =&amp;gt; e.Name)
                    .Skip(2)
                    .Take(2)                      
                    .ToList();&lt;/pre&gt;

&lt;p&gt;DataServiceContext does a little bit of magic to turn the LINQ query into the following HTTP request. It’s LINQ to REST:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;GET /AcmeDataService.svc/Employees()&lt;br /&gt;    ?$filter=Name%20eq%20&amp;#39;Scott&amp;#39;&amp;amp;$orderby=Name&amp;amp;$skip=2&amp;amp;$top=2 HTTP/1.1&lt;br /&gt;User-Agent: Microsoft ADO.NET Data Services&lt;br /&gt;Accept: application/atom+xml,application/xml&lt;br /&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;The data service will respond with some XML that the data context uses to create objects that look just like the server side DTOs. &lt;/p&gt;

&lt;p&gt;I’m sure some are horrified at this metification of REST, but for scenarios when you need to talk between two CLR appdomains (think ASP.NET and Silverlight), this approach gives you the advantages of thinking about nouns in a RESTful model without writing all the glue code to wire up an endpoints and parse XML. Beauty! &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://www.pluralsight.com/community/aggbug.aspx?PostID=52475" width="1" height="1"&gt;</content><author><name>scott-allen</name><uri>http://www.pluralsight.com/community/members/scott_2D00_allen/default.aspx</uri></author></entry><entry><title>Optimizing LINQ Queries</title><link rel="alternate" type="text/html" href="/community/blogs/scottallen/archive/2008/07/14/optimizing-linq-queries.aspx" /><id>/community/blogs/scottallen/archive/2008/07/14/optimizing-linq-queries.aspx</id><published>2008-07-14T23:34:00Z</published><updated>2008-07-14T23:34:00Z</updated><content type="html">&lt;p&gt;I&amp;rsquo;ve been asked a few times about how to optimize LINQ code. The first step in optimizing LINQ code is to take some measurements and make sure you really have a problem.&amp;nbsp; &lt;/p&gt;
&lt;p align="center"&gt;&lt;a href="http://blogs.msdn.com/ericgu/archive/2006/06/26/647877.aspx"&gt;&lt;img title="premature" style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" alt="premature" src="http://pluralsight.com/community/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/scottallen/premature_5F00_6.jpg" border="0" height="417" width="564" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It turns out that optimizing LINQ code isn&amp;rsquo;t that different from optimizing regular C# code. You need to form a hypothesis, make changes, and measure, measure, measure every step of the way. Measurement is important, because sometimes the changes you need to make are not intuitive. &lt;/p&gt;
&lt;p&gt;Here is a specific example using LINQ to Objects. &lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s say we have 100,000 of these in memory: &lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;CensusRecord&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;    &lt;span style="color:blue;"&gt;public string &lt;/span&gt;District{ &lt;span style="color:blue;"&gt;get&lt;/span&gt;; &lt;span style="color:blue;"&gt;set&lt;/span&gt;; }&lt;br /&gt;    &lt;span style="color:blue;"&gt;public long &lt;/span&gt;Males { &lt;span style="color:blue;"&gt;get&lt;/span&gt;; &lt;span style="color:blue;"&gt;set&lt;/span&gt;; }&lt;br /&gt;    &lt;span style="color:blue;"&gt;public long &lt;/span&gt;Females { &lt;span style="color:blue;"&gt;get&lt;/span&gt;; &lt;span style="color:blue;"&gt;set&lt;/span&gt;; }&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We need a query that will give us back a list of districts ordered by their male / female population ratio, and include the ratio in the query result. A first attempt might look like this:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;query =&lt;br /&gt;    &lt;span style="color:blue;"&gt;from &lt;/span&gt;r &lt;span style="color:blue;"&gt;in &lt;/span&gt;_censusRecords&lt;br /&gt;    &lt;span style="color:blue;"&gt;orderby &lt;/span&gt;(&lt;span style="color:blue;"&gt;double&lt;/span&gt;)r.Males / (&lt;span style="color:blue;"&gt;double&lt;/span&gt;)r.Females &lt;span style="color:blue;"&gt;descending&lt;br /&gt;    select new&lt;br /&gt;    &lt;/span&gt;{&lt;br /&gt;        District = r.District,&lt;br /&gt;        Ratio = (&lt;span style="color:blue;"&gt;double&lt;/span&gt;)r.Males / (&lt;span style="color:blue;"&gt;double&lt;/span&gt;)r.Females&lt;br /&gt;    };&lt;br /&gt;&lt;br /&gt;query = query.ToList();&lt;br /&gt; &lt;/pre&gt;
&lt;p&gt;It&amp;rsquo;s tempting to look at the query and think - &amp;ldquo;If we only calculate the ratio once, we can make the query faster &lt;i&gt;and&lt;/i&gt; more readable! A win-win!&amp;rdquo;. We do this by introducing a new range variable with the &lt;i&gt;let&lt;/i&gt; clause:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;query =&lt;br /&gt;    &lt;span style="color:blue;"&gt;from &lt;/span&gt;r &lt;span style="color:blue;"&gt;in &lt;/span&gt;_censusRecords&lt;br /&gt;    &lt;b&gt;&lt;span style="color:blue;"&gt;let &lt;/span&gt;ratio = (&lt;span style="color:blue;"&gt;double&lt;/span&gt;)r.Males / (&lt;span style="color:blue;"&gt;double&lt;/span&gt;)r.Females&lt;/b&gt;
    &lt;span style="color:blue;"&gt;orderby &lt;/span&gt;ratio &lt;span style="color:blue;"&gt;descending &lt;br /&gt;    select new&lt;br /&gt;    &lt;/span&gt;{&lt;br /&gt;        District = r.District,&lt;br /&gt;        Ratio = ratio &lt;br /&gt;    };&lt;br /&gt;&lt;br /&gt;query = query.ToList();                &lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you measure the execution time of each query on 100,000 objects, however, you&amp;rsquo;ll find the second query is about 14% &lt;i&gt;slower&lt;/i&gt; than the first query, despite the fact that we are only calculating the ratio once. Surprising! See why we need to take measurements? &lt;/p&gt;
&lt;h3&gt;Look At Time &lt;i&gt;and&lt;/i&gt; Space&lt;/h3&gt;
&lt;p&gt;The key to this specific issue is understanding how the C# compiler introduces the range variable &lt;i&gt;ratio &lt;/i&gt;into the query processing. We know that C# translates declarative queries into a series of method calls. Imagine the method calls forming a pipeline for pumping objects. The first query we wrote would translate into the following:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;query =&lt;br /&gt;    _censusRecords.OrderByDescending(r =&amp;gt; (&lt;span style="color:blue;"&gt;double&lt;/span&gt;)r.Males /&lt;br /&gt;                                          (&lt;span style="color:blue;"&gt;double&lt;/span&gt;)r.Females)&lt;br /&gt;                  .Select(r =&amp;gt; &lt;span style="color:blue;"&gt;new &lt;/span&gt;{ District = r.District,&lt;br /&gt;                                     Ratio = (&lt;span style="color:blue;"&gt;double&lt;/span&gt;)r.Males /&lt;br /&gt;                                             (&lt;span style="color:blue;"&gt;double&lt;/span&gt;)r.Females });&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The second query, the one with the &lt;i&gt;let&lt;/i&gt; clause, is asking LINQ to pass an additional piece of state through the object pipeline. In other words, we need to pump both a CensusRecord object and a double value (the ratio) into the OrderByDescending and Select methods. There is no magic involved - the only way to get both pieces of data through the pipeline is to instantiate a new object that will carry both pieces of data. When C# is done translating the second query, the result looks like this:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;query =&lt;br /&gt;    _censusRecords.Select(r =&amp;gt; &lt;span style="color:blue;"&gt;new &lt;/span&gt;{ Record = r, &lt;br /&gt;                                     Ratio = (&lt;span style="color:blue;"&gt;double&lt;/span&gt;)r.Males / &lt;br /&gt;                                             (&lt;span style="color:blue;"&gt;double&lt;/span&gt;)r.Females })&lt;br /&gt;                  .OrderByDescending(r =&amp;gt; r.Ratio)&lt;br /&gt;                  .Select(r =&amp;gt; &lt;span style="color:blue;"&gt;new &lt;/span&gt;{ District = r.Record.District,&lt;br /&gt;                                     Ratio = r.Ratio });&lt;br /&gt;&lt;/pre&gt;
&lt;p align="center"&gt;&lt;img title="clr profiler results" style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" alt="clr profiler results" src="http://pluralsight.com/community/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/scottallen/profile_5F00_3.jpg" border="0" height="237" width="244" /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The above query requires &lt;i&gt;two&lt;/i&gt; projections, which is 200,000 object instantiations.&amp;nbsp; &lt;a href="http://www.microsoft.com/DownLoads/details.aspx?familyid=A362781C-3870-43BE-8926-862B40AA0CD0&amp;amp;displaylang=en" target="_blank"&gt;CLR Profiler&lt;/a&gt; says the &lt;i&gt;let &lt;/i&gt;version of the query uses 60% more memory.&lt;/p&gt;
&lt;p&gt;Now we have a better idea why performance decreased, and we can try a different optimization. We&amp;rsquo;ll write the query using method calls instead of a declarative syntax, and do a projection into the type we need &lt;i&gt;first&lt;/i&gt;, and &lt;i&gt;then&lt;/i&gt; order the objects. &lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;query =&lt;br /&gt;    _censusRecords.Select(r =&amp;gt; &lt;span style="color:blue;"&gt;new &lt;/span&gt;{ District = r.District, &lt;br /&gt;                                     Ratio = (&lt;span style="color:blue;"&gt;double&lt;/span&gt;)r.Males / &lt;br /&gt;                                             (&lt;span style="color:blue;"&gt;double&lt;/span&gt;)r.Females })&lt;br /&gt;                  .OrderByDescending(r =&amp;gt; r.Ratio);&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This query will perform about 6% faster than the first query in the post, but consistently (and mysteriously) uses 5% more memory. Ah, tradeoffs. &lt;/p&gt;
&lt;h1&gt;&lt;/h1&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;Moral Of The Story?&lt;/h3&gt;
&lt;p&gt;The moral of the story is &lt;b&gt;not&lt;/b&gt; to rewrite all your LINQ queries to save a 5 milliseconds here and there. The first priority is always to build working, maintainable software. The moral of the story &lt;b&gt;is&lt;/b&gt; that LINQ, like any technology, requires analysis and measurements to make optimization gains because the path to better performance isn&amp;rsquo;t always obvious. Also remember that a query &amp;ldquo;optimized&amp;rdquo; for LINQ to Objects might make things worse when the same query uses a different provider, like LINQ to SQL. &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://www.pluralsight.com/community/aggbug.aspx?PostID=51850" width="1" height="1"&gt;</content><author><name>scott-allen</name><uri>http://www.pluralsight.com/community/members/scott_2D00_allen/default.aspx</uri></author></entry><entry><title>Using an ORM? Think Objects!</title><link rel="alternate" type="text/html" href="/community/blogs/scottallen/archive/2008/07/13/using-an-orm-think-objects.aspx" /><id>/community/blogs/scottallen/archive/2008/07/13/using-an-orm-think-objects.aspx</id><published>2008-07-14T02:40:00Z</published><updated>2008-07-14T02:40:00Z</updated><content type="html">&lt;p&gt;I recently had some time on airplanes to read through &lt;a href="http://www.manning.com/tate2/" target="_blank"&gt;Bitter EJB&lt;/a&gt;, &lt;a href="http://www.manning.com/crichardson/" target="_blank"&gt;POJOs in Action&lt;/a&gt;, and&amp;nbsp; &lt;a href="http://www.amazon.com/Better-Faster-Lighter-Java-Bruce/dp/0596006764" target="_blank"&gt;Better, Faster, Lighter Java&lt;/a&gt;. All three books were good, but the last one was my favorite, and was recommended to me by &lt;a href="http://codebetter.com/blogs/ian_cooper/" target="_blank"&gt;Ian Cooper&lt;/a&gt;. No, I&amp;rsquo;m not planning on trading in assemblies for jar files just yet. I read the books to get some insight and perspectives into specific trends in the Java ecosystem. &lt;a href="http://www.scaryforkids.com/a-sound-of-thunder/" target="_blank"&gt;&lt;img title="A Sound Of Thunder" style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" alt="A Sound Of Thunder" src="http://www.pluralsight.com/community/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/scottallen/a_2D00_sound_2D00_of_2D00_thunder_5F00_3.jpg" align="right" border="0" height="317" width="250" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s impossible to summarize the books in one paragraph, but I&amp;rsquo;ll try anyway:&lt;/p&gt;
&lt;p&gt;Some Java developers shun the EJB framework so they can focus on objects. &lt;i&gt;Simple&lt;/i&gt; objects. &lt;i&gt;Testable&lt;/i&gt; objects. &lt;i&gt;Malleable&lt;/i&gt; objects. &lt;i&gt;Plain old Java objects&lt;/i&gt; that solve business problems without being encumbered by infrastructure and technology concerns. &lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s the gist of the three books in 35 words. The books also talk about patterns, anti-patterns, domain driven design, lightweight frameworks, processes, and generally how to&amp;nbsp; write software. You&amp;rsquo;d be surprised how much content is applicable to .NET. In fact, when reading through the books I began to think of .NET and Java as two parallel universes whose deviations could be explained by the accidental killing of one butterfly during a &lt;a href="http://en.wikipedia.org/wiki/A_Sound_of_Thunder" target="_blank"&gt;time traveling safari&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;The focus of this post is one particular deviation that really stood out. &lt;/p&gt;
&lt;h3&gt;From Objects To ORMs&lt;/h3&gt;
&lt;p&gt;The Java developers who focus on objects eventually have to deal with other concerns like persistence. Their&amp;nbsp; object focus naturally leads some of them to try object-relational mapping frameworks. ORMs like &lt;a href="http://www.hibernate.org/" target="_blank"&gt;Hibernate&lt;/a&gt; not only provide these developers with productivity gains, but do so in a &lt;i&gt;relatively&lt;/i&gt;&amp;nbsp;&lt;a href="http://www.hibernate.org/345.html" target="_blank"&gt;transparent&lt;/a&gt; and non-intrusive manner. The two work well together right from the start as the developers understand the ORMs, and the ORMs seem to understand the developers. &lt;/p&gt;
&lt;h3&gt;From DataSets to ORMs&lt;/h3&gt;
&lt;p&gt;.NET includes includes DataSets, DataTables, and DataViews. There is an IDE with a Data menu, and a GUI toolbox with Data tab full of Data controls and DataSources. It&amp;rsquo;s easy to stereotype mainstream .NET development as data centric. When you introduce an ORM to a .NET developer who has never seen one, the typical questions are along the lines of:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;How do I manage my identity values after an INSERT?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;... and ...&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Does this thing work with stored procedures?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Perfectly reasonable questions given the data-centric atmosphere of .NET, but you can almost feel the tension in these questions. And that is the deviation that stood out to me. On the airplane, I read about Java developers who focused on objects and went in search of ORMs. In .NET land, I&amp;rsquo;m seeing the ORMs going in search of the developer who is focused on data. The ORMs in particular are LINQ to SQL (currently shipping in Visual Studio) and the Entity Framework (shipping in SP1). Anyone expecting something like &amp;ldquo;ADO.NET 3.5&amp;rdquo; is in for a surprise. Persistent entities and DataSets are two different creatures, and require two different mind sets. &lt;/p&gt;
&lt;h3&gt;Will .NET Developers Focus On Objects?&lt;/h3&gt;
&lt;p&gt;It&amp;rsquo;s possible, but the tools make it difficult. The Entity Framework, for instance, presents developers with cognitive dissonance at several points. The documentation will tell you the goal is for EF to create a rich, conceptual object model, but the press releases proclaim that the Entity Framework &lt;a href="http://www.microsoft.com/presspass/press/2007/dec07/12-06EntityBeta3PR.mspx" target="_blank"&gt;simplifies data-centric development&lt;/a&gt;.&amp;nbsp; There will not be any plain old CLR objects (POCOs) in EF, and the object-focused implicit lazy-loading that comes standard in most ORMs isn&amp;rsquo;t available (you can read any property on this entity, um, except &lt;i&gt;that&lt;/i&gt; one &amp;ndash; you&amp;rsquo;ll have to load it first). &lt;/p&gt;
&lt;p&gt;LINQ to SQL is different. LINQ to SQL is &lt;a href="http://blogs.msdn.com/mattwar/archive/2007/06/21/linq-to-sql-objects-all-the-way-down.aspx" target="_blank"&gt;objects all the way down&lt;/a&gt;. You can use plain old CLR objects with LINQ to SQL if you dig beyond the surface. However, the surface is a shiny designer that looks just like the typed DataSet designer. LINQ to SQL also needs some additional mapping flexibility to truly separate the object&amp;nbsp; model from the underlying database schema &amp;ndash; hopefully we&amp;rsquo;ll see this in the next version. &lt;/p&gt;
&lt;h3&gt;What To Do?&lt;/h3&gt;
&lt;p&gt;If you are a .NET developer who is starting to use an ORM &amp;ndash;any ORM, you owe it to yourself and your project to reset your defaults and think differently about the new paradigm. Forget what you know about DataSets and learn about the unit of work pattern. Forget what you know about data readers and learn how an ORM identity map works. Think objects first, data second. If you can&amp;rsquo;t think of data second, an ORM might not be the technology for you.&amp;nbsp; &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://www.pluralsight.com/community/aggbug.aspx?PostID=51829" width="1" height="1"&gt;</content><author><name>scott-allen</name><uri>http://www.pluralsight.com/community/members/scott_2D00_allen/default.aspx</uri></author></entry><entry><title>LINQ Deep Dive at D.C. ALT.NET Next Week</title><link rel="alternate" type="text/html" href="/community/blogs/scottallen/archive/2008/07/11/linq-deep-dive-at-d-c-alt-net-next-week.aspx" /><id>/community/blogs/scottallen/archive/2008/07/11/linq-deep-dive-at-d-c-alt-net-next-week.aspx</id><published>2008-07-11T05:42:29Z</published><updated>2008-07-11T05:42:29Z</updated><content type="html">&lt;p&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2008/07/09/dc-alt-net-7-24-2008-linq-deep-dive-with-scott-allen.aspx" target="_blank"&gt;Matt Podwysocki&lt;/a&gt; invited me to speak at the D.C. alt.net meeting next Thursday evening (July 24th). The topic is LINQ. Matt specifically requested a code-heavy presentation, so expect two slides followed by plenty of hot lambda and Expression&amp;lt;T&amp;gt; action. &lt;/p&gt;  &lt;p&gt;Hopefully, Matt doesn’t &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2008/07/10/aspects-of-functional-programming-in-c-presentation-and-code.aspx" target="_blank"&gt;blackout the neighborhood&lt;/a&gt; like he did at the nearby RockNUG meeting this week. The White House is two blocks away and the people inside get a little jumpy about blackouts. &lt;/p&gt;  &lt;p&gt;&lt;/span&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;span style="text-decoration:underline;"&gt;DateTime:&lt;/span&gt;     &lt;br /&gt;7/24/2008 - 7PM-9PM     &lt;br /&gt;    &lt;br /&gt;&lt;span style="text-decoration:underline;"&gt;Location:&lt;/span&gt;     &lt;br /&gt;Cynergy Systems Inc.     &lt;br /&gt;1600 K St NW     &lt;br /&gt;Suite 300     &lt;br /&gt;Washington, DC 20006     &lt;br /&gt;&lt;a href="http://maps.google.com/maps?q=1600+K+Street+NW,+Washington,+DC++20006&amp;amp;iwloc=A&amp;amp;hl=en"&gt;&lt;font color="#800080"&gt;Show Map&lt;/font&gt;&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://www.pluralsight.com/community/aggbug.aspx?PostID=51798" width="1" height="1"&gt;</content><author><name>scott-allen</name><uri>http://www.pluralsight.com/community/members/scott_2D00_allen/default.aspx</uri></author></entry><entry><title>Keeping LINQ Code Healthy</title><link rel="alternate" type="text/html" href="/community/blogs/scottallen/archive/2008/07/08/keeping-linq-code-healthy.aspx" /><id>/community/blogs/scottallen/archive/2008/07/08/keeping-linq-code-healthy.aspx</id><published>2008-07-09T02:40:23Z</published><updated>2008-07-09T02:40:23Z</updated><content type="html">&lt;p&gt;In the BI space I’ve seen a lot of SQL queries succumb to complexity. A data extraction query adds some joins, then some filters, then some nested SELET statements, and it becomes an unhealthy mess in short order. It’s unfortunate, but standard SQL just isn’t a language geared for refactoring towards simplification (although &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc164062.aspx" target="_blank"&gt;UDFs&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc163346.aspx" target="_blank"&gt;CTE&lt;/a&gt;s in T-SQL have helped). &lt;/p&gt;  &lt;p&gt;I’ve really enjoyed writing LINQ queries this year, and I’ve found them easy to keep &lt;a href="http://www.codinghorror.com/blog/archives/000615.html" target="_blank"&gt;pretty&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;For example, suppose you need to parse some values out of the following XML:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;ROOT&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;data&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;record&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;field &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:blue;"&gt;Country&lt;/span&gt;&amp;quot;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;Afghanistan&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;field&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;field &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:blue;"&gt;Year&lt;/span&gt;&amp;quot;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;1993&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;field&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;field &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:blue;"&gt;Value&lt;/span&gt;&amp;quot;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;16870000&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;field&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
      &amp;lt;!--  &lt;/span&gt;&lt;span style="color:green;"&gt;... &lt;/span&gt;&lt;span style="color:blue;"&gt;--&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;record&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
    &amp;lt;!--  &lt;/span&gt;&lt;span style="color:green;"&gt;... &lt;/span&gt;&lt;span style="color:blue;"&gt;--&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;data&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;ROOT&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;A first crack might look like the following:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;entries =
    &lt;span style="color:blue;"&gt;from &lt;/span&gt;r &lt;span style="color:blue;"&gt;in &lt;/span&gt;doc.Descendants(&lt;span style="color:#a31515;"&gt;&amp;quot;record&amp;quot;&lt;/span&gt;)
    &lt;span style="color:blue;"&gt;select new
    &lt;/span&gt;{
        Country = r.Elements(&lt;span style="color:#a31515;"&gt;&amp;quot;field&amp;quot;&lt;/span&gt;)
                   .Where(f =&amp;gt; f.Attribute(&lt;span style="color:#a31515;"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;) .Value == &lt;span style="color:#a31515;"&gt;&amp;quot;Country&amp;quot;&lt;/span&gt;)
                   .First().Value,
        Year    = r.Elements(&lt;span style="color:#a31515;"&gt;&amp;quot;field&amp;quot;&lt;/span&gt;)
                   .Where(f =&amp;gt; f.Attribute(&lt;span style="color:#a31515;"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;).Value == &lt;span style="color:#a31515;"&gt;&amp;quot;Year&amp;quot;&lt;/span&gt;)
                   .First().Value,
        Value = &lt;span style="color:blue;"&gt;double&lt;/span&gt;.Parse
                 (r.Elements(&lt;span style="color:#a31515;"&gt;&amp;quot;field&amp;quot;&lt;/span&gt;)
                   .Where(f =&amp;gt; f.Attribute(&lt;span style="color:#a31515;"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;).Value == &lt;span style="color:#a31515;"&gt;&amp;quot;Value&amp;quot;&lt;/span&gt;)
                   .First().Value)
    };&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;The above is just a mass of method calls and string literals. But, add in a quick helper or extension method…&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public static &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;XElement &lt;/span&gt;Field(&lt;span style="color:blue;"&gt;this &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;XElement &lt;/span&gt;element, &lt;span style="color:blue;"&gt;string &lt;/span&gt;name)
{
    &lt;span style="color:blue;"&gt;return &lt;/span&gt;element.Elements(&lt;span style="color:#a31515;"&gt;&amp;quot;field&amp;quot;&lt;/span&gt;)
                  .Where(f =&amp;gt; f.Attribute(&lt;span style="color:#a31515;"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;).Value == name)
                  .First();
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;… and you can quickly turn the query around into something readable. &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;entries =
    &lt;span style="color:blue;"&gt;from &lt;/span&gt;r &lt;span style="color:blue;"&gt;in &lt;/span&gt;doc.Descendants(&lt;span style="color:#a31515;"&gt;&amp;quot;record&amp;quot;&lt;/span&gt;)
    &lt;span style="color:blue;"&gt;select new
    &lt;/span&gt;{
        Country = r.Field(&lt;span style="color:#a31515;"&gt;&amp;quot;Country&amp;quot;&lt;/span&gt;).Value,
        Year = r.Field(&lt;span style="color:#a31515;"&gt;&amp;quot;Year&amp;quot;&lt;/span&gt;).Value,
        Value = &lt;span style="color:blue;"&gt;double&lt;/span&gt;.Parse(r.Field(&lt;span style="color:#a31515;"&gt;&amp;quot;Value&amp;quot;&lt;/span&gt;).Value)
    };&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;If only SQL code was just as easy to break apart!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://www.pluralsight.com/community/aggbug.aspx?PostID=51648" width="1" height="1"&gt;</content><author><name>scott-allen</name><uri>http://www.pluralsight.com/community/members/scott_2D00_allen/default.aspx</uri></author></entry><entry><title>Restku</title><link rel="alternate" type="text/html" href="/community/blogs/scottallen/archive/2008/07/07/restku.aspx" /><id>/community/blogs/scottallen/archive/2008/07/07/restku.aspx</id><published>2008-07-08T02:12:00Z</published><updated>2008-07-08T02:12:00Z</updated><content type="html">&lt;p&gt;&lt;a href="http://oaks.nvg.org/hapo.html" target="_blank"&gt;Haiku&lt;/a&gt; is a popular poetic form that has evolved over centuries. &lt;b&gt;Restku &lt;/b&gt;is Haiku with a&amp;nbsp; twist.&lt;/p&gt;
&lt;blockquote&gt; 
  crystal pixels&lt;br /&gt;
  get brighter&lt;br /&gt;
  an abundance of excitement&lt;br /&gt;
&lt;/blockquote&gt;
&lt;p&gt;The twist is that the author of a Restku is restricted to using a single verb from this list: get, post, put, and delete. Although traditional Restku insists on present tense usage of the four verbs, adventurous&amp;nbsp; authors will mix in past tense, future tense, and on occasion, present perfect tense. &lt;/p&gt;
&lt;blockquote&gt; 
  unexpected dialog&lt;br /&gt;
  a &amp;ldquo;progress&amp;rdquo; bar&lt;br /&gt; 
  vista has posted the bad news&lt;br /&gt;
&lt;/blockquote&gt;
&lt;p&gt;Although Restku was inspired by REST, a software architecture style,&amp;nbsp; there is no reason an author can&amp;rsquo;t frame concepts from outside the world of information technology into a Restku. &lt;/p&gt;
&lt;blockquote&gt; 
  weathered glove&lt;br /&gt;
  humid skies&lt;br /&gt;
  put on a childhood dream
&lt;/blockquote&gt;
&lt;p&gt;Relax your mind with the mental stimulation of writing a Restku today, for tomorrow is still a mystery. &lt;/p&gt;
&lt;blockquote&gt; 
  four hundred and four&lt;br /&gt;
  electrical neurons&lt;br /&gt;
  delete her memory
&lt;/blockquote&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://www.pluralsight.com/community/aggbug.aspx?PostID=51520" width="1" height="1"&gt;</content><author><name>scott-allen</name><uri>http://www.pluralsight.com/community/members/scott_2D00_allen/default.aspx</uri></author></entry><entry><title>Swimming Upstream Is Hazardous</title><link rel="alternate" type="text/html" href="/community/blogs/scottallen/archive/2008/07/03/swimming-upstream-is-hazardous.aspx" /><id>/community/blogs/scottallen/archive/2008/07/03/swimming-upstream-is-hazardous.aspx</id><published>2008-07-03T05:56:09Z</published><updated>2008-07-03T05:56:09Z</updated><content type="html">&lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Salmon swim upstream, and look at what happens …&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;p align="center"&gt;&lt;a href="http://www.pluralsight.com/community/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/scottallen/salmon_5F00_2.jpg"&gt;&lt;img title="salmon" style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="216" alt="salmon" src="http://www.pluralsight.com/community/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/scottallen/salmon_5F00_thumb.jpg" width="244" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Every developer is familiar with the “&lt;em&gt;work around&lt;/em&gt;”. These are the extra bits of extra code we write to overcome limitations in an API, platform, or framework. &lt;/p&gt;  &lt;p&gt;But, sometimes those limitations are a feature. The designer of a framework might be guiding you in a specific direction. Take the Silverlight networking APIs as an example. The APIs provide only asynchronous communication options, yet I’ve seen a few people try to block on network operations with code like the following: &lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:#2b91af;"&gt;AutoResetEvent &lt;/span&gt;_event = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;AutoResetEvent&lt;/span&gt;(&lt;span style="color:blue;"&gt;false&lt;/span&gt;);
&lt;span style="color:#2b91af;"&gt;WebClient &lt;/span&gt;client = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;WebClient&lt;/span&gt;(); 
client.DownloadStringCompleted += 
    (s, ev) =&amp;gt; { _message.Text = ev.Result; _event.Set(); };     
client.DownloadStringAsync(&lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Uri&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;foo.xml&amp;quot;&lt;/span&gt;, &lt;span style="color:#2b91af;"&gt;UriKind&lt;/span&gt;.Relative));
_event.WaitOne();&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This code results in a deadlock, since the WebClient tries to raise the completed event on the main thread, but the main thread is blocked inside WaitOne and waiting for the completed event to fire. This deadlock is not only fatal to the Silverlight application, but can bring down the web browser, too. Even if this code didn&amp;#39;t create a deadlock, do you really want your application to block over a slow network connection?&lt;/p&gt;

&lt;p&gt;When you find yourself writing “work around” code, it’s worthwhile to review the situation. Are you really &lt;em&gt;working around&lt;/em&gt; a limitation? Or are you &lt;em&gt;working against&lt;/em&gt; the intended use of a framework? Working against the framework is rarely a good idea – there can be a lot of hungry bears waiting to catch you in the future. &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://www.pluralsight.com/community/aggbug.aspx?PostID=51452" width="1" height="1"&gt;</content><author><name>scott-allen</name><uri>http://www.pluralsight.com/community/members/scott_2D00_allen/default.aspx</uri></author></entry><entry><title>Fun with Fills in Silverlight 1.0</title><link rel="alternate" type="text/html" href="/community/blogs/scottallen/archive/2008/02/20/50284.aspx" /><id>/community/blogs/scottallen/archive/2008/02/20/50284.aspx</id><published>2008-02-21T03:33:00Z</published><updated>2008-02-21T03:33:00Z</updated><content type="html">&lt;p&gt;I've saw a few people trip over the Fill property of Silverlight objects in JavaScript last week. Let's use the following XAML snippet as an example:
&lt;/p&gt;
&lt;div style='font-family:monospace;font-size: 8pt;border:solid 1pt black; background-color: #d3d3bd;'&gt;
&lt;span style='color:Blue'&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#a31515'&gt;Canvas&lt;/span&gt;&lt;span style='color:Blue'&gt;&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&lt;/span&gt;&lt;span style='color:#a31515'&gt;Rectangle&lt;/span&gt;&lt;span style='color:Blue'&gt; &lt;/span&gt;&lt;span style='color:Red'&gt;x:Name&lt;/span&gt;&lt;span style='color:Blue'&gt; =&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt;_box1&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt; &lt;/span&gt;&lt;span style='color:Red'&gt;Canvas.Left&lt;/span&gt;&lt;span style='color:Blue'&gt;=&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt;10&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt; &lt;/span&gt;&lt;span style='color:Red'&gt;Canvas.Top&lt;/span&gt;&lt;span style='color:Blue'&gt;=&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt;10&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;br/&gt;
&lt;/span&gt;&lt;span style='color:Blue'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style='color:Red'&gt;Width&lt;/span&gt;&lt;span style='color:Blue'&gt;=&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt;100&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt; &lt;/span&gt;&lt;span style='color:Red'&gt;Height&lt;/span&gt;&lt;span style='color:Blue'&gt;=&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt;100&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt; &lt;/span&gt;&lt;span style='color:Red'&gt;Fill&lt;/span&gt;&lt;span style='color:Blue'&gt;=&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt;Red&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt; /&amp;gt;&lt;br/&gt;
&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&lt;/span&gt;&lt;span style='color:#a31515'&gt;Rectangle&lt;/span&gt;&lt;span style='color:Blue'&gt; &lt;/span&gt;&lt;span style='color:Red'&gt;x:Name&lt;/span&gt;&lt;span style='color:Blue'&gt; =&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt;_box2&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt; &lt;/span&gt;&lt;span style='color:Red'&gt;Canvas.Left&lt;/span&gt;&lt;span style='color:Blue'&gt;=&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt;120&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt; &lt;/span&gt;&lt;span style='color:Red'&gt;Canvas.Top&lt;/span&gt;&lt;span style='color:Blue'&gt;=&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt;10&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;br/&gt;
&lt;/span&gt;&lt;span style='color:Blue'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style='color:Red'&gt;Width&lt;/span&gt;&lt;span style='color:Blue'&gt;=&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt;100&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt; &lt;/span&gt;&lt;span style='color:Red'&gt;Height&lt;/span&gt;&lt;span style='color:Blue'&gt;=&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt;100&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt; &lt;/span&gt;&lt;span style='color:Red'&gt;Fill&lt;/span&gt;&lt;span style='color:Blue'&gt;=&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt;Red&lt;/span&gt;&lt;span style='color:black'&gt;&amp;quot;&lt;/span&gt;&lt;span style='color:Blue'&gt; /&amp;gt;&lt;br/&gt;
&amp;lt;/&lt;/span&gt;&lt;span style='color:#a31515'&gt;Canvas&lt;/span&gt;&lt;span style='color:Blue'&gt;&amp;gt;&lt;br/&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;Given the way the Fill attributes look (Fill="Red"), you'd be tempted to think the following code would work:
&lt;/p&gt;
&lt;div style='font-family:monospace;font-size: 8pt;border:solid 1pt black; background-color: #d3d3bd;'&gt;
&lt;span style='color:Blue'&gt;var&lt;/span&gt;&lt;span style='color:black'&gt; box1 = silverlightControl.content.findName(&lt;/span&gt;&lt;span style='color:#a31515'&gt;'_box1'&lt;/span&gt;&lt;span style='color:black'&gt;);&lt;br/&gt;
&lt;/span&gt;&lt;span style='color:Blue'&gt;var&lt;/span&gt;&lt;span style='color:black'&gt; box2 = silverlightControl.content.findName(&lt;/span&gt;&lt;span style='color:#a31515'&gt;'_box2'&lt;/span&gt;&lt;span style='color:black'&gt;);&lt;br/&gt;
&lt;br/&gt;
&lt;br/&gt;
box1.Fill = &lt;/span&gt;&lt;span style='color:#a31515'&gt;&amp;quot;Green&amp;quot;&lt;/span&gt;&lt;span style='color:black'&gt;;&lt;br/&gt;
box2.Fill = box1.Fill; &lt;/span&gt;&lt;span style='color:Green'&gt;// this will create an error&lt;br/&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;But, that last line of code will create a runtime error:
&lt;/p&gt;
&lt;p align="center"&gt;&lt;img src="http://pluralsight.com/scott.allen/images/200802/setvalueerror.jpg" alt="AG_E_RUNTIME_SETVALUE" /&gt;&lt;/p&gt;
&lt;p&gt;
	&lt;/p&gt;&lt;p&gt;The infamous AG_E_RUNTIME_SETVALUE error! The error says we are trying to put a square object into a round hole. What is the Fill property expecting? Aren't we just copying a simple string reference around? The debugger's &lt;a href="http://msdn2.microsoft.com/en-us/library/f177hahy(VS.80).aspx"&gt;immediate window&lt;/a&gt; can give us a few hints as to what is happening:
&lt;/p&gt;&lt;p align="center"&gt;&lt;img src="http://pluralsight.com/scott.allen/images/200802/immediate.jpg" alt="immediate window debugging" /&gt;
	&lt;/p&gt;&lt;p&gt;The first dump shows us that the Fill property doesn't reference a simple string. The second dump tells us we are actually dealing with a &lt;a href="http://msdn2.microsoft.com/en-us/library/bb979843.aspx"&gt;SolidColorBrush&lt;/a&gt; object. Hmmm – what happened to "Red"? Even more mysterious - the third dump (the brush's Color property) is a negative number. 
&lt;/p&gt;&lt;h2&gt;Magical Colors
&lt;/h2&gt;&lt;p&gt;The MSDN docs for &lt;a href="http://msdn2.microsoft.com/en-us/library/bb980062.aspx"&gt;Color&lt;/a&gt; describe some of the type conversion magic operating behind the scenes. Essentially, you can set a Color property (or the Fill property of a SolidColorBrush) using a well known name ("Green"), a hex string ('#FF00FF00'), or an ScRGB string ('sc#1,0,1,0'). Silverlight will take care of converting the incoming value into the proper internal representation. So, setting a Color property is easy. What's difficult is getting a meaningful value &lt;em&gt;out&lt;/em&gt; of the Color property.  Take note of this remark in the MSDN docs:
&lt;/p&gt;&lt;p&gt;&lt;em&gt;&lt;span style="font-family:Verdana; font-size:8pt"&gt;The runtime scripting value of an existing &lt;strong&gt;Color&lt;/strong&gt; is potentially stored differently on different browsers and platforms. You can compare color values obtained from scripting between existing instances, or set one &lt;strong&gt;Color&lt;/strong&gt; property with the value of another existing &lt;strong&gt;Color&lt;/strong&gt;, but directly comparing these values to named color strings, hex strings, or ScRGB strings that were used to specify the color initially is not supported.&lt;/span&gt;
		&lt;/em&gt;&lt;/p&gt;&lt;p&gt;This means we can avoid the runtime error with the following code …
&lt;/p&gt;
&lt;div style='font-family:monospace;font-size: 8pt;border:solid 1pt black; background-color: #d3d3bd;'&gt;
box1.Fill = &lt;span style='color:#a31515'&gt;'BlanchedAlmond'&lt;/span&gt;&lt;span style='color:black'&gt;;&lt;br/&gt;
box2.Fill.Color = box1.Fill.Color;&lt;br/&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;… but we need to be careful with code like the following. The makeHexColor function will compute the correct hex string for Color properties on IE7 (like '#ff00ff00' for a green color, which we saw yielded -65536 in the debugger), but according to the docs this code might break in FireFox or on a Mac, because the internal representation of colors might be different.
&lt;/p&gt;
&lt;div style='font-family:monospace;font-size: 8pt;border:solid 1pt black; background-color: #d3d3bd;'&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;box1.Fill = &lt;span style='color:#a31515'&gt;'Green'&lt;/span&gt;&lt;span style='color:black'&gt;;&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;box2.Fill.Color = makeHexColor(box1.Fill.Color);&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style='color:Green'&gt;// ...&lt;br/&gt;
&lt;/span&gt;&lt;span style='color:black'&gt;
&lt;br/&gt;
&lt;/span&gt;&lt;span style='color:Blue'&gt;function&lt;/span&gt;&lt;span style='color:black'&gt; makeHexColor(color)&lt;br/&gt;
{&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style='color:Blue'&gt;var&lt;/span&gt;&lt;span style='color:black'&gt; result = &lt;/span&gt;&lt;span style='color:#a31515'&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style='color:black'&gt;;&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style='color:Blue'&gt;for&lt;/span&gt;&lt;span style='color:black'&gt; (&lt;/span&gt;&lt;span style='color:Blue'&gt;var&lt;/span&gt;&lt;span style='color:black'&gt; i = 0; i &amp;lt; 8; i++) {&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;result = (color &amp;amp; 0xF).toString(16) + result;&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;color &amp;gt;&amp;gt;= 4;&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style='color:Blue'&gt;return&lt;/span&gt;&lt;span style='color:black'&gt; &lt;/span&gt;&lt;span style='color:#a31515'&gt;&amp;quot;#&amp;quot;&lt;/span&gt;&lt;span style='color:black'&gt; + result;&lt;br/&gt;
}&lt;br/&gt;
&lt;/span&gt;
&lt;/div&gt;
 &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://www.pluralsight.com/community/aggbug.aspx?PostID=50284" width="1" height="1"&gt;</content><author><name>scott-allen</name><uri>http://www.pluralsight.com/community/members/scott_2D00_allen/default.aspx</uri></author></entry><entry><title>Silverlight 1.1 Alpha – An Appetite for Exceptions</title><link rel="alternate" type="text/html" href="/community/blogs/scottallen/archive/2007/11/13/49054.aspx" /><id>/community/blogs/scottallen/archive/2007/11/13/49054.aspx</id><published>2007-11-13T16:18:00Z</published><updated>2007-11-13T16:18:00Z</updated><content type="html">&lt;p&gt;
The current Silverlight 1.1 alpha will eat exceptions.
I’m not sure if the final version will behave similarly, but if you are working with the alpha don’t let this behavior surprise you. 
&lt;/p&gt;&lt;p&gt;
For example, consider the following event handlers that listen to a shape’s mouse events:
&lt;/p&gt;
&lt;div style='font-family:monospace;font-size: 8pt;border:solid 1pt black; background-color: #d3d3bd;'&gt;
&lt;span style='color:Blue'&gt;void&lt;/span&gt;&lt;span style='color:black'&gt; _box_MouseLeftButtonDown(&lt;/span&gt;&lt;span style='color:Blue'&gt;object&lt;/span&gt;&lt;span style='color:black'&gt; sender, &lt;/span&gt;&lt;span style='color:#2b91af'&gt;MouseEventArgs&lt;/span&gt;&lt;span style='color:black'&gt; e)&lt;br/&gt;
{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style='color:Blue'&gt;throw&lt;/span&gt;&lt;span style='color:black'&gt; &lt;/span&gt;&lt;span style='color:Blue'&gt;new&lt;/span&gt;&lt;span style='color:black'&gt; &lt;/span&gt;&lt;span style='color:#2b91af'&gt;NotImplementedException&lt;/span&gt;&lt;span style='color:black'&gt;();&lt;br/&gt;
}&lt;br/&gt;
&lt;br/&gt;
&lt;/span&gt;&lt;span style='color:Blue'&gt;void&lt;/span&gt;&lt;span style='color:black'&gt; _box_MouseLeftButtonUp(&lt;/span&gt;&lt;span style='color:Blue'&gt;object&lt;/span&gt;&lt;span style='color:black'&gt; sender, &lt;/span&gt;&lt;span style='color:#2b91af'&gt;MouseEventArgs&lt;/span&gt;&lt;span style='color:black'&gt; e)&lt;br/&gt;
{&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_box.Width += 5;&lt;br/&gt;
}&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;
The left mouse button goes down and … nothing happens. There is no indication of an error. 
&lt;/p&gt;&lt;p&gt;
Later, when the left mouse button goes up … the shape will grow in size. Silverlight is still running with the attitude of a Broadway director. Despite the setback - the show must go on.
&lt;/p&gt;&lt;p&gt;
If you have some mysterious behavior and are not catching exceptions inside event handlers, a good start might be to go into the Visual Studio Exceptions dialog (Debug -&gt; Exceptions) and configure the debugger to break as soon as code throws an exception (the default setting is to only break on a user unhandled exception). This might help locate the problem. 
&lt;/p&gt;&lt;p&gt;
There is one area where an unhandled exception will stop the show. Consider the following user control:
&lt;/p&gt;
&lt;div style='font-family:monospace;font-size: 8pt;border:solid 1pt black; background-color: #d3d3bd;'&gt;
&lt;span style='color:Blue'&gt;public&lt;/span&gt;&lt;span style='color:black'&gt; &lt;/span&gt;&lt;span style='color:Blue'&gt;class&lt;/span&gt;&lt;span style='color:black'&gt; &lt;/span&gt;&lt;span style='color:#2b91af'&gt;ColorfulSlider&lt;/span&gt;&lt;span style='color:black'&gt; : &lt;/span&gt;&lt;span style='color:#2b91af'&gt;Control&lt;br/&gt;
&lt;/span&gt;&lt;span style='color:black'&gt;{&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style='color:Blue'&gt;public&lt;/span&gt;&lt;span style='color:black'&gt; ColorfulSlider()&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style='color:Green'&gt;// code ...&lt;br/&gt;
&lt;/span&gt;&lt;span style='color:black'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style='color:Blue'&gt;throw&lt;/span&gt;&lt;span style='color:black'&gt; &lt;/span&gt;&lt;span style='color:Blue'&gt;new&lt;/span&gt;&lt;span style='color:black'&gt; &lt;/span&gt;&lt;span style='color:#2b91af'&gt;InvalidOperationException&lt;/span&gt;&lt;span style='color:black'&gt;(&lt;/span&gt;&lt;span style='color:#a31515'&gt;&amp;quot;something went wrong...&amp;quot;&lt;/span&gt;&lt;span style='color:black'&gt;);&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br/&gt;
}&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;
If we place this faulty user control into a Page.xaml file that loads with the plugin – Silverlight will tell us there is a parser error. The error message tends to make one look inside the .xaml file for malformed XML. The real problem is that Silverlight can’t instantiate the object requested in XAML because the default constructor throws an exception. 
&lt;/p&gt;   &lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://www.pluralsight.com/community/aggbug.aspx?PostID=49054" width="1" height="1"&gt;</content><author><name>scott-allen</name><uri>http://www.pluralsight.com/community/members/scott_2D00_allen/default.aspx</uri></author></entry><entry><title>The New ASP.NET Framework</title><link rel="alternate" type="text/html" href="/community/blogs/scottallen/archive/2007/10/14/48769.aspx" /><id>/community/blogs/scottallen/archive/2007/10/14/48769.aspx</id><published>2007-10-14T23:19:00Z</published><updated>2007-10-14T23:19:00Z</updated><content type="html">&lt;p&gt;The New ASP.NET Framework
&lt;/p&gt;&lt;p&gt;ScottGu gave a demo of the new MVC framework for ASP.NET at the &lt;a href="http://www.altnetconf.com/"&gt;ALT.NET Conference&lt;/a&gt;. Here are some notes and thoughts I had after watching &lt;a href="http://www.hanselman.com/blog/ScottGuMVCPresentationAndScottHaScreencastFromALTNETConference.aspx"&gt;ScottHanselman's recording&lt;/a&gt;.
&lt;/p&gt;&lt;p&gt;The framework should go live in the spring of 2008. The framework is &lt;em&gt;not&lt;/em&gt; a replacement for ASP.NET WebForms, but provides an alternative paradigm for building web applications. The new framework still works inside of the ASP.NET runtime - meaning all of the wonderful infrastructure pieces like the configuration system, provider model, SQL cache invalidation, health monitoring, and master pages continue to exist. Like the ASP.NET AJAX extensions – it sounds like we'll only need a new assembly to start the party. 
&lt;/p&gt;&lt;p&gt;Being an "MVC framework", the software features the Model View Controller pattern. MVC and its many permutations have been (and continue to be) principal patterns in many application frameworks and development environments. There were several mentions of Rails (both the &lt;a href="http://www.castleproject.org/monorail/index.html"&gt;Mono&lt;/a&gt; and &lt;a href="http://www.rubyonrails.org/"&gt;Ruby&lt;/a&gt; types), and at least one reference to &lt;a href="http://www.djangobook.com/en/beta/"&gt;Django&lt;/a&gt; (the Django book is a good read to get into the framework's mindset). 
&lt;/p&gt;&lt;p&gt;The framework will provide:
&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Mechanisms to enforce a clean separation of concerns
&lt;/li&gt;&lt;li&gt;An API designed for testability
&lt;/li&gt;&lt;li&gt;A pluggable and extensible technology stack
&lt;/li&gt;&lt;/ol&gt;&lt;h1&gt;Separation of Concerns
&lt;/h1&gt;&lt;p&gt; ASP.NET WebForms map an incoming URL to a single .aspx file. Although one can use an &lt;a href="http://www.devx.com/dotnet/Article/33889/0/page/5"&gt;HttpModule&lt;/a&gt; or VirtualPathProvider to change this behavior, the majority of ASP.NET web applications use this default behavior. In practice, this approach tends to produce code-behind files with intermingled presentation, data access, and business logic. This approach also tightly couples URLs to the physical arrangement of .aspx files in the file system. 
&lt;/p&gt;&lt;p&gt;The new MVC framework offers a loose coupling between incoming URLs and the view that will ultimately render HTML. ScottGu describes a flexible and pluggable URL dispatching engine that can handle clean and &lt;a href="http://www.artima.com/weblogs/viewpost.jsp?thread=153170"&gt;procedural URLs&lt;/a&gt; like:
&lt;/p&gt;&lt;p&gt;http://OdeToCode.com/articles/show/450
&lt;/p&gt;&lt;p&gt;or
&lt;/p&gt;&lt;p&gt;http://www.pluralsight.com/classes/register/appliedsilverlight
&lt;/p&gt;&lt;p&gt;The URL dispatching engine examines incoming URLs, routes each request to an instance of a controller class, and invokes a method on the controller (the action). 
&lt;/p&gt;&lt;h2&gt;Controllers
&lt;/h2&gt;&lt;p&gt;Controllers in the new framework support &lt;a href="http://www.agiledata.org/essays/tdd.html"&gt;test first&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Presenter_First"&gt;controller first&lt;/a&gt; development styles. In other words, you can implement a controller sans any views, and fully test the controller's ability to utilize application logic and services to respond to a request. 
&lt;/p&gt;&lt;p&gt;The framework provides a low-level interface definition you can implement to claim absolute power over controller policy, but also provides a hierarchy of concrete controller classes to build upon. 
&lt;/p&gt;&lt;p&gt;When the controller is ready, it can call RenderView(string viewName, object viewData) to generate the appropriate user interface response. Thanks to generics, this boundary can also be strongly typed. 
&lt;/p&gt;&lt;h2&gt;Views
&lt;/h2&gt;&lt;p&gt;Views render HTML, but views are not web forms - there is no page lifecycle, postbacks, or viewstate in this framework. 
&lt;/p&gt;&lt;p&gt;In general, views are simple &lt;a href="http://en.wikipedia.org/wiki/Template_engine_%28web%29"&gt; templates&lt;/a&gt;.  Templates are concerned only with presentation. To enforce a separation of concerns, many template engines do not allow a template to change the value of a variable or call into application logic, but the ASP.NET framework will allow code blocks and data-binding. The view never references a controller, and the controller never references a view - so testability and a separation of concerns prevail. 
&lt;/p&gt;&lt;h1&gt;Designed For Testability
&lt;/h1&gt;&lt;p&gt;One of the current difficulties in writing unit tests for code running inside an ASP.NET environment is the pervasiveness of sealed classes like &lt;a href="http://haacked.com/archive/2005/06/11/simulating_httpcontext.aspx"&gt;HttpContext&lt;/a&gt;.The MVC framework features an interface based API with IHttpContext, IResponse, IRequest, etc. 
&lt;/p&gt;&lt;p&gt;There is a mock view engine and, I believe, mock implementations of IHttpContext and the like to make testing easier. 
&lt;/p&gt;&lt;p&gt;At the beginning of the discussion Scott mentioned that the framework will include &lt;a href="http://martinfowler.com/articles/injection.html"&gt;inversion of control containers&lt;/a&gt;, although I don't recall this feature appearing in the presentation.
&lt;/p&gt;&lt;h1&gt;A Pluggable Architecture
&lt;/h1&gt;&lt;p&gt;It sounds as if every major service in the MVC framework is pluggable – the view engine (throw in &lt;a href="http://wiki.castleproject.org/index.php/MonoRail:Brail"&gt;Brail&lt;/a&gt;),  the dependency injection framework (throw in &lt;a href="http://structuremap.sourceforge.net/Default.htm"&gt;Structure Map&lt;/a&gt;), the URL dispatcher, the controllers, and more. 
&lt;/p&gt;&lt;h1&gt;Look For the CTP Soon
&lt;/h1&gt;&lt;p&gt;The new framework appears to marry mature paradigms from outside the world of ASP.NET with the high performance and robust infrastructure of the ASP.NET runtime. Look for a CTP by the end of the year. &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://www.pluralsight.com/community/aggbug.aspx?PostID=48769" width="1" height="1"&gt;</content><author><name>scott-allen</name><uri>http://www.pluralsight.com/community/members/scott_2D00_allen/default.aspx</uri></author></entry><entry><title>Silverlight Subtleties</title><link rel="alternate" type="text/html" href="/community/blogs/scottallen/archive/2007/10/10/48732.aspx" /><id>/community/blogs/scottallen/archive/2007/10/10/48732.aspx</id><published>2007-10-10T15:18:00Z</published><updated>2007-10-10T15:18:00Z</updated><content type="html">&lt;p&gt;Moving from a Silverlight 1.0 JavaScript mindset to a Silverlight 1.1 C# mindset isn't terribly difficult, but there are little traps here and there. For example, to download and display an image, you need to create and configure a &lt;a href="http://msdn2.microsoft.com/en-us/library/bb232904.aspx"&gt;Downloader&lt;/a&gt; object.
&lt;/p&gt;&lt;p&gt;In JavaScript:
&lt;/p&gt;
&lt;div style='font-family:monospace;font-size: 8pt;border:solid 1pt black; background-color: #d3d3bd;'&gt;
&lt;span style='color:Blue'&gt;var&lt;/span&gt;&lt;span style='color:black'&gt; downloader = &lt;/span&gt;&lt;span style='color:Blue'&gt;this&lt;/span&gt;&lt;span style='color:black'&gt;.control.createObject(&lt;/span&gt;&lt;span style='color:#a31515'&gt;&amp;quot;downloader&amp;quot;&lt;/span&gt;&lt;span style='color:black'&gt;);&lt;br/&gt;
&lt;br/&gt;
downloader.addEventListener(&lt;/span&gt;&lt;span style='color:#a31515'&gt;&amp;quot;completed&amp;quot;&lt;/span&gt;&lt;span style='color:black'&gt;, &lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Silverlight.createDelegate(&lt;/span&gt;&lt;span style='color:Blue'&gt;this&lt;/span&gt;&lt;span style='color:black'&gt;, &lt;/span&gt;&lt;span style='color:Blue'&gt;this&lt;/span&gt;&lt;span style='color:black'&gt;._onDownloaderComplete));&lt;br/&gt;
&lt;br/&gt;
downloader.open(&lt;/span&gt;&lt;span style='color:#a31515'&gt;&amp;quot;GET&amp;quot;&lt;/span&gt;&lt;span style='color:black'&gt;, &lt;/span&gt;&lt;span style='color:#a31515'&gt;&amp;quot;YodaDog.jpg&amp;quot;&lt;/span&gt;&lt;span style='color:black'&gt;);&lt;br/&gt;
downloader.send();&lt;br/&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;In C#:
&lt;/p&gt;
&lt;div style='font-family:monospace;font-size: 8pt;border:solid 1pt black; background-color: #d3d3bd;'&gt;
Downloader download = &lt;span style='color:Blue'&gt;new&lt;/span&gt;&lt;span style='color:black'&gt; Downloader();&lt;br/&gt;
download.Completed += &lt;/span&gt;&lt;span style='color:Blue'&gt;new&lt;/span&gt;&lt;span style='color:black'&gt; &lt;/span&gt;&lt;span style='color:#2b91af'&gt;EventHandler&lt;/span&gt;&lt;span style='color:black'&gt;(download_Completed);&lt;br/&gt;
download.Open(&lt;/span&gt;&lt;span style='color:#a31515'&gt;&amp;quot;GET&amp;quot;&lt;/span&gt;&lt;span style='color:black'&gt;, &lt;/span&gt;&lt;span style='color:Blue'&gt;new&lt;/span&gt;&lt;span style='color:black'&gt; Uri(&lt;/span&gt;&lt;span style='color:#a31515'&gt;&amp;quot;YodaDog.jpg&amp;quot;&lt;/span&gt;&lt;span style='color:black'&gt;, UriKind.Relative));&lt;br/&gt;
download.Send();&lt;br/&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;The obvious differences are the wiring of events and construction of the Downloader object. However, the basic patterns in the code are true to their environments and easy to remember. It's the little things that bite you, for instance the Uri passed into the downloader. If the C# version looked like this:
&lt;/p&gt;
&lt;div style='font-family:monospace;font-size: 8pt;border:solid 1pt black; background-color: #d3d3bd;'&gt;
download.Open(&lt;span style='color:#a31515'&gt;&amp;quot;GET&amp;quot;&lt;/span&gt;&lt;span style='color:black'&gt;, &lt;/span&gt;&lt;span style='color:Blue'&gt;new&lt;/span&gt;&lt;span style='color:black'&gt; Uri(&lt;/span&gt;&lt;span style='color:#a31515'&gt;&amp;quot;YodaDog.jpg&amp;quot;&lt;/span&gt;&lt;span style='color:black'&gt;));&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;… then it still compiles and looks like its JavaScript counterpart, but unfortunately doesn't work. We have to explicitly tell the Uri constructor we are passing a relative URI. 
&lt;/p&gt;&lt;p&gt;The code for getting the downloaded image into an Image control is almost identical.
&lt;/p&gt;&lt;p&gt;JavaScript:
&lt;/p&gt;
&lt;div style='font-family:monospace;font-size: 8pt;border:solid 1pt black; background-color: #d3d3bd;'&gt;
_onDownloaderComplete: &lt;span style='color:Blue'&gt;function&lt;/span&gt;&lt;span style='color:black'&gt;(sender, eventArgs)&lt;br/&gt;
{&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style='color:Blue'&gt;this&lt;/span&gt;&lt;span style='color:black'&gt;._mainImage.SetSource(sender, &lt;/span&gt;&lt;span style='color:#a31515'&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style='color:black'&gt;);&lt;br/&gt;
}&lt;br/&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;C#:
&lt;/p&gt;
&lt;div style='font-family:monospace;font-size: 8pt;border:solid 1pt black; background-color: #d3d3bd;'&gt;
&lt;span style='color:Blue'&gt;void&lt;/span&gt;&lt;span style='color:black'&gt; download_Completed(&lt;/span&gt;&lt;span style='color:Blue'&gt;object&lt;/span&gt;&lt;span style='color:black'&gt; sender, &lt;/span&gt;&lt;span style='color:#2b91af'&gt;EventArgs&lt;/span&gt;&lt;span style='color:black'&gt; e)&lt;br/&gt;
{&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style='color:Blue'&gt;this&lt;/span&gt;&lt;span style='color:black'&gt;._mainImage.SetSource((Downloader)sender, &lt;/span&gt;&lt;span style='color:#a31515'&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style='color:black'&gt;);&lt;br/&gt;
}&lt;br/&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;Presto! Yoda dog appears:
&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.flickr.com/photos/71859211@N00/287825296/" title="yodadog"&gt;&lt;img src="http://www.pluralsight.com/scott.allen/images/200710/101007_1644_silverlight1.jpg" alt="Yoda Dog" border="0"/&gt;&lt;/a&gt;
	&lt;/p&gt;&lt;p&gt;But what if we want to take Yoda dog off the screen? The following JavaScript works:
&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;i&gt;Edit - Oops! The wrong code was pasted here, but is now fixed. &lt;/i&gt;&lt;/b&gt;&lt;/p&gt;

&lt;div style='font-family:monospace;font-size: 8pt;border:solid 1pt black; background-color: #d3d3bd;'&gt;
onMouseButtonUp: &lt;span style='color:Blue'&gt;function&lt;/span&gt;&lt;span style='color:black'&gt;(sender, eventArgs)&lt;br/&gt;
{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style='color:Blue'&gt;this&lt;/span&gt;&lt;span style='color:black'&gt;._mainImage.source = &lt;/span&gt;&lt;span style='color:Blue'&gt;null&lt;/span&gt;&lt;span style='color:black'&gt;;&lt;br/&gt;
},&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br/&gt;
&lt;/span&gt;
&lt;/div&gt;

&lt;p&gt;But the equivalent C# code will leave Yoda dog on the screen*.
&lt;/p&gt;
&lt;div style='font-family:monospace;font-size: 8pt;border:solid 1pt black; background-color: #d3d3bd;'&gt;
&lt;span style='color:Blue'&gt;void&lt;/span&gt;&lt;span style='color:black'&gt; Page_MouseLeftButtonDown(&lt;/span&gt;&lt;span style='color:Blue'&gt;object&lt;/span&gt;&lt;span style='color:black'&gt; sender, MouseEventArgs e)&lt;br/&gt;
{&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style='color:Blue'&gt;this&lt;/span&gt;&lt;span style='color:black'&gt;._mainImage.Source = &lt;/span&gt;&lt;span style='color:Blue'&gt;null&lt;/span&gt;&lt;span style='color:black'&gt;;&lt;br/&gt;
}&lt;br/&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;Instead, an empty URI is needed:
&lt;/p&gt;
&lt;div style='font-family:monospace;font-size: 8pt;border:solid 1pt black; background-color: #d3d3bd;'&gt;
&lt;span style='color:Blue'&gt;void&lt;/span&gt;&lt;span style='color:black'&gt; Page_MouseLeftButtonDown(&lt;/span&gt;&lt;span style='color:Blue'&gt;object&lt;/span&gt;&lt;span style='color:black'&gt; sender, MouseEventArgs e)&lt;br/&gt;
{&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style='color:Blue'&gt;this&lt;/span&gt;&lt;span style='color:black'&gt;._mainImage.Source = &lt;/span&gt;&lt;span style='color:Blue'&gt;new&lt;/span&gt;&lt;span style='color:black'&gt; Uri(&lt;/span&gt;&lt;span style='color:#a31515'&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style='color:black'&gt;, UriKind.Relative);&lt;br/&gt;
}&lt;br/&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;
    Of course, the XAML remains the same in both environments.
&lt;/p&gt;
    &lt;p&gt;
        * Just look at the expression on the dog's face. You know what she is thinking? She's thinking: "Tonight, my owner's favorite shoes become a shredded pile of wet leather…". 
    &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://www.pluralsight.com/community/aggbug.aspx?PostID=48732" width="1" height="1"&gt;</content><author><name>scott-allen</name><uri>http://www.pluralsight.com/community/members/scott_2D00_allen/default.aspx</uri></author></entry><entry><title>Hello, .NET Source Code</title><link rel="alternate" type="text/html" href="/community/blogs/scottallen/archive/2007/10/03/48656.aspx" /><id>/community/blogs/scottallen/archive/2007/10/03/48656.aspx</id><published>2007-10-03T16:43:00Z</published><updated>2007-10-03T16:43:00Z</updated><content type="html">&lt;P&gt;ScottGu &lt;A href="http://weblogs.asp.net/scottgu/archive/2007/10/03/releasing-the-source-code-for-the-net-framework-libraries.aspx"&gt;announced&lt;/A&gt; that Microsoft is releasing the source code for the .NET Libraries. The cheering from developers won&amp;#8217;t subside for some time. &lt;A href="http://www.aisto.com/roeder/dotnet/"&gt;Reflector&lt;/A&gt; is a great tool for peering into assemblies, but nothing beats breakpoints and an &lt;A href="http://msdn2.microsoft.com/en-us/library/f177hahy(VS.80).aspx"&gt;Immediate Window&lt;/A&gt; for seeing what is happening in managed code. &lt;/P&gt;
&lt;P&gt;Developers will now be able to reference the implementation of production quality components. Using Windows Workflow as an example, a developer can use the &lt;A href="http://www.odetocode.com/Articles/455.aspx"&gt;base activity library&lt;/A&gt; and&amp;nbsp;WF service implementations&amp;nbsp;as a guide when extending WF. &lt;/P&gt;
&lt;P&gt;Looking at the design time implementation of various components in the presentation frameworks will also, I think, be a great advantage. &lt;/P&gt;
&lt;P&gt;Is there a drawback? Sure - I can imagine some people wrapping themselves around a tree trying to find a bug in an underlying framework when they really need to be looking at their own code. I know this, because I&amp;#8217;ve been guilty of this behavior myself. :)&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://www.pluralsight.com/community/aggbug.aspx?PostID=48656" width="1" height="1"&gt;</content><author><name>scott-allen</name><uri>http://www.pluralsight.com/community/members/scott_2D00_allen/default.aspx</uri></author></entry></feed>