<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>TJ Solutions - ADO.Net</title>
    <link>http://www.tjsolutions.nl/</link>
    <description />
    <language>en-us</language>
    <copyright>Tijmen van de Kamp, Tom de Koning</copyright>
    <lastBuildDate>Thu, 12 Nov 2009 19:14:15 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>blog@tjsolutions.nl</managingEditor>
    <webMaster>blog@tjsolutions.nl</webMaster>
    <item>
      <trackback:ping>http://www.tjsolutions.nl/Trackback.aspx?guid=eba99e4a-f608-48e7-8541-397cb4c9bcf1</trackback:ping>
      <pingback:server>http://www.tjsolutions.nl/pingback.aspx</pingback:server>
      <pingback:target>http://www.tjsolutions.nl/PermaLink,guid,eba99e4a-f608-48e7-8541-397cb4c9bcf1.aspx</pingback:target>
      <dc:creator>Tom</dc:creator>
      <wfw:comment>http://www.tjsolutions.nl/CommentView,guid,eba99e4a-f608-48e7-8541-397cb4c9bcf1.aspx</wfw:comment>
      <wfw:commentRss>http://www.tjsolutions.nl/SyndicationService.asmx/GetEntryCommentsRss?guid=eba99e4a-f608-48e7-8541-397cb4c9bcf1</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
One of my colleagues asked me today if I could write a utility function that would
make his life more easy.
</p>
        <p>
The objective was to call a stored procedure n-times for every item in a list. Of
course some of the properties on every item had to be mapped to parameters in the
stored procedure.
</p>
        <p>
 
</p>
        <p>
The desired syntax would be something like:
</p>
        <p>
builder.SetStoredProcedureNameTo( " some sproc name ") 
<br />
        .Execute ( customerList , 
<br />
            (s,t) =&gt; s.AddParameterWIthValue(
parameterName, t.PROPERTY_NAME_1) , 
<br />
            (s,t) =&gt; s.AddParameterWIthValue(
parameterName, t.PROPERTY_NAME_2) , 
<br />
             (s,t) =&gt;
s.AddParameterWIthValue( parameterName, t.PROPERTY_NAME_3) 
<br />
      );
</p>
        <p>
 
</p>
        <p>
I already have a fluent wrapper class to make our ADO.Net a bit more friendly, the
interface looks like:
</p>
        <pre class="code">
          <span style="color: blue">public interface </span>
          <span style="color: #2b91af">ICustomSqlCommandBuilder </span>{ <span style="color: #2b91af">ICustomSqlCommandBuilder </span>SetStoredProcedureNameTo(<span style="color: blue">string </span>storedProcedureName); <span style="color: #2b91af">ICustomSqlCommandBuilder </span>AddParameterWithValue(<span style="color: blue">string </span>parameterNameInDatabase, <span style="color: blue">object </span>value); <span style="color: #2b91af">ICustomSqlCommandBuilder </span>AddExlicitOutputIntParameterToTheCommandFor(<span style="color: blue">string </span>parameterName); <span style="color: #2b91af">ICustomSqlCommandBuilder </span>AddExlicitOutputDateTimeParameterToTheCommandFor(<span style="color: blue">string </span>parameterName); <span style="color: #2b91af">IList</span>&lt;TReturnType&gt;
ExecuteReaderFor&lt;TReturnType&gt;() <span style="color: blue">where </span>TReturnType
: <span style="color: blue">new</span>(); }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Nothing special except for the ExecuteReaderFor&lt;TReturnType&gt;() but that's for
another post. 
</p>
        <p>
 
</p>
        <p>
Having the fluent interface on ICustomSqlCommandBuilder combined with the lambda magic
explained in <a href="http://www.tjsolutions.nl/2009/10/31/FunctionalProgrammingForEveryday.aspx">this</a> post
I came up with the following signature:
</p>
        <pre class="code">
          <span style="color: #2b91af">ICustomSqlCommandBuilder </span>ExecuteScalarForThisList&lt;T&gt;( <span style="color: #2b91af">IList</span>&lt;T&gt;
list, <span style="color: blue">params </span><span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">ICustomSqlCommandBuilder</span>,
T&gt;[] actionList);</pre>
        <a href="http://11011.net/software/vspaste">
        </a>The general idea is to apply <pre class="code">(s, t) =&gt; s.AddParameterWithValue(<span style="color: #a31515">"parameterName"</span>,
t.PROPERTY_NAME)</pre><p>
to every property in the object&lt;T&gt; that needs to be mapped against a parameter
in the stored procedure. Having the params allows us to map as many properties to
parameters in the stored procedure as we like. If we then loop through all the items
in the list first and apply all actions in the actionList per item, we can then call
ExecuteScalar on the stored procedure.
</p><pre class="code"><span style="color: blue">foreach </span>(<span style="color: blue">var </span>item <span style="color: blue">in </span>list)
{ <span style="color: blue">foreach </span>(<span style="color: blue">var </span>action <span style="color: blue">in </span>actionList)
{ action.Invoke(<span style="color: blue">this</span>, item); } <span style="color: blue">var </span>result
= sqlCommand.ExecuteScalar(); sqlCommand.Parameters.Clear(); }</pre><p>
Now, another requirement was to wrap it in a transaction and also to have the verification
of the result a bit more flexible. All in all not too complicated, just a matter of
introducing another Action:
</p><pre class="code"><span style="color: blue">public </span><span style="color: #2b91af">ICustomSqlCommandBuilder </span>ExecuteScalarForThisList&lt;T&gt;( <span style="color: #2b91af">IList</span>&lt;T&gt;
list, <span style="color: #2b91af">Action</span>&lt;<span style="color: blue">object</span>&gt;
verify, <span style="color: blue">params </span><span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">ICustomSqlCommandBuilder</span>,
T&gt;[] actionList) { sqlCommand.Connection.Open(); <span style="color: #2b91af">SqlTransaction </span>transaction
= sqlCommand.Connection.BeginTransaction(); sqlCommand.Transaction = transaction; <span style="color: blue">try </span>{ <span style="color: blue">foreach </span>(<span style="color: blue">var </span>item <span style="color: blue">in </span>list)
{ <span style="color: blue">foreach </span>(<span style="color: blue">var </span>action <span style="color: blue">in </span>actionList)
{ action.Invoke(<span style="color: blue">this</span>, item); } <span style="color: blue">var </span>result
= sqlCommand.ExecuteScalar(); verify(result); sqlCommand.Parameters.Clear(); } transaction.Commit();
} <span style="color: blue">catch </span>(<span style="color: #2b91af">SqlException</span>)
{ transaction.Rollback(); <span style="color: blue">throw</span>; } <span style="color: blue">catch </span>(<span style="color: #2b91af">Exception</span>)
{ transaction.Rollback(); <span style="color: blue">throw</span>; } <span style="color: blue">return
this</span>; }</pre><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a><p>
 
</p><p>
Including a bit of exceptionally brittle exception handling and calling the stored
procedure on a list of Customers now resolves to:
</p><pre class="code"><span style="color: #2b91af">Action</span>&lt;<span style="color: blue">object</span>&gt;
resultConstraint = result =&gt; { <span style="color: blue">if </span>(result != <span style="color: blue">null</span>)
{ <span style="color: blue">if </span>((<span style="color: blue">int</span>)result
&gt; 1000) { <span style="color: blue">throw new </span><span style="color: #2b91af">Exception</span>(<span style="color: #a31515">"result
should be less than 1000!"</span>); } } }; builder .SetStoredProcedureNameTo(<span style="color: #a31515">"sens_sp_modifyCustomerIncome"</span>)
.ExecuteScalarForThisList( customerList, resultConstraint, (s, t) =&gt; s.AddParameterWithValue(<span style="color: #a31515">"id"</span>,
t.ID), (s, t) =&gt; s.AddParameterWithValue(<span style="color: #a31515">"income"</span>,
t.Income) <span style="color: green">//etc for the rest of the properties </span>);</pre><a href="http://11011.net/software/vspaste"></a>I really, really like this syntax! <img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=eba99e4a-f608-48e7-8541-397cb4c9bcf1" /></body>
      <title>Having fun with functional programming and legacy ADO.Net!</title>
      <guid isPermaLink="false">http://www.tjsolutions.nl/PermaLink,guid,eba99e4a-f608-48e7-8541-397cb4c9bcf1.aspx</guid>
      <link>http://www.tjsolutions.nl/2009/11/12/HavingFunWithFunctionalProgrammingAndLegacyADONet.aspx</link>
      <pubDate>Thu, 12 Nov 2009 19:14:15 GMT</pubDate>
      <description>&lt;p&gt;
One of my colleagues asked me today if I could write a utility function that would
make his life more easy.
&lt;/p&gt;
&lt;p&gt;
The objective was to call a stored procedure n-times for every item in a list. Of
course some of the properties on every item had to be mapped to parameters in the
stored procedure.
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
The desired syntax would be something like:
&lt;/p&gt;
&lt;p&gt;
builder.SetStoredProcedureNameTo( " some sproc name ") 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Execute ( customerList , 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (s,t) =&amp;gt; s.AddParameterWIthValue(
parameterName, t.PROPERTY_NAME_1) , 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (s,t) =&amp;gt; s.AddParameterWIthValue(
parameterName, t.PROPERTY_NAME_2) , 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (s,t) =&amp;gt;
s.AddParameterWIthValue( parameterName, t.PROPERTY_NAME_3) 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; );
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
I already have a fluent wrapper class to make our ADO.Net a bit more friendly, the
interface looks like:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ICustomSqlCommandBuilder &lt;/span&gt;{ &lt;span style="color: #2b91af"&gt;ICustomSqlCommandBuilder &lt;/span&gt;SetStoredProcedureNameTo(&lt;span style="color: blue"&gt;string &lt;/span&gt;storedProcedureName); &lt;span style="color: #2b91af"&gt;ICustomSqlCommandBuilder &lt;/span&gt;AddParameterWithValue(&lt;span style="color: blue"&gt;string &lt;/span&gt;parameterNameInDatabase, &lt;span style="color: blue"&gt;object &lt;/span&gt;value); &lt;span style="color: #2b91af"&gt;ICustomSqlCommandBuilder &lt;/span&gt;AddExlicitOutputIntParameterToTheCommandFor(&lt;span style="color: blue"&gt;string &lt;/span&gt;parameterName); &lt;span style="color: #2b91af"&gt;ICustomSqlCommandBuilder &lt;/span&gt;AddExlicitOutputDateTimeParameterToTheCommandFor(&lt;span style="color: blue"&gt;string &lt;/span&gt;parameterName); &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;TReturnType&amp;gt;
ExecuteReaderFor&amp;lt;TReturnType&amp;gt;() &lt;span style="color: blue"&gt;where &lt;/span&gt;TReturnType
: &lt;span style="color: blue"&gt;new&lt;/span&gt;(); }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Nothing special except for the ExecuteReaderFor&amp;lt;TReturnType&amp;gt;() but that's for
another post. 
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
Having the fluent interface on ICustomSqlCommandBuilder combined with the lambda magic
explained in &lt;a href="http://www.tjsolutions.nl/2009/10/31/FunctionalProgrammingForEveryday.aspx"&gt;this&lt;/a&gt; post
I came up with the following signature:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;ICustomSqlCommandBuilder &lt;/span&gt;ExecuteScalarForThisList&amp;lt;T&amp;gt;( &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;T&amp;gt;
list, &lt;span style="color: blue"&gt;params &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ICustomSqlCommandBuilder&lt;/span&gt;,
T&amp;gt;[] actionList);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;The general idea is to apply &lt;pre class="code"&gt;(s, t) =&amp;gt; s.AddParameterWithValue(&lt;span style="color: #a31515"&gt;&amp;quot;parameterName&amp;quot;&lt;/span&gt;,
t.PROPERTY_NAME)&lt;/pre&gt;
&lt;p&gt;
to every property in the object&amp;lt;T&amp;gt; that needs to be mapped against a parameter
in the stored procedure. Having the params allows us to map as many properties to
parameters in the stored procedure as we like. If we then loop through all the items
in the list first and apply all actions in the actionList per item, we can then call
ExecuteScalar on the stored procedure.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;item &lt;span style="color: blue"&gt;in &lt;/span&gt;list)
{ &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;action &lt;span style="color: blue"&gt;in &lt;/span&gt;actionList)
{ action.Invoke(&lt;span style="color: blue"&gt;this&lt;/span&gt;, item); } &lt;span style="color: blue"&gt;var &lt;/span&gt;result
= sqlCommand.ExecuteScalar(); sqlCommand.Parameters.Clear(); }&lt;/pre&gt;
&lt;p&gt;
Now, another requirement was to wrap it in a transaction and also to have the verification
of the result a bit more flexible. All in all not too complicated, just a matter of
introducing another Action:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ICustomSqlCommandBuilder &lt;/span&gt;ExecuteScalarForThisList&amp;lt;T&amp;gt;( &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;T&amp;gt;
list, &lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;
verify, &lt;span style="color: blue"&gt;params &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ICustomSqlCommandBuilder&lt;/span&gt;,
T&amp;gt;[] actionList) { sqlCommand.Connection.Open(); &lt;span style="color: #2b91af"&gt;SqlTransaction &lt;/span&gt;transaction
= sqlCommand.Connection.BeginTransaction(); sqlCommand.Transaction = transaction; &lt;span style="color: blue"&gt;try &lt;/span&gt;{ &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;item &lt;span style="color: blue"&gt;in &lt;/span&gt;list)
{ &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;action &lt;span style="color: blue"&gt;in &lt;/span&gt;actionList)
{ action.Invoke(&lt;span style="color: blue"&gt;this&lt;/span&gt;, item); } &lt;span style="color: blue"&gt;var &lt;/span&gt;result
= sqlCommand.ExecuteScalar(); verify(result); sqlCommand.Parameters.Clear(); } transaction.Commit();
} &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;SqlException&lt;/span&gt;)
{ transaction.Rollback(); &lt;span style="color: blue"&gt;throw&lt;/span&gt;; } &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Exception&lt;/span&gt;)
{ transaction.Rollback(); &lt;span style="color: blue"&gt;throw&lt;/span&gt;; } &lt;span style="color: blue"&gt;return
this&lt;/span&gt;; }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
Including a bit of exceptionally brittle exception handling and calling the stored
procedure on a list of Customers now resolves to:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;
resultConstraint = result =&amp;gt; { &lt;span style="color: blue"&gt;if &lt;/span&gt;(result != &lt;span style="color: blue"&gt;null&lt;/span&gt;)
{ &lt;span style="color: blue"&gt;if &lt;/span&gt;((&lt;span style="color: blue"&gt;int&lt;/span&gt;)result
&amp;gt; 1000) { &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Exception&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;result
should be less than 1000!&amp;quot;&lt;/span&gt;); } } }; builder .SetStoredProcedureNameTo(&lt;span style="color: #a31515"&gt;&amp;quot;sens_sp_modifyCustomerIncome&amp;quot;&lt;/span&gt;)
.ExecuteScalarForThisList( customerList, resultConstraint, (s, t) =&amp;gt; s.AddParameterWithValue(&lt;span style="color: #a31515"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;,
t.ID), (s, t) =&amp;gt; s.AddParameterWithValue(&lt;span style="color: #a31515"&gt;&amp;quot;income&amp;quot;&lt;/span&gt;,
t.Income) &lt;span style="color: green"&gt;//etc for the rest of the properties &lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;I really, really like this syntax! &lt;img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=eba99e4a-f608-48e7-8541-397cb4c9bcf1" /&gt;</description>
      <comments>http://www.tjsolutions.nl/CommentView,guid,eba99e4a-f608-48e7-8541-397cb4c9bcf1.aspx</comments>
      <category>.NET</category>
      <category>ADO.Net</category>
      <category>Functional</category>
    </item>
  </channel>
</rss>