<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>illdata.com &#187; Home</title>
	<atom:link href="http://illdata.com/blog/category/home/feed/" rel="self" type="application/rss+xml" />
	<link>http://illdata.com/blog</link>
	<description>(Weblog)Duckworth.ToString()</description>
	<lastBuildDate>Wed, 26 Jan 2011 23:34:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Optimizing Dynamic 8-bit PNG’s in C# and ASP.NET MVC</title>
		<link>http://illdata.com/blog/2010/11/10/optimizing-dynamic-8-bit-png%e2%80%99s-in-c-and-asp-net-mvc/</link>
		<comments>http://illdata.com/blog/2010/11/10/optimizing-dynamic-8-bit-png%e2%80%99s-in-c-and-asp-net-mvc/#comments</comments>
		<pubDate>Wed, 10 Nov 2010 12:03:51 +0000</pubDate>
		<dc:creator>duckworth</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Home]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://illdata.com/blog/?p=113</guid>
		<description><![CDATA[Programmatically generating images to be served real-time over the web can be useful for many scenarios, such as CAPTCHA’s, watermarks, thumbnail generation, or complicated layouts and effects… Continue reading →]]></description>
			<content:encoded><![CDATA[<p>Programmatically generating images to be served real-time over the web can be useful for many scenarios, such as CAPTCHA’s, watermarks, thumbnail generation, or complicated layouts and effects… <a href="http://www.stackrunner.com/asp-net-mvc/optimizing-dynamic-8-bit-png%E2%80%99s-in-c-and-asp-net-mvc/">Continue reading →</a></p>
]]></content:encoded>
			<wfw:commentRss>http://illdata.com/blog/2010/11/10/optimizing-dynamic-8-bit-png%e2%80%99s-in-c-and-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Server Batch Inserts of Parent/Child Data with iBATIS</title>
		<link>http://illdata.com/blog/2010/01/13/sql-server-batch-inserts-of-parentchild-data-with-ibatis/</link>
		<comments>http://illdata.com/blog/2010/01/13/sql-server-batch-inserts-of-parentchild-data-with-ibatis/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 04:24:20 +0000</pubDate>
		<dc:creator>duckworth</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Home]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[iBATIS]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[SqlServer]]></category>

		<guid isPermaLink="false">http://illdata.com/blog/?p=81</guid>
		<description><![CDATA[The common pattern for persistence of our domain classes to the Database is through the use of an ORM layer with support for your standard CRUD operations. Many ORM's have mechanisms for optimizing Selects, N + 1 Selects, lazy loading etc., but often don't have mechanisms for batching insertions. Very often when optimizing database calls [...]]]></description>
			<content:encoded><![CDATA[<p>The common pattern for persistence of our domain classes to the Database is through the use of an <a href="http://en.wikipedia.org/wiki/Object-relational_mapping">ORM</a> layer with support for your standard CRUD operations. Many ORM's have mechanisms for optimizing Selects, N + 1 Selects, lazy loading etc., but often don't have mechanisms for batching insertions. Very often when optimizing database calls and eliminating unnecessary round-trips to the Database servers, I come across the scenario where I need to insert many Parent objects and their Child objects in a single batch. The Parent objects primary key is the foreign key for the Child objects so the Parent objects need to be persisted first, and you need to get the Identity of each Parent before inserting the Client. In SQL Server 2008 they introduced the <a href="http://technet.microsoft.com/en-us/library/bb510625.aspx">MERGE</a> statement which can be used for this purpose, however I needed this to work in SQL 2005 as well as 2008. Luckily, SQL Server 2005 introduced the <a href="http://technet.microsoft.com/en-us/library/ms177564.aspx">OUTPUT</a> clause, which, when combined with a second surrogate key, can be used to batch insert all the Parent Objects at once, OUTPUT all the Identity's of the Parent objects and then batch insert all the Child Objects.</p>
<p>In my scenario I am using <a href="http://ibatis.apache.org">iBATIS</a>, which is a lightweight and flexible ORM that provides some easy mechanisms for handling these edge cases. If you were using standard ADO.NET you could also leverage the new Table-Valued Parameters to pass in the sets of Parent and Child data.</p>
<p>For the example I will use two tables, Parent and Child:</p>
<pre class="tsql">&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> Parent<span style="color: #808080;">&#40;</span>
      Id <span style="color: #0000FF;">INT</span> NOT NULL <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span>,
      Name <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span> NOT NULL,
      SequenceNumber <span style="color: #0000FF;">INT</span> NOT NULL<span style="color: #808080;">&#41;</span>
&nbsp;
GO 
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> Child<span style="color: #808080;">&#40;</span>
      Id <span style="color: #0000FF;">INT</span> NOT NULL <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span>,
      ParentId <span style="color: #0000FF;">INT</span> NOT NULL <span style="color: #0000FF;">REFERENCES</span> Parent<span style="color: #808080;">&#40;</span>ParentID<span style="color: #808080;">&#41;</span>,
      Name VARCHAR255<span style="color: #808080;">&#41;</span> NOT NULL<span style="color: #808080;">&#41;</span>
GO
&nbsp;</pre>
<p>I have corresponding Parent and Child classes:</p>
<pre class="csharp">&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Parent
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Id <span style="color: #000000;">&#123;</span> get; set;<span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Name <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> IList&lt;Child&gt; Children <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Child
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Id <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Name <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;</pre>
<p>The SequenceNumber in the Parent table is just a surrogate key used to match the Parents in the batch back to the Children in that same batch, but I don't want it to pollute my Domain classes, so I will merely create a sequence number right before I send it to the iBATIS map using linq and anonymous types:</p>
<pre class="csharp">&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">int</span> InsertBatch<span style="color: #000000;">&#40;</span>IList&lt;Parent&gt; parentList<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    var sequencedParents = parentList.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>
        <span style="color: #000000;">&#40;</span>parent, index<span style="color: #000000;">&#41;</span> =&gt;
        <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a>
            <span style="color: #000000;">&#123;</span>
                SequenceNumber = index,
                Parent = parent,
                Children = parent.<span style="color: #0000FF;">Children</span>.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>
                child =&gt;
                <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a>
                    <span style="color: #000000;">&#123;</span>
                        SequenceNumber = index,
                        Child = child
                     <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    var sequencedChildren = sequencedParents.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>p =&gt; p.<span style="color: #0000FF;">Children</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    var args = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Hashtable
                       <span style="color: #000000;">&#123;</span>
                           <span style="color: #000000;">&#123;</span><span style="color: #808080;">&quot;Parents&quot;</span>, sequencedParents<span style="color: #000000;">&#125;</span>,
                           <span style="color: #000000;">&#123;</span><span style="color: #808080;">&quot;Children&quot;</span>, sequencedChildren<span style="color: #000000;">&#125;</span>
                       <span style="color: #000000;">&#125;</span>;
&nbsp;
    <span style="color: #0600FF;">return</span> Mapper.<span style="color: #0000FF;">Instance</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Update</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;InsertParentBatch&quot;</span>, args<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;</pre>
<p>The InsertBatch method is sending two separate lists to the iBATIS Mapped Statement, the list of Parents and the list of Children, which are associated by the sequence number we assigned which is unique for each Parent in this batch. We will use this to join them when we build the SQL in the following iBATIS map:</p>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;statement</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;InsertParentBatch&quot;</span> <span style="color: #000066;">parameterClass</span>=<span style="color: #ff0000;">&quot;map&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
	DECLARE @Inserted TABLE (
	  ID int,
	  SequenceNumber int
	)
&nbsp;
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;isNotEmpty</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;Parents&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
		INSERT INTO Parent
		(
			Name,
			SequenceNumber
		)
		OUTPUT INSERTED.Id, INSERTED.SequenceNumber INTO @Inserted
		SELECT Name, SequenceNumber FROM (
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;iterate</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;Parents&quot;</span> <span style="color: #000066;">conjunction</span>=<span style="color: #ff0000;">&quot; UNION ALL&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
		  SELECT
		  #Parents[].Parent.Name# Name,
		  #Parents[].SequenceNumber# SequenceNumber
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/iterate<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		) P
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/isNotEmpty<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;isNotEmpty</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;Children&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
		INSERT INTO Children (ParentId, Name)
		SELECT
			INS.Id,
			C.Name
		FROM
		(
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;iterate</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;Children&quot;</span> <span style="color: #000066;">conjunction</span>=<span style="color: #ff0000;">&quot; UNION &quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
		SELECT
		#Children[].Child.Name# Name,
		#Children[].SequenceNumber# SequenceNumber
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/iterate<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		) C
		INNER JOIN @Inserted INS ON
		INS.SequenceNumber = C.SequenceNumber
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/isNotEmpty<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/statement<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;</pre>
<p>The above map generates a single SQL Server statement that declares a SQL table variable to store the inserted Id's and the corresponding Sequence Numbers that the OUTPUT clause is returning back from our insert Parents statement. The iBATIS iterate property is simply iterating the list of Parents we sent to the map and doing an inline UNION ALL so we can insert them in one step. The insert into Children statement joins back to the @Inserted table variable on the Sequence number to get the Id for it's Parent.</p>
<p>Although the solution requires the addition of the SequenceNumber column which is only used for this purpose and un-related to our data model, it can be worth the performance increase if you are facing a situation where you have high volumes of data being submitted to your application in batches. By using the anonymous types in the persistence layer and keeping the SequenceNumber out of our domain model we can limit it to an edge case performance optimization that should be well encapsulated.</p>
]]></content:encoded>
			<wfw:commentRss>http://illdata.com/blog/2010/01/13/sql-server-batch-inserts-of-parentchild-data-with-ibatis/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Last.fm</title>
		<link>http://illdata.com/blog/2007/12/20/lastfm/</link>
		<comments>http://illdata.com/blog/2007/12/20/lastfm/#comments</comments>
		<pubDate>Fri, 21 Dec 2007 04:36:20 +0000</pubDate>
		<dc:creator>duckworth</dc:creator>
				<category><![CDATA[Home]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[radio]]></category>

		<guid isPermaLink="false">http://new.illdata.com/blog/2008/01/24/lastfm/</guid>
		<description><![CDATA[What I've been listening to lately...]]></description>
			<content:encoded><![CDATA[<p>What I've been listening to lately...</p>
<p><!-- table.lfmWidgetquilt_f942bfaa87dacb749f69d2f68e16e106 td {margin:0 !important;padding:0 !important;border:0 !important;}table.lfmWidgetquilt_f942bfaa87dacb749f69d2f68e16e106 tr.lfmHead a:hover {background:url(http://cdn.last.fm/widgets/images/en/header/quilt/album_horizontal_grey.png) no-repeat 0 0 !important;}table.lfmWidgetquilt_f942bfaa87dacb749f69d2f68e16e106 tr.lfmEmbed object {float:left;}table.lfmWidgetquilt_f942bfaa87dacb749f69d2f68e16e106 tr.lfmFoot td.lfmConfig a:hover {background:url(http://cdn.last.fm/widgets/images/en/footer/grey.png) no-repeat 0px 0 !important;;}table.lfmWidgetquilt_f942bfaa87dacb749f69d2f68e16e106 tr.lfmFoot td.lfmView a:hover {background:url(http://cdn.last.fm/widgets/images/en/footer/grey.png) no-repeat -85px 0 !important;}table.lfmWidgetquilt_f942bfaa87dacb749f69d2f68e16e106 tr.lfmFoot td.lfmPopup a:hover {background:url(http://cdn.last.fm/widgets/images/en/footer/grey.png) no-repeat -159px 0 !important;} --></p>
<table class="lfmWidgetquilt_f942bfaa87dacb749f69d2f68e16e106" style="width: 460px;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr class="lfmHead">
<td><a style="border: 0pt none; background: transparent url(http://cdn.last.fm/widgets/images/en/header/quilt/album_horizontal_grey.png) no-repeat scroll 0pt -20px; overflow: hidden; display: block; height: 20px; width: 460px; text-decoration: none;" title="Top albums" href="http://www.last.fm/user/duckworth/charts" target="_blank"></a></td>
</tr>
<tr class="lfmEmbed">
<td><object width="460" height="180" data="http://cdn.last.fm/widgets/quilt/13.swf" type="application/x-shockwave-flash"><param name="id" value="lfmEmbed_1196203296" /><param name="flashvars" value="type=user&amp;variable=duckworth&amp;file=topalbums&amp;bgColor=grey&amp;theme=grey&amp;lang=en&amp;widget_id=quilt_f942bfaa87dacb749f69d2f68e16e106" /><param name="allowScriptAccess" value="always" /><param name="allowNetworking" value="all" /><param name="allowFullScreen" value="true" /><param name="quality" value="high" /><param name="bgcolor" value="999999" /><param name="wmode" value="transparent" /><param name="menu" value="true" /><param name="src" value="http://cdn.last.fm/widgets/quilt/13.swf" /></object></td>
</tr>
<tr class="lfmFoot">
<td style="background:url(http://cdn.last.fm/widgets/images/footer_bg/grey.png) repeat-x 0 0;text-align:right;">
<table style="width: 460px;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="lfmConfig"><a style="border: 0pt none; background: transparent url(http://cdn.last.fm/widgets/images/en/footer/grey.png) no-repeat scroll 0px -20px; overflow: hidden; display: block; width: 85px; height: 20px; float: right; text-decoration: none;" title="Get your own widget" href="http://www.last.fm/widgets/?url=user%2Fduckworth%2Fpersonal&amp;colour=grey&amp;quiltType=album&amp;orient=horizontal&amp;height=medium&amp;from=code&amp;widget=quilt" target="_blank"></a></td>
<td class="lfmView" style="width: 74px;"><a style="border: 0pt none; background: transparent url(http://cdn.last.fm/widgets/images/en/footer/grey.png) no-repeat scroll -85px -20px; overflow: hidden; display: block; width: 74px; height: 20px; text-decoration: none;" title="View duckworth's profile" href="http://www.last.fm/user/duckworth" target="_blank"></a></td>
<td class="lfmPopup" style="width: 25px;"><a style="border: 0pt none; background: transparent url(http://cdn.last.fm/widgets/images/en/footer/grey.png) no-repeat scroll -159px -20px; overflow: hidden; display: block; width: 25px; height: 20px; text-decoration: none;" title="Load this quilt in a pop up" onclick="window.open(this.href + '&amp;resize=0','lfm_popup','height=280,width=510,resizable=yes,scrollbars=yes'); return false;" href="http://www.last.fm/widgets/popup/?url=user%2Fduckworth%2Fpersonal&amp;colour=grey&amp;quiltType=album&amp;orient=horizontal&amp;height=medium&amp;from=code&amp;widget=quilt&amp;resize=1" target="_blank"></a></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://illdata.com/blog/2007/12/20/lastfm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thats del.icio.us</title>
		<link>http://illdata.com/blog/2006/01/16/thats-delicious/</link>
		<comments>http://illdata.com/blog/2006/01/16/thats-delicious/#comments</comments>
		<pubDate>Tue, 17 Jan 2006 02:18:52 +0000</pubDate>
		<dc:creator>duckworth</dc:creator>
				<category><![CDATA[Home]]></category>

		<guid isPermaLink="false">http://new.illdata.com/2006/01/16/thats-delicious/</guid>
		<description><![CDATA[my del.icio.us]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript" src="http://del.icio.us/feeds/js/duckworth?title=my%20del.icio.us;bullet=%C2%BB;icon"></script><br />
<noscript><br />
<a href="http://del.icio.us/duckworth">my del.icio.us</a><br />
</noscript></p>
]]></content:encoded>
			<wfw:commentRss>http://illdata.com/blog/2006/01/16/thats-delicious/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

