<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Infovark Underground</title>
	
	<link>http://underground.infovark.com</link>
	<description />
	<pubDate>Wed, 19 Nov 2008 20:02:19 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/InfovarkUnderground" type="application/rss+xml" /><item>
		<title>The Curse of the Singleton</title>
		<link>http://feeds.feedburner.com/~r/InfovarkUnderground/~3/458605816/</link>
		<comments>http://underground.infovark.com/2008/11/19/the-curse-of-the-singleton/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 17:16:59 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[Infovark]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Testing]]></category>

		<category><![CDATA[performance]]></category>

		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=161</guid>
		<description><![CDATA[It took us six weeks to break the curse of the singleton. Six weeks! By the end of it, we&#8217;d rewritten most of our data access layer.
We began the process of removing singletons innocently enough. I thought I was well prepared for the task. I&#8217;d just finished reading The Pragmatic Programmer (my review of The [...]]]></description>
			<content:encoded><![CDATA[<p>It took us six weeks to break the curse of the singleton. Six weeks! By the end of it, we&#8217;d rewritten most of our data access layer.</p>
<p>We began the process of removing singletons innocently enough. I thought I was well prepared for the task. I&#8217;d just finished reading <a href="http://www.pragprog.com/titles/tpp/the-pragmatic-programmer">The Pragmatic Programmer</a> (my <a href="http://underground.infovark.com/2008/11/05/review-the-pragmatic-programmer/">review of The Pragmatic Programmer</a>) and <a href="http://www.amazon.com/Working-Effectively-Legacy-Robert-Martin/dp/0131177052">Working Effectively with Legacy Code</a> (my <a href="http://underground.infovark.com/2008/11/07/review-working-effectively-with-legacy-code/">review of Legacy Code</a>). I remember telling Gordon I&#8217;d tackle the problem over the weekend&#8230;</p>
<h4>What&#8217;s a singleton?</h4>
<p>The <a href="http://en.wikipedia.org/wiki/Singleton_pattern">Singleton Design Pattern</a> is one of the first patterns introduced in many software design books. But don&#8217;t let this fool you like it did me. Its prominent position has nothing to do with its importance. The Singleton is usually listed first because it&#8217;s the easiest pattern to explain and implement. It made a convenient place for the author to start, but the Singleton&#8217;s real uses are very limited. </p>
<p>Which is appropriate, actually, since the real use of the singleton is to limit usage. A class that implements the Singleton pattern allows only one object to be instantiated at a time. There are a few cases where this is desirable. For example, classes that control access to a single hardware device or that set up global variables. But the danger of the Singleton is that there are many cases where you&#8217;ll want to <em>misuse</em> it.</p>
<h4>Why are they bad?</h4>
<p>Scott Densmore lists the four key characteristics of the Singleton and how each can get you into trouble in his <a href="http://blogs.msdn.com/scottdensmore/archive/2004/05/25/140827.aspx">Why Singletons are Evil</a> blog post. </p>
<p>For another cautionary tale of the cycle attraction, infatuation, disappointment, and rejection, read <a href="http://www.codingwithoutcomments.com/2008/10/08/singleton-i-love-you-but-youre-bringing-me-down/">Singleton, I love you, but you&#8217;re bringing me down</a>.</p>
<p>In our case, we&#8217;d gleefully implemented Singletons for database access, content indexing, security and access control, and in a few other places where we thought we needed just one instance. If Steve Yegge were here, he&#8217;d call what we&#8217;d done an instance of the <em>Simpleton</em> pattern &#8212; a failure to grasp basic principles of object-oriented programming. You can read more about <a href="http://steve.yegge.googlepages.com/singleton-considered-stupid">Yegge&#8217;s thoughts on the singleton and design patterns for dummies</a>.</p>
<p>Our automated tests were running slowly because we had to set up and tear down the database for every test. Making a change to one component would frequently cause several tests to fail. Everything was tied together at the hip &#8212; at the Singleton classes &#8212; and it was impossible to disentangle our code to test particular items in isolation. We had tests, but not <em>unit</em> tests. They were integration tests, and the points of integration were the handful of singleton classes we&#8217;d built.</p>
<p>Worse, our database performance was lousy. Since we had a global variable for our database object, we could sprinkle database access code throughout the rest of our object model. We discovered that we were opening and closing database connections all the time. And we&#8217;d had to implement tricky locking code to guarantee that our SQL statements would get executed in the right order. </p>
<h4>What did we do about them?</h4>
<p>The Singleton let us be lazy about our programming habits. It allowed us to make assumptions we shouldn&#8217;t have. You can call it <a href="http://en.wikipedia.org/wiki/Premature_optimization#When_to_optimize">premature optimization</a> or a retreat into <a href="http://en.wikipedia.org/wiki/Procedural_programming">procedural programming</a> techniques from an earlier era. Ultimately, we&#8217;d found that it allowed us to cut too many corners. </p>
<p>So we slowly rooted out each Singleton class from our API and reimplemented the functionality in other ways. Fortunately, we had a large battery of integration tests to help guide us. And luckily, we&#8217;d decided to tackle the problem during our first Alpha test, when we could still afford to make sweeping changes. But correcting bad design takes much longer than avoiding it in the first place &#8212; even if you&#8217;ve read all the right books.</p>
<p>Six weeks later, we finally sorted out the mess we&#8217;d made for ourselves. There&#8217;s a handful of odds and ends left to do, but the design feels better. My gut tells me it&#8217;s an improvement, and our tests &#8212; now we have both unit and integration tests &#8212; show that we&#8217;ve almost tripled the speed of the data access layer. </p>
<p>It was worth our time to break the Curse of the Singleton. Beware lest ye, too, fall under its spell!</p>
<img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/458605816" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2008/11/19/the-curse-of-the-singleton/feed/</wfw:commentRss>
		<feedburner:origLink>http://underground.infovark.com/2008/11/19/the-curse-of-the-singleton/</feedburner:origLink></item>
		<item>
		<title>Review: Working Effectively with Legacy Code</title>
		<link>http://feeds.feedburner.com/~r/InfovarkUnderground/~3/445696354/</link>
		<comments>http://underground.infovark.com/2008/11/07/review-working-effectively-with-legacy-code/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 17:29:00 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
		
		<category><![CDATA[Books]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Testing]]></category>

		<category><![CDATA[Review]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=147</guid>
		<description><![CDATA[I know what you&#8217;re thinking: &#8220;Infovark&#8217;s been around for barely a year! Surely you guys aren&#8217;t having to deal with legacy code already?&#8221; If you accept Michael Feather&#8217;s expansive definition of legacy code &#8212; code without unit tests &#8212; then yes, despite our best efforts, we have lots of legacy code.
But even if you don&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://underground.infovark.com/wp-content/uploads/2008/11/working-effectively-with-legacy-code.jpg"><img class="alignleft size-medium wp-image-176" title="Working Effectively With Legacy Code" src="http://underground.infovark.com/wp-content/uploads/2008/11/working-effectively-with-legacy-code-300x300.jpg" alt="" width="300" height="300" /></a>I know what you&#8217;re thinking: &#8220;Infovark&#8217;s been around for barely a year! Surely you guys aren&#8217;t having to deal with legacy code <em>already?</em>&#8221; If you accept Michael Feather&#8217;s expansive definition of legacy code &#8212; code without unit tests &#8212; then yes, despite our best efforts, we have lots of legacy code.</p>
<p>But even if you don&#8217;t buy that definition, and even if you&#8217;re working on a completely <a href="http://en.wikipedia.org/wiki/Greenfield_project">greenfield</a> application, chances are you&#8217;ll have a lot of code in your project that isn&#8217;t fully understood. Or perhaps isn&#8217;t fully understood <em>by all members of the team</em>. And it&#8217;s in dealing with this issue that <a href="http://www.amazon.com/Working-Effectively-Legacy-Robert-Martin/dp/0131177052">Working Effectively with Legacy Code</a> really shines.</p>
<p>What happens when you need to change code that isn&#8217;t fully understood? Are you making it better or worse? The author says that you can&#8217;t know the answer to this question without tests in place. Having documentation is nice, but unit testing provides measurable output.</p>
<h4>The brass tacks</h4>
<p>Unlike many programming books, this one is organized in a Q&amp;A format. Once past the introduction &#8212; which you can skip if you already understand the importance of refactoring and test-driven development &#8212; you&#8217;ll find the chapters organized by topic. Here&#8217;s a sample of a few chapter headings:</p>
<ul>
<li>It Takes Forever to Make a Change</li>
<li>I Can&#8217;t Run This Method in a Test Harness</li>
<li>Dependencies on Libraries are Killing Me</li>
<li>I Don&#8217;t Understand the Code Well Enough to Change It</li>
<li>This Class is Too Big and I Don&#8217;t Want It to Get Any Bigger</li>
</ul>
<p>This is a great way to organize a highly technical book. Each chapter has a specific purpose. The author then spends the chapter discussing the ways you can get out of the jam and weighing the pros and cons of each.</p>
<p>You&#8217;ll find in-depth examples of each of the techniques used, but be prepared to shift between languages. To get the most out of the book, you&#8217;ll need to be comfortable scanning unfamiliar syntax.</p>
<p>As a bonus, the book contains an index of common refactoring patterns. Certain patterns make appearances in more than one chapter, and the index provides another place for the author to work through some real-world examples.</p>
<p>All in all, this is a practical field manual for a set of problems that occur too often out in the wild. I highly recommend it.</p>
<img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/445696354" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2008/11/07/review-working-effectively-with-legacy-code/feed/</wfw:commentRss>
		<feedburner:origLink>http://underground.infovark.com/2008/11/07/review-working-effectively-with-legacy-code/</feedburner:origLink></item>
		<item>
		<title>Review: The Pragmatic Programmer</title>
		<link>http://feeds.feedburner.com/~r/InfovarkUnderground/~3/443350191/</link>
		<comments>http://underground.infovark.com/2008/11/05/review-the-pragmatic-programmer/#comments</comments>
		<pubDate>Wed, 05 Nov 2008 15:46:54 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
		
		<category><![CDATA[Books]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Review]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=145</guid>
		<description><![CDATA[You&#8217;ll find The Pragmatic Programmer on many software developers&#8217; must-read books lists. After reading it from cover to cover, I&#8217;ve added it to my essential reading list as well. 
It&#8217;s not a book for beginners, though. The subtitle of the book, &#8220;From Journeyman to Master&#8221; sums it up. The Pragmatic Programmer describes the skills, attributes, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://underground.infovark.com/wp-content/uploads/2008/11/tpp.jpg"><img src="http://underground.infovark.com/wp-content/uploads/2008/11/tpp.jpg" alt="" title="The Pragmatic Programmer" width="181" height="228" class="alignleft size-full wp-image-169" /></a>You&#8217;ll find <a href="http://www.pragprog.com/titles/tpp/the-pragmatic-programmer">The Pragmatic Programmer</a> on many software developers&#8217; must-read books lists. After reading it from cover to cover, I&#8217;ve added it to my essential reading list as well. </p>
<p>It&#8217;s not a book for beginners, though. The subtitle of the book, &#8220;From Journeyman to Master&#8221; sums it up. The Pragmatic Programmer describes the skills, attributes, and attitudes that a mid-level programmer needs to become a professional developer. </p>
<p>Its purpose is to distill the wisdom gathered from a career in programming into about 70 tips. Each of these tips is explained and illustrated with examples that most programmers will find familiar. </p>
<p>The tips are not necessarily about writing code. The authors, Andrew Hunt and David Thomas, take a holistic approach to the craft of programming. They cover topics like communicating effectively, planning and scheduling, and building teams.</p>
<p>I&#8217;d read somewhere that you can judge the quality of a craftsman by the quality of his tools. The Pragmatic Programmer is a book I&#8217;d expect to see on any professional developer&#8217;s shelf.</p>
<img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/443350191" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2008/11/05/review-the-pragmatic-programmer/feed/</wfw:commentRss>
		<feedburner:origLink>http://underground.infovark.com/2008/11/05/review-the-pragmatic-programmer/</feedburner:origLink></item>
		<item>
		<title>Write Big to Write Small</title>
		<link>http://feeds.feedburner.com/~r/InfovarkUnderground/~3/425987876/</link>
		<comments>http://underground.infovark.com/2008/10/20/write-big-to-write-small/#comments</comments>
		<pubDate>Mon, 20 Oct 2008 02:40:12 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=143</guid>
		<description><![CDATA[Writing code is like writing literature. Sometimes you have to write big to write small. 
OK, if you&#8217;re a rock star, you might be able to think big and write small, but I can&#8217;t manage that feat. I often need a day of hunting down nasty copy-and-paste bugs before I realize, hey, all this stuff [...]]]></description>
			<content:encoded><![CDATA[<p>Writing code is like writing literature. Sometimes you have to write big to write small. </p>
<p>OK, if you&#8217;re a rock star, you might be able to think big and write small, but I can&#8217;t manage that feat. I often need a day of hunting down nasty copy-and-paste bugs before I realize, hey, all this stuff is redundant. I can write a function that makes this duplication unnecessary. </p>
<p>I can&#8217;t count the number of times I&#8217;ve tried to write concise, elegant code from the start, only to find that I&#8217;ve been writing small by thinking small. Getting lost in the details is a great way to write optimized code that works like a charm &#8212; but doesn&#8217;t actually do anything of value.</p>
<p>Once I thought that <a href="http://en.wikipedia.org/wiki/Design_pattern_(computer_science)">design patterns</a> might be the answer to my programming struggles. Then I discovered that my accuracy rate in picking the right pattern for any given programming problem was really poor. More often, it&#8217;s led me to implement overly complex solutions for simple problems. </p>
<p>For a long time, I thought it might be my lack of a computer science degree. But I haven&#8217;t found a programming style or development framework that fixes the problem. Maybe there just isn&#8217;t an efficient way for me to get my thoughts into the computer. The only way I know to do it is to write a big, sloppy mess at the start. Then I&#8217;ll slowly, carefully, edit it down to its bare essentials.   </p>
<p>Mozart might be able to write a symphony in a single sitting without penning a stray note. Not me.</p>
<img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/425987876" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2008/10/20/write-big-to-write-small/feed/</wfw:commentRss>
		<feedburner:origLink>http://underground.infovark.com/2008/10/20/write-big-to-write-small/</feedburner:origLink></item>
		<item>
		<title>Switch By Type in C#</title>
		<link>http://feeds.feedburner.com/~r/InfovarkUnderground/~3/416215910/</link>
		<comments>http://underground.infovark.com/2008/10/09/switch-by-type-in-c/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 23:08:13 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=100</guid>
		<description><![CDATA[The introduction of generics into C# 2.0 simplified many programming tasks. It especially helped in the creation of type-safe collections.
During our reworking of the Infovark data access layer, we created several generic methods to return items from our database. This allowed us to eliminate many duplicated methods and eliminate a lot of type casting. For [...]]]></description>
			<content:encoded><![CDATA[<p>The introduction of generics into C# 2.0 simplified many programming tasks. It especially helped in the creation of type-safe collections.</p>
<p>During our reworking of the Infovark data access layer, we created several generic methods to return items from our database. This allowed us to eliminate many duplicated methods and eliminate a lot of type casting. For example, before we began using generics, we had methods with signatures like this:</p>
<div class="geshi no csharp">
<ol>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">public</span> Entity GetEntity<span class="br0">&#40;</span><span class="kw4">long</span> id, <span class="kw4">int</span> revision<span class="br0">&#41;</span> <span class="br0">&#123;</span> &#8230; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">public</span> Relationship GetRelationship<span class="br0">&#40;</span><span class="kw4">long</span> id, <span class="kw4">int</span> revision<span class="br0">&#41;</span> <span class="br0">&#123;</span> &#8230; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">public</span> Resource GetResource<span class="br0">&#40;</span><span class="kw4">long</span> id, <span class="kw4">int</span> revision<span class="br0">&#41;</span> <span class="br0">&#123;</span> &#8230; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>became this after our refactoring: </p>
<div class="geshi no csharp">
<ol>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">public</span> T GetObject<span class="sy0">&lt;</span>T<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="kw4">long</span> id, <span class="kw4">int</span> revision<span class="br0">&#41;</span> where T <span class="sy0">:</span> MetaInstance</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; Type type <span class="sy0">=</span> <span class="kw3">typeof</span><span class="br0">&#40;</span>T<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>type <span class="sy0">==</span> <span class="kw3">typeof</span><span class="br0">&#40;</span>Entity<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw1">return</span> _DatabaseHandler.<span class="me1">GetEntity</span><span class="br0">&#40;</span>id, revision<span class="br0">&#41;</span> <span class="kw1">as</span> T;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>type <span class="sy0">==</span> <span class="kw3">typeof</span><span class="br0">&#40;</span>Relationship<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw1">return</span> _DatabaseHandler.<span class="me1">GetRelationship</span><span class="br0">&#40;</span>id, revision<span class="br0">&#41;</span> <span class="kw1">as</span> T;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>type <span class="sy0">==</span> <span class="kw3">typeof</span><span class="br0">&#40;</span>Resource<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw1">return</span> _DatabaseHandler.<span class="me1">GetResource</span><span class="br0">&#40;</span>id, revision<span class="br0">&#41;</span> <span class="kw1">as</span> T;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Wait a minute. All we&#8217;re doing here is wrapping all our unique methods up in a generic method! Why bother? Good question. We abandoned that approach in favor of something more sensible later on.</p>
<p>That&#8217;s actually not the point of this post. The point is that C# doesn&#8217;t have the ability to perform a <code>switch</code> on types. See all those <code>if</code> statements in our generic method? That&#8217;s how we worked around the lack of type switching. If anyone has a better approach, we&#8217;d like to hear it.</p>
<p>Peter Hallam&#8217;s WebLog provides more information about <a href="http://blogs.msdn.com/peterhal/archive/2005/07/05/435760.aspx">why C# doesn&#8217;t have the ability to switch on a type</a>. I&#8217;m not convinced by the reasons outlined there. As one commenter noted, there&#8217;s already a type-switching construct in C#: the <code>catch</code> statement.</p>
<img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/416215910" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2008/10/09/switch-by-type-in-c/feed/</wfw:commentRss>
		<feedburner:origLink>http://underground.infovark.com/2008/10/09/switch-by-type-in-c/</feedburner:origLink></item>
		<item>
		<title>One-Way Serialization</title>
		<link>http://feeds.feedburner.com/~r/InfovarkUnderground/~3/411966650/</link>
		<comments>http://underground.infovark.com/2008/10/05/one-way-serialization/#comments</comments>
		<pubDate>Sun, 05 Oct 2008 15:31:13 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
		
		<category><![CDATA[New]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=110</guid>
		<description><![CDATA[Has anyone found a good pattern for serialization without deserialization?
In my previous post about mixing serialization, I mentioned that we&#8217;d implemented the IXmlSerializable interface in many of our classes. The main reason for doing this was to include elements and attributes that were read-only. 
In other words, we wanted additional information to appear in the [...]]]></description>
			<content:encoded><![CDATA[<p>Has anyone found a good pattern for serialization <em>without</em> deserialization?</p>
<p>In my previous post about <a href="http://underground.infovark.com/2008/09/17/dont-mix-your-serialization/">mixing serialization</a>, I mentioned that we&#8217;d implemented the <code>IXmlSerializable</code> interface in many of our classes. The main reason for doing this was to include elements and attributes that were read-only. </p>
<p>In other words, we wanted additional information to appear in the serialized stream. This additional information is not needed in order to rehydrate the object.</p>
<h4>Why would you want to do that?</h4>
<p>Most serialization frameworks don&#8217;t handle this case. After all, if the point of serialization is to transport an object across the wire, cluttering up the stream with extra information is wasteful. Besides, you can&#8217;t dictate what someone on the other side might do with the data. Nicholas Allen discusses this point in his post explaining why <a href="http://blogs.msdn.com/drnick/archive/2008/09/10/read-only-data-members.aspx">read-only data members are not supported in WCF</a>. </p>
<p>While I understand his point from an object remoting perspective, the reason for choosing XML as a data format in the first place was to allow for human readability. What if you want to embed metadata in your XML that doesn&#8217;t affect the behavior of the object? You&#8217;d need to include it in the XML output, but you aren&#8217;t interested reading it back in from an XML stream. What&#8217;s the best way to achieve this?</p>
<p>For example, in our data layer we have a Version object that brings together information about when the object was created and by whom, when it was modified and by whom, as well as the object&#8217;s revision number. It&#8217;s useful to have this information in resulting XML string, but we don&#8217;t need to read it back in.</p>
<p>So here&#8217;s my question: Are we doing this wrong? Does the need for one-way serialization indicate a problem with our object model? Is it a code smell, or is it simply an edge case not considered by existing frameworks?</p>
<img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/411966650" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2008/10/05/one-way-serialization/feed/</wfw:commentRss>
		<feedburner:origLink>http://underground.infovark.com/2008/10/05/one-way-serialization/</feedburner:origLink></item>
		<item>
		<title>Don’t Mix Your Serialization</title>
		<link>http://feeds.feedburner.com/~r/InfovarkUnderground/~3/395575430/</link>
		<comments>http://underground.infovark.com/2008/09/17/dont-mix-your-serialization/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 21:33:38 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[AJAX]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[XML]]></category>

		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=112</guid>
		<description><![CDATA[Who doesn&#8217;t like mixing their Raisin Flakes with their Oaty-O&#8217;s in the morning? Yum! But it&#8217;s not a good idea if you&#8217;re talking about serial formats in C# 3.5 instead of breakfast cereals. You&#8217;ll get output that might leave a bad taste in your mouth.
Breakfast Quiz
Question: You&#8217;re writing a web API for an application. To [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://underground.infovark.com/wp-content/uploads/2008/09/breakfast_time.jpg"><img src="http://underground.infovark.com/wp-content/uploads/2008/09/breakfast_time.jpg" alt="" title="breakfast_time" width="300" height="224" class="alignright size-full wp-image-125" /></a>Who doesn&#8217;t like mixing their Raisin Flakes with their Oaty-O&#8217;s in the morning? Yum! But it&#8217;s not a good idea if you&#8217;re talking about <em>serial</em> formats in C# 3.5 instead of breakfast cereals. You&#8217;ll get output that might leave a bad taste in your mouth.</p>
<h4>Breakfast Quiz</h4>
<p>Question: You&#8217;re writing a web API for an application. To give developers the most flexibility in interacting with your system, you want to expose classes that can be serialized to either XML or JSON. Using WCF and C# 3.5 SP1, what are your options?</p>
<p>Answer: There&#8217;s only one option unless you rely on 3rd party serialization libraries. You must mark the class with the <code>[DataContract]</code> attribute and mark each serializable member with <code>[DataMember]</code>. This allows you to serialize and deserialize using the <a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx">DataContractSerializer</a> and <a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx">DataContractJsonSerializer</a> for XML and JSON respectively.</p>
<p>I mention this because we&#8217;d gone to great lengths to customize our XML using the <a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx">IXmlSerializable interface</a>. This gave us fine control over the properties we wanted to appear in our XML output and how they were formatted. But if you use the <code>IXmlSerializable</code> interface, you can&#8217;t also annotate the class with the <code>[DataContract]</code> attribute. You&#8217;ll get a compiler error. <a href="http://blogs.msdn.com/sowmy/archive/2006/05/14/597476.aspx">Sowmy Srinivasan explains this serialization restriction</a>.</p>
<p>I know what you&#8217;re thinking: If the framework provides an <code>IXmlSerializable</code> interface, isn&#8217;t there also an <code>IJsonSerializable</code> interface? Sadly, no. There&#8217;s no way to fine-tune the JSON output. Sigh.</p>
<p>So, if you&#8217;re currently using <code>IXmlSerializable</code>, you can forget about the <code>DataContractJsonSerializer</code>. Or you can accept that you&#8217;re <a href="http://underground.infovark.com/2008/05/06/fighting-the-framework/">fighting the framework</a>, forget about your fancy-pants XML format, and accept the default serialization, keeping these <a href="http://blogs.msdn.com/drnick/archive/2008/02/22/datamember-best-practices.aspx">data member best practices</a> in mind.</p>
<h4>What did we choose?</h4>
<p>Infovark has too much invested in our XML layout at this point. We&#8217;ve built our XSD files, XSL Transforms, and many, many unit tests. So we gave up on the <code>DataContractJsonSerializer</code> and turned to the excellent <a href="http://www.codeplex.com/Json">JSON.NET</a>, written by <a href="http://james.newtonking.com">James Newton-King</a>. It&#8217;s now version 3.0 and fully supports the new LINQ constructs.</p>
<p>It&#8217;s a little more work, but we think it&#8217;s worth it.</p>
<img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/395575430" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2008/09/17/dont-mix-your-serialization/feed/</wfw:commentRss>
		<feedburner:origLink>http://underground.infovark.com/2008/09/17/dont-mix-your-serialization/</feedburner:origLink></item>
		<item>
		<title>Converting IEnumerable to a Comma-Delimited String</title>
		<link>http://feeds.feedburner.com/~r/InfovarkUnderground/~3/381586766/</link>
		<comments>http://underground.infovark.com/2008/09/02/converting-ienumerable-to-a-comma-delimited-string/#comments</comments>
		<pubDate>Tue, 02 Sep 2008 18:24:55 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[collection]]></category>

		<category><![CDATA[generic]]></category>

		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=107</guid>
		<description><![CDATA[I&#8217;m not sure whether it&#8217;s the fastest way to convert an enumerable collection of longs or ints to a comma-delimited list in C#, but it might be the shortest.



IEnumerable&#60;long&#62; ids = new long&#91;&#93;&#123;1,3,4,5&#125;;


string delimitedIds = string.Join&#40;&#34;,&#34;, ids.Select&#40;x =&#62; x.ToString&#40;&#41;&#41;.ToArray&#40;&#41;&#41;;



If you need a LINQ-free version for backward compatibility, check out Missing Functions on IEnumerable on Steve [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not sure whether it&#8217;s the <em>fastest</em> way to convert an enumerable collection of <code>long</code>s or <code>int</code>s to a comma-delimited list in C#, but it might be the <em>shortest</em>.</p>
<div class="geshi no csharp">
<ol>
<li class="li1">
<div class="de1">IEnumerable<span class="sy0">&lt;</span><span class="kw4">long</span><span class="sy0">&gt;</span> ids <span class="sy0">=</span> <span class="kw3">new</span> <span class="kw4">long</span><span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#123;</span><span class="nu0">1</span>,<span class="nu0">3</span>,<span class="nu0">4</span>,<span class="nu0">5</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">string</span> delimitedIds <span class="sy0">=</span> <span class="kw4">string</span>.<span class="me1">Join</span><span class="br0">&#40;</span><span class="st0">&quot;,&quot;</span>, ids.<span class="me1">Select</span><span class="br0">&#40;</span>x <span class="sy0">=&gt;</span> x.<span class="me1">ToString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>.<span class="me1">ToArray</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>If you need a LINQ-free version for backward compatibility, check out <a href="http://www.stevecooper.org/2008/01/03/c-coding-missing-functions-on-ienumerable/">Missing Functions on IEnumerable</a> on Steve Cooper&#8217;s blog.</p>
<img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/381586766" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2008/09/02/converting-ienumerable-to-a-comma-delimited-string/feed/</wfw:commentRss>
		<feedburner:origLink>http://underground.infovark.com/2008/09/02/converting-ienumerable-to-a-comma-delimited-string/</feedburner:origLink></item>
		<item>
		<title>Tools: Beyond Compare 3</title>
		<link>http://feeds.feedburner.com/~r/InfovarkUnderground/~3/376360510/</link>
		<comments>http://underground.infovark.com/2008/08/27/tools-beyond-compare-3/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 17:37:08 +0000</pubDate>
		<dc:creator>Dean</dc:creator>
		
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=28</guid>
		<description><![CDATA[With my roots in maintenance programming, I&#8217;ve used a wide assortment of programming tools. Maintaining legacy code is not much fun. Anything that makes it easier gets added to my personal collection of productivity applications. I make a point of learning them inside and out.
I carry the tools with me from project to project. I [...]]]></description>
			<content:encoded><![CDATA[<p>With <a href="http://www.infovark.com/2007/12/14/confessions-of-a-maintenance-programmer/">my roots in maintenance programming</a>, I&#8217;ve used a wide assortment of programming tools. Maintaining legacy code is not much fun. Anything that makes it easier gets added to my personal collection of productivity applications. I make a point of learning them inside and out.</p>
<p>I carry the tools with me from project to project. I recommend them to coworkers. I&#8217;ll use them even if my employer provides alternate tools for free. I&#8217;ll complain loudly if the company I work for won&#8217;t allow me to use them &#8212; for policy reasons, consistency reasons, or security reasons. (Most of my bosses eventually concede the point. After all, they&#8217;re interfering with my ability to <strong>get things done</strong>, which is presumably why they hired me.)</p>
<p><a href="http://www.scootersoftware.com/">Beyond Compare by Scooter Software</a> is one of those tools. It&#8217;s the best file comparison tool I&#8217;ve ever used. And not just me - It&#8217;s also <a href="http://www.codinghorror.com/blog/archives/000454.html">earned praise</a> from other programmers.</p>
<p>When I heard that version 3 was soon to be released, I was both excited and a bit nervous. What new features would they add? Were they going to break any behavior I relied on?</p>
<p>The website lists the <a href="http://www.scootersoftware.com/moreinfo.php?zz=newfeatures">new features</a>, but the best new feature is the polished user interface, which makes it easier to move blocks of text and make inline edits to files.</p>
<div id="attachment_104" class="wp-caption aligncenter" style="width: 310px"><a href="http://underground.infovark.com/wp-content/uploads/2008/08/beyondcompare3.png"><img class="size-medium wp-image-104" title="beyondcompare3" src="http://underground.infovark.com/wp-content/uploads/2008/08/beyondcompare3-300x156.png" alt="File Comparison in Beyond Compare 3" width="300" height="156" /></a><p class="wp-caption-text">File Comparison in Beyond Compare 3</p></div>
<p>And no, they didn&#8217;t mess up anything in the process.</p>
<p>I played with the beta for several weeks, and recently bought the full version. If you&#8217;re not using it already, you should try it.</p>
<img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/376360510" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2008/08/27/tools-beyond-compare-3/feed/</wfw:commentRss>
		<feedburner:origLink>http://underground.infovark.com/2008/08/27/tools-beyond-compare-3/</feedburner:origLink></item>
		<item>
		<title>Handling COM Error Codes</title>
		<link>http://feeds.feedburner.com/~r/InfovarkUnderground/~3/370269990/</link>
		<comments>http://underground.infovark.com/2008/08/20/handling-com-error-codes/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 20:06:49 +0000</pubDate>
		<dc:creator>Gordon</dc:creator>
		
		<category><![CDATA[New]]></category>

		<guid isPermaLink="false">http://underground.infovark.com/?p=84</guid>
		<description><![CDATA[Sometimes COM Objects return a HRESULT that is outside of the bounds of a C#  integer.
Unfortunately, the only way to correctly handle a COM Exception in .NET is to use the ComException Class. But if the HRESULT isn&#8217;t an Int, and the ErrorCode Property on the ComException class is an Int, how are you supposed [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes COM Objects return a HRESULT that is outside of the bounds of a C#  integer.</p>
<p>Unfortunately, the only way to correctly handle a COM Exception in .NET is to use the <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.comexception_members(VS.71).aspx">ComException Class. </a>But if the HRESULT isn&#8217;t an Int, and the ErrorCode Property on the ComException class <em>is </em>an Int, how are you supposed to ever be able to catch that specific HRESULT?</p>
<p>Fear not! Here&#8217;s how to handle a COM ErrorCode that can&#8217;t be converted to an Int (in this example, it&#8217;s unsigned)</p>
<div class="geshi no csharp">
<div class="head">try</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; _PropertyHandler.<span class="me1">Open</span><span class="br0">&#40;</span>filePath, <span class="kw1">false</span>, dsoFileOpenOptions.<span class="me1">dsoOptionDefault</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw1">catch</span> <span class="br0">&#40;</span>COMException comEx<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>comEx.<span class="me1">ErrorCode</span> <span class="sy0">==</span> <span class="kw3">unchecked</span><span class="br0">&#40;</span><span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span>0&#215;800300FC<span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">throw</span> <span class="kw3">new</span> FileNotFoundException<span class="br0">&#40;</span><span class="st0">&quot;Could not find file:&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>The magic happens in the &#8216;unchecked&#8217; statement - which tells the compiler not to perform the overflow-checking context for integral-type arithmetic operations and conversions.</p>
<p>More on the Unchecked Operator over on <a href="http://msdn.microsoft.com/en-us/library/a569z7k8(VS.71).aspx">MSDN.</a></p>
<img src="http://feeds.feedburner.com/~r/InfovarkUnderground/~4/370269990" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://underground.infovark.com/2008/08/20/handling-com-error-codes/feed/</wfw:commentRss>
		<feedburner:origLink>http://underground.infovark.com/2008/08/20/handling-com-error-codes/</feedburner:origLink></item>
	</channel>
</rss>
