<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
    <title>James Arendt</title>
    
    <link rel="hub" href="http://hubbub.api.typepad.com/" />
    <link rel="alternate" type="text/html" href="http://jamesarendt.typepad.com/jamesarendt/" />
    <id>tag:typepad.com,2003:weblog-107834</id>
    <updated>2008-03-27T19:40:03-04:00</updated>
    <subtitle>Explorations in .NET Development &amp; Mobile Computing</subtitle>
    <generator uri="http://www.typepad.com/">TypePad</generator>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/typepad/jamesarendt" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><entry>
        <title>C# null coalescing operator</title>
        <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typepad/jamesarendt/~3/5qIP2SfWAkw/c-null-coalesci.html" />
        <link rel="replies" type="text/html" href="http://jamesarendt.typepad.com/jamesarendt/2008/03/c-null-coalesci.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-47633436</id>
        <published>2008-03-27T19:40:03-04:00</published>
        <updated>2008-03-27T19:40:03-04:00</updated>
        <summary>The C# null coalescing operator, ??, is a simple, useful little operator that's been available since C# 2.0. When a value is null, the operator allows you to supply an alternate value. If the value is not null, it keeps...</summary>
        <author>
            <name>James Arendt</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="C#" />
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://jamesarendt.typepad.com/jamesarendt/">
<div xmlns="http://www.w3.org/1999/xhtml"><p>The C# null coalescing operator, <code>??</code>, is a simple, useful little operator that's been available since C# 2.0. When a value is null, the operator allows you to supply an alternate value. If the value is not null, it keeps the original value.</p>

<p>It's unfortunate that I keep forgetting about it. My old habits lead me to write code like:</p>

<div class="code"><pre>string middleInitial = person.MiddleInitial != null ? person.MiddleInitial : "";</pre></div>

<p>While using the <code>??</code> operator simplifies that all too common use of the ternary (<code>?:</code>) operator:</p>

<div class="code"><pre>string middleInitial = person.MiddleInitial ?? "";</pre></div>

<p>It was introduced to make working with nullable value types a lot easier. As shown above, it's not limited to nullable types. It can also be used with reference types.</p>

<p>Things get more interesting when you use it with the new C# language features like LINQ. Scott Guthrie has a write up that <a href="http://weblogs.asp.net/scottgu/archive/2007/09/20/the-new-c-null-coalescing-operator-and-using-it-with-linq.aspx">demonstrates using the ?? operator with LINQ</a>.</p>

<p>A comment on Oren Eini's <a href="http://www.ayende.com/Blog/archive/2008/03/27/ReSharper-is-smarter-than-me.aspx">recent blog</a> post brought it back to my attention.</p>

<p>Maybe this time I will not forget. :-)</p></div>
</content>


    <feedburner:origLink>http://jamesarendt.typepad.com/jamesarendt/2008/03/c-null-coalesci.html</feedburner:origLink></entry>
    <entry>
        <title>Observations on serializing LINQ expression trees</title>
        <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typepad/jamesarendt/~3/z3rf9un1Epc/observations-so.html" />
        <link rel="replies" type="text/html" href="http://jamesarendt.typepad.com/jamesarendt/2008/03/observations-so.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-46467330</id>
        <published>2008-03-03T01:10:13-05:00</published>
        <updated>2008-03-03T01:10:13-05:00</updated>
        <summary>I filled in the remaining expression types and greatly cleaned up the code for serializing LINQ expression trees. I think I got all the expression types correctly handled. Based on my current experiments, both simple and complex expression trees can...</summary>
        <author>
            <name>James Arendt</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term=".NET" />
        <category scheme="http://www.sixapart.com/ns/types#category" term="LINQ" />
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://jamesarendt.typepad.com/jamesarendt/">
<div xmlns="http://www.w3.org/1999/xhtml"><p>I filled in the remaining expression types and greatly cleaned up the code for serializing LINQ expression trees. I think I got all the expression types correctly handled. Based on my current experiments, both simple and complex expression trees can so far be serialized without issue using the <a href="http://msdn2.microsoft.com/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx">BinaryFormatter</a> .</p>

<p>Some observations:</p>

<ul><li>My default implementation created a matching serializable expression type for each LINQ expression type. This made the implementation straight forward. I also learned a great deal about the structure of the expression trees. Whether I leverage the serialization code heavily or not in the future, this knowledge alone was worth it. </li>

<li>Expression trees in serialized form are ridiculously too big. The culprit here is the default behavior of serialization with the BinaryFormatter. The BinaryFormatter writes out metadata describing all the types represented in the object graph. In this case, it has to provide the metadata for all of the expression types represented in the graph. The metadata for a simple 70-80 byte expression in C# source form comes out to be over 4KB in size. Yikes! </li>

<li>Mouth, meet foot. My confidence was unfounded. The <a href="http://msdn2.microsoft.com/library/system.runtime.serialization.datacontractserializer.aspx">DataContractSerializer</a> fails on the expression trees. The DataContractSerializer requires known types to be specified ahead of time. Unfortunately, some of the objects referenced by the expression trees rely on a type, System.RuntimeType. System.RuntimeType is marked internal and cannot be provided as part of the known types list.</li></ul></div>
</content>


    <feedburner:origLink>http://jamesarendt.typepad.com/jamesarendt/2008/03/observations-so.html</feedburner:origLink></entry>
    <entry>
        <title>Serializing LINQ expression trees</title>
        <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typepad/jamesarendt/~3/2WJ2AgZFCmA/serializing-lin.html" />
        <link rel="replies" type="text/html" href="http://jamesarendt.typepad.com/jamesarendt/2008/02/serializing-lin.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-46358510</id>
        <published>2008-02-29T19:00:00-05:00</published>
        <updated>2008-02-29T19:00:00-05:00</updated>
        <summary>I got simple LINQ expression trees to serialize last night. As I expected, the direct approach is to create a parallel set of expression types for each type in System.Linq.Expressions. With a set of serializable expression types, serialization is a...</summary>
        <author>
            <name>James Arendt</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term=".NET" />
        <category scheme="http://www.sixapart.com/ns/types#category" term="LINQ" />
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://jamesarendt.typepad.com/jamesarendt/">
<div xmlns="http://www.w3.org/1999/xhtml"><p>I got simple LINQ expression trees to serialize last night. As I expected, the direct approach is to create a parallel set of expression types for each type in <a href="http://msdn2.microsoft.com/library/system.linq.expressions.aspx">System.Linq.Expressions</a>. With a set of serializable expression types, serialization is a snap. Here's what the code needs to do:</p>

<p><strong>For Serialization</strong></p>

<ol><li>Perform partial evaluation on the LINQ expression tree. See <a href="http://blogs.msdn.com/mattwar/archive/2007/07/30/linq-building-an-iqueryable-provider-part-i.aspx">this article</a> by <a href="http://blogs.msdn.com/mattwar/default.aspx">Matt Warren</a> for code that supports partial evaluation. See <a href="http://jamesarendt.typepad.com/jamesarendt/2008/02/working-with-li.html">my previous post</a> for why this step is necessary.</li>

<li>Map each LINQ expression to a corresponding serializable expression. A visitor class greatly simplifies this step.</li>

<li>Serialize the expression tree. I tested with the <a href="http://msdn2.microsoft.com/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx">BinaryFormatter</a>, but I'm confident the <a href="http://msdn2.microsoft.com/library/system.runtime.serialization.datacontractserializer.aspx">DataContractSerializer</a> would work as well with its support for [Serializable] types.</li></ol>

<p><strong>For De-serialization</strong></p>

<ol><li>De-serialize the expression tree.</li>

<li>Map each serializable expression back to its original LINQ expression. Again, a visitor class is recommended for this step.</li>

<li>Use the expression tree.</li></ol>

<p>As of now, my code supports lambda, parameter, constant, methodcall, and a convert expressions. To support the rest, it should be just a matter of implementing the remaining expression types as the proof of concept worked.</p></div>
</content>


    <feedburner:origLink>http://jamesarendt.typepad.com/jamesarendt/2008/02/serializing-lin.html</feedburner:origLink></entry>
    <entry>
        <title>LINQ expression visualizer</title>
        <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typepad/jamesarendt/~3/T1xk2X2BWT4/linq-expression.html" />
        <link rel="replies" type="text/html" href="http://jamesarendt.typepad.com/jamesarendt/2008/02/linq-expression.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-46267378</id>
        <published>2008-02-27T18:05:25-05:00</published>
        <updated>2008-02-27T18:05:25-05:00</updated>
        <summary>Much like how Mole is useful as you learn to work with WPF, having a visualizer for LINQ expressions comes in handy as well. Visual Studio 2008 comes with a sample code for LINQ including code for a visualizer. Updated...</summary>
        <author>
            <name>James Arendt</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term=".NET" />
        <category scheme="http://www.sixapart.com/ns/types#category" term="C#" />
        <category scheme="http://www.sixapart.com/ns/types#category" term="LINQ" />
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://jamesarendt.typepad.com/jamesarendt/">
<div xmlns="http://www.w3.org/1999/xhtml"><p>Much like how <a href="http://karlshifflett.wordpress.com/mole-for-visual-studio/">Mole</a> is useful as you learn to work with WPF, having a visualizer for LINQ expressions comes in handy as well. Visual Studio 2008 comes with a sample code for LINQ including code for a visualizer.</p> <p>Updated versions of the sample code can be found here:</p> <p><a href="http://code.msdn.microsoft.com/csharpsamples">C# Code Samples</a></p></div>
</content>


    <feedburner:origLink>http://jamesarendt.typepad.com/jamesarendt/2008/02/linq-expression.html</feedburner:origLink></entry>
    <entry>
        <title>Working with LINQ expression trees</title>
        <link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typepad/jamesarendt/~3/1NAV1LuDXmY/working-with-li.html" />
        <link rel="replies" type="text/html" href="http://jamesarendt.typepad.com/jamesarendt/2008/02/working-with-li.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-46267136</id>
        <published>2008-02-27T18:00:22-05:00</published>
        <updated>2008-02-27T18:00:22-05:00</updated>
        <summary>For some code I'm working on, I've been teaching myself how to build and modify expression trees. I can say initially I found myself a little intimidated. Whether your code modifies elements within the tree, translates the expressions into another...</summary>
        <author>
            <name>James Arendt</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term=".NET" />
        <category scheme="http://www.sixapart.com/ns/types#category" term="LINQ" />
        
        
<content type="html" xml:lang="en-US" xml:base="http://jamesarendt.typepad.com/jamesarendt/">
&lt;div xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;p&gt;For some code I'm working on, I've been teaching myself how to build and modify expression trees. I can say initially I found myself a little intimidated.&lt;/p&gt; &lt;p&gt;Whether your code modifies elements within the tree, translates the expressions into another form, or simply visualizes the nodes, it will need to recursively visit each node in the tree. There's quite a variety of node types in &lt;a href="http://msdn2.microsoft.com/library/system.linq.expressions.aspx"&gt;System.Linq.Expressions&lt;/a&gt;. Writing code to support visiting all those node types just isn't my idea of fun.&lt;/p&gt; &lt;p&gt;If you consider for a moment that Microsoft's own libraries had to do just that to support LINQ to SQL, one would imagine that there must be a class in the framework that simplifies this. And there is! The class is ExpressionVisitor.&lt;/p&gt; &lt;p&gt;Of course, it's &lt;em&gt;conveniently&lt;/em&gt; marked internal and you can't use it. :-)&lt;/p&gt; &lt;p&gt;Fortunately, code for an ExpressionVisitor is available as part of a how-to article on MSDN:&lt;/p&gt; &lt;p&gt;&lt;a href="http://msdn2.microsoft.com/library/bb882521.aspx"&gt;How to: Implement an Expression Tree Visitor&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Expression trees are immutable so you cannot modify the expression nodes in place unlike some other common tree models. Much like how a string operation returns a copy of the string with the modifications in place, the same has to be done with expression trees. Expression tree visitors come to the rescue here to make it easy to visit all the nodes, return copies of the ones you want to keep, and build new nodes for the changes you want to make.&lt;/p&gt; &lt;p&gt;MSDN has a follow up how-to that covers how to do this:&lt;/p&gt; &lt;p&gt;&lt;a href="http://msdn2.microsoft.com/library/bb546136.aspx"&gt;How to: Modify Expression Trees&lt;/a&gt;&lt;/p&gt; &lt;p&gt;For the scenario I'm investigating, serializing expression trees, I realized that if I have any hope in getting it to work, I need to evaluate part of the tree to remove references to locally accessed variables (closures) in the lambdas. The reason for this is two-fold:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;C# and Visual Basic implement closures by generating non-serializable classes. Let's say I come up with a solution for creating a serializable set of expression nodes. I would still have the dilemma that these classes were not serializable even though the fields they contain are serializable. We don't have control over how the compiler generates the closures, so it would be best just to get them out of the picture.  &lt;li&gt;If the idea is to serialize the tree, for persistence or sending over a network, why serialize information that can be calculated once?&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;I worked through it in my head that to achieve this I would have to walk the tree and find which expression nodes depended on the fields in the closures. Better yet, find the nodes that are not dependent on the parameters passed into the expression. The idea is that if I find nodes that are not dependent on the parameters, I can evaluate them and replace them with constant expressions. This requires finding the node, wrapping the node in a lambda expression, compiling the lambda into a delegate, invoking the delegate, and finally creating a new constant expression with the value returned from the delegate invocation.&lt;/p&gt; &lt;p&gt;The real trick is finding at what level can you replace the nodes with a constant expression. Given enough time, I'm sure I could've figured out a slick way of doing this myself. But, again, I am not a glutton for punishment. LINQ to SQL has to take a similar course of action before it attempts to translate the LINQ expression into a SQL statement. There's got to be code out there that does some of this dirty work for me.&lt;/p&gt; &lt;p&gt;If you look deep enough into System.Data.Linq in &lt;a href="http://www.aisto.com/roeder/dotnet/"&gt;Reflector&lt;/a&gt;, you'll eventually find something called &lt;a href="http://www.google.com/search?hl=en&amp;amp;q=Funcletize"&gt;Funcletize&lt;/a&gt; that does this dirty work.&lt;/p&gt; &lt;p&gt;I don't feel comfortable taking code out of Reflector and plugging it into a solution. I have to really "get it" so I can be sure to customize it to my own needs. Finding this information was useful in that it did point me in the right direction. Most LINQ providers need to do this work. Although, I'm not writing a LINQ provider the problem space overlaps a great deal with what I'm trying to do. It was high time I got to reading some articles and blog postings I had bookmarked about implementing custom LINQ providers.&lt;/p&gt; &lt;p&gt;I found just what I was looking for in &lt;a href="http://blogs.msdn.com/mattwar/default.aspx"&gt;Matt Warren's&lt;/a&gt; &lt;a href="http://blogs.msdn.com/mattwar/archive/2007/07/30/linq-building-an-iqueryable-provider-part-i.aspx"&gt;series on building an IQueryable provider&lt;/a&gt;. Specifically, &lt;a href="http://blogs.msdn.com/mattwar/archive/2007/08/01/linq-building-an-iqueryable-provider-part-iii.aspx"&gt;part 3&lt;/a&gt; of his series he discusses performing partial evaluation to replace expression nodes with constant expressions. The write-up is good and the code even better!&lt;/p&gt; &lt;p&gt;With Matt's code, I can get the expression down to the bare minimum for serialization. The next step will be to transform the tree into a serializable form. I'm not sure if that's necessarily going to be as involved as these steps or if it'll just require a lot of busy work. I'm suspecting more the latter case as my current thinking suggests that I'll be building for all intents and purposes a hierarchy of types that are equivalent to the expression types in &lt;a href="http://msdn2.microsoft.com/library/system.linq.expressions.aspx"&gt;System.Linq.Expressions&lt;/a&gt;. The difference would be that mine are serializable.&lt;/p&gt;&lt;/div&gt;
</content>


    <feedburner:origLink>http://jamesarendt.typepad.com/jamesarendt/2008/02/working-with-li.html</feedburner:origLink></entry>
 
</feed><!-- ph=1 --><!-- nhm:dynamic-ssi -->
