<?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 - .NET</title>
    <link>http://www.tjsolutions.nl/</link>
    <description />
    <language>en-us</language>
    <copyright>Tijmen van de Kamp, Tom de Koning</copyright>
    <lastBuildDate>Mon, 16 Nov 2009 18:01:36 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=b9a7be28-2e24-451c-82a2-4ac11d6922d4</trackback:ping>
      <pingback:server>http://www.tjsolutions.nl/pingback.aspx</pingback:server>
      <pingback:target>http://www.tjsolutions.nl/PermaLink,guid,b9a7be28-2e24-451c-82a2-4ac11d6922d4.aspx</pingback:target>
      <dc:creator>Tom</dc:creator>
      <wfw:comment>http://www.tjsolutions.nl/CommentView,guid,b9a7be28-2e24-451c-82a2-4ac11d6922d4.aspx</wfw:comment>
      <wfw:commentRss>http://www.tjsolutions.nl/SyndicationService.asmx/GetEntryCommentsRss?guid=b9a7be28-2e24-451c-82a2-4ac11d6922d4</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Just a quick update on my <a href="http://www.tjsolutions.nl/2009/10/31/FunctionalProgrammingWithTheRequestResponseServiceLayer.aspx">previous</a> posting
following one of the comments made by <a href="http://benpittoors.wordpress.com/">den
Ben</a></p>
        <p>
 
</p>
        <p>
just out of curiosity... why wouldn't you 
<br />
dispatcher.Add&lt;SaveDocumentRequest&gt;(s =&gt; 
<br />
{ 
<br />
s.Document = document; 
<br />
s.UserID = userID; 
<br />
});
</p>
        <p>
 
</p>
        <p>
He's absolutely right. Using a Func forced me to (mis)use the params keyword as you
are not allowed  to use the above syntax with the old code: 
</p>
        <pre class="code">
          <span style="color: blue">public virtual </span>
          <span style="color: #2b91af">IDispatcher </span>Add&lt;TRequestType&gt;(<span style="color: blue">params </span><span style="color: #2b91af">Func</span>&lt;TRequestType, <span style="color: blue">object</span>&gt;[]
funcs) <span style="color: blue">where </span>TRequestType : <span style="color: #2b91af">Request</span>, <span style="color: blue">new</span>()
{ TRequestType request = <span style="color: blue">new </span>TRequestType(); <span style="color: blue">foreach </span>(<span style="color: blue">var </span>func <span style="color: blue">in </span>funcs)
{ func.Invoke(request); } AddRequest(request, <span style="color: blue">false</span>); <span style="color: blue">return
this</span>; }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
          <a href="http://11011.net/software/vspaste">
          </a>
        </p>
        <p>
I actually had noticed the code not using the return value by the func but disregarded
it as I was a bit too happy with the "elegance" of my inital solution.
</p>
        <p>
But I think (and I think you'll agree) that using an Action is much cleaner here:
</p>
        <pre class="code">
          <span style="color: blue">public virtual </span>
          <span style="color: #2b91af">IDispatcher </span>Add&lt;TRequestType&gt;(<span style="color: #2b91af">Action</span>&lt;TRequestType&gt;
action) <span style="color: blue">where </span>TRequestType : <span style="color: #2b91af">Request</span>, <span style="color: blue">new</span>()
{ TRequestType request = <span style="color: blue">new </span>TRequestType(); action(request);
AddRequest(request, <span style="color: blue">false</span>); <span style="color: blue">return
this</span>; }</pre>
        <p>
 
</p>
        <p>
Thanks <a href="http://benpittoors.wordpress.com/">den Ben</a>!<a href="http://11011.net/software/vspaste"></a></p>
        <img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=b9a7be28-2e24-451c-82a2-4ac11d6922d4" />
      </body>
      <title>Functional programming with the Request/Response Service Layer part2</title>
      <guid isPermaLink="false">http://www.tjsolutions.nl/PermaLink,guid,b9a7be28-2e24-451c-82a2-4ac11d6922d4.aspx</guid>
      <link>http://www.tjsolutions.nl/2009/11/16/FunctionalProgrammingWithTheRequestResponseServiceLayerPart2.aspx</link>
      <pubDate>Mon, 16 Nov 2009 18:01:36 GMT</pubDate>
      <description>&lt;p&gt;
Just a quick update on my &lt;a href="http://www.tjsolutions.nl/2009/10/31/FunctionalProgrammingWithTheRequestResponseServiceLayer.aspx"&gt;previous&lt;/a&gt; posting
following one of the comments made by &lt;a href="http://benpittoors.wordpress.com/"&gt;den
Ben&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
just out of curiosity... why wouldn't you 
&lt;br /&gt;
dispatcher.Add&amp;lt;SaveDocumentRequest&amp;gt;(s =&amp;gt; 
&lt;br /&gt;
{ 
&lt;br /&gt;
s.Document = document; 
&lt;br /&gt;
s.UserID = userID; 
&lt;br /&gt;
});
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
He's absolutely right. Using a Func forced me to (mis)use the params keyword as you
are not allowed&amp;#160; to use the above syntax with the old code: 
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public virtual &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IDispatcher &lt;/span&gt;Add&amp;lt;TRequestType&amp;gt;(&lt;span style="color: blue"&gt;params &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TRequestType, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;[]
funcs) &lt;span style="color: blue"&gt;where &lt;/span&gt;TRequestType : &lt;span style="color: #2b91af"&gt;Request&lt;/span&gt;, &lt;span style="color: blue"&gt;new&lt;/span&gt;()
{ TRequestType request = &lt;span style="color: blue"&gt;new &lt;/span&gt;TRequestType(); &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;func &lt;span style="color: blue"&gt;in &lt;/span&gt;funcs)
{ func.Invoke(request); } AddRequest(request, &lt;span style="color: blue"&gt;false&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;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
I actually had noticed the code not using the return value by the func but disregarded
it as I was a bit too happy with the "elegance" of my inital solution.
&lt;/p&gt;
&lt;p&gt;
But I think (and I think you'll agree) that using an Action is much cleaner here:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public virtual &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IDispatcher &lt;/span&gt;Add&amp;lt;TRequestType&amp;gt;(&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;TRequestType&amp;gt;
action) &lt;span style="color: blue"&gt;where &lt;/span&gt;TRequestType : &lt;span style="color: #2b91af"&gt;Request&lt;/span&gt;, &lt;span style="color: blue"&gt;new&lt;/span&gt;()
{ TRequestType request = &lt;span style="color: blue"&gt;new &lt;/span&gt;TRequestType(); action(request);
AddRequest(request, &lt;span style="color: blue"&gt;false&lt;/span&gt;); &lt;span style="color: blue"&gt;return
this&lt;/span&gt;; }&lt;/pre&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
Thanks &lt;a href="http://benpittoors.wordpress.com/"&gt;den Ben&lt;/a&gt;!&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=b9a7be28-2e24-451c-82a2-4ac11d6922d4" /&gt;</description>
      <comments>http://www.tjsolutions.nl/CommentView,guid,b9a7be28-2e24-451c-82a2-4ac11d6922d4.aspx</comments>
      <category>.NET</category>
      <category>Functional</category>
      <category>Linq</category>
      <category>Request Response Service Layer</category>
    </item>
    <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>
    <item>
      <trackback:ping>http://www.tjsolutions.nl/Trackback.aspx?guid=da163853-8db7-45bf-a157-9c0058030aa3</trackback:ping>
      <pingback:server>http://www.tjsolutions.nl/pingback.aspx</pingback:server>
      <pingback:target>http://www.tjsolutions.nl/PermaLink,guid,da163853-8db7-45bf-a157-9c0058030aa3.aspx</pingback:target>
      <dc:creator>Tom</dc:creator>
      <wfw:comment>http://www.tjsolutions.nl/CommentView,guid,da163853-8db7-45bf-a157-9c0058030aa3.aspx</wfw:comment>
      <wfw:commentRss>http://www.tjsolutions.nl/SyndicationService.asmx/GetEntryCommentsRss?guid=da163853-8db7-45bf-a157-9c0058030aa3</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
At my current project we are using a dispatcher class to batch requests to our WCF
layer based on the <a href="http://davybrion.com/blog/2009/11/requestresponse-service-layer-series/">Request/Response
Service Layer</a> created by Davy Brion. Once you call the Get&lt;TResponseType&gt;()
on the dispatcher the WCF layer starts working on the requests one by one and return
them in one roundtrip.
</p>
        <p>
Adding requests to the dispatcher is easy:
</p>
        <pre class="code">
          <span style="color: blue">var </span>saveDocumentRequest = <span style="color: blue">new </span><span style="color: #2b91af">SaveDocumentRequest</span>(); <span style="color: blue">var </span>getRemainingDocuments
= <span style="color: blue">new </span><span style="color: #2b91af">GetRemainingDocumentsRequest</span>();
dispatcher.Add(saveDocumentRequest, getRemainingDocuments);</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
 
</p>
        <p>
Where the signature of the Add method in Dispatcher is like;
</p>
        <pre class="code">
          <span style="color: blue">public void </span>Add(<span style="color: blue">params </span><span style="color: #2b91af">Request</span>[]
requests)</pre>
        <p>
        </p>
        <p>
Although this syntax is not bad, it feels like so much like code from last month:
not very sexy. Wouldn't be more fun if we could spice it up a bit using generics and
a bit of Func?
</p>
        <p>
I'd love to get rid of instantiating a variable just for the sake of adding it to
my dispatcher. After a bit of fiddling around with my unit tests I came up with the
following solution.
</p>
        <p>
What I want is to call the Add method in a generic way and set the parameters on my
request object inline, using a Func. This is best illustrated with a test:
</p>
        <pre class="code">[<span style="color: #2b91af">Test</span>] <span style="color: blue">public
void </span>can_assign_value_using_a_func_in_a_generic_method_call() { <span style="color: #2b91af">Guid </span>id
= <span style="color: #2b91af">Guid</span>.NewGuid(); <span style="color: blue">var </span>dispatcher
= <span style="color: blue">new </span><span style="color: #2b91af">Dispatcher</span>();
dispatcher.Add&lt;<span style="color: #2b91af">GetPersonsRequest</span>&gt;(s =&gt;
s.ID = id); <span style="color: green">//disregard ugly cast; this is only for test
purposes </span><span style="color: blue">var </span>request = (<span style="color: #2b91af">GetPersonsRequest</span>)dispatcher.Requests.First();
request.ID.ShouldEqual(id); }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>Where the Request and dispatcher have
these signatures: 
<br /><pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">GetPersonsRequest</span>:<span style="color: #2b91af">Request </span>{ <span style="color: blue">public </span><span style="color: #2b91af">Guid </span>ID
{ <span style="color: blue">get</span>; <span style="color: blue">set</span>; } } <span style="color: blue">public
class </span><span style="color: #2b91af">Dispatcher </span>{ <span style="color: blue">public </span>Dispatcher()
{ Requests = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Request</span>&gt;();
} <span style="color: blue">public void </span>Add&lt;TRequestType&gt;(<span style="color: blue">params </span><span style="color: #2b91af">Func</span>&lt;TRequestType, <span style="color: blue">object</span>&gt;[]
funcs)<br /><span style="color: blue">where </span>TRequestType : <span style="color: #2b91af">Request</span>, <span style="color: blue">new</span>()
{ <span style="color: blue">var </span>request = <span style="color: blue">new </span>TRequestType(); <span style="color: blue">foreach </span>(<span style="color: blue">var </span>func <span style="color: blue">in </span>funcs)
{ func(request); } Requests.Add(request); } <span style="color: blue">public </span><span style="color: #2b91af">IList</span>&lt;<span style="color: #2b91af">Request</span>&gt;
Requests { <span style="color: blue">get</span>; <span style="color: blue">set</span>;
} }</pre><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a><p>
I can now set only property in my Request type, what if it has more properties that
need to be set? Just introduce the infamous params to the method signature like so:
</p><pre class="code"><span style="color: blue">public void </span>Add&lt;TRequestType&gt;(<span style="color: blue">params </span><span style="color: #2b91af">Func</span>&lt;TRequestType, <span style="color: blue">object</span>&gt;[]
funcs) 
<br /><span style="color: blue">where </span>TRequestType:<span style="color: #2b91af">Request</span>, <span style="color: blue">new</span>()
{ <span style="color: blue">var </span>request = <span style="color: blue">new </span>TRequestType(); <span style="color: blue">foreach </span>(<span style="color: blue">var </span>func <span style="color: blue">in </span>funcs)
{ func(request); } Requests.Add(request); }</pre><p><a href="http://11011.net/software/vspaste"></a></p><p>
And again the test:
</p><pre class="code">[<span style="color: #2b91af">Test</span>] <span style="color: blue">public
void </span>can_assign_two_values_using_a_func_in_a_generic_method_call() { <span style="color: blue">var </span>dispatcher
= <span style="color: blue">new </span><span style="color: #2b91af">Dispatcher</span>(); <span style="color: #2b91af">Guid </span>id
= <span style="color: #2b91af">Guid</span>.NewGuid(); dispatcher.Add&lt;<span style="color: #2b91af">GetPersonsRequest</span>&gt;(s
=&gt; s.ID = id, t=&gt; t.GetAllDetails = <span style="color: blue">true</span>); <span style="color: green">//disregard
ugly cast; this is only for test purposes </span><span style="color: blue">var </span>request
= (<span style="color: #2b91af">GetPersonsRequest</span>)dispatcher.Requests.First();
request.ID.ShouldEqual(id); request.GetAllDetails.ShouldBeTrue(); }</pre><a href="http://11011.net/software/vspaste"></a><p>
When we change the return type of the Add method to the dispatcher itself, we can
then use a fluent interface to get a very friendly syntax  for making calls:
</p><p>
 
</p><pre class="code"><span style="color: green">//input parameters for first request </span><span style="color: blue">var </span>document
= <span style="color: blue">new object</span>(); <span style="color: blue">var </span>userID
= <span style="color: #2b91af">Guid</span>.NewGuid(); <span style="color: green">//input
parameters for second request </span><span style="color: blue">var </span>dossierID
= <span style="color: #2b91af">Guid</span>.NewGuid(); dispatcher .Add&lt;<span style="color: #2b91af">SaveDocumentRequest</span>&gt;(s
=&gt; s.Document = document, t =&gt; t.UserID = userID) .Add&lt;<span style="color: #2b91af">GetZaakRequest</span>&gt;(s
=&gt; s.DossierId = dossierID);</pre><p>
I really like the fact that I don't have to initialize a variable and still have access
to all the properties in the request object..
</p><img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=da163853-8db7-45bf-a157-9c0058030aa3" /></body>
      <title>Functional programming with the Request/Response Service Layer</title>
      <guid isPermaLink="false">http://www.tjsolutions.nl/PermaLink,guid,da163853-8db7-45bf-a157-9c0058030aa3.aspx</guid>
      <link>http://www.tjsolutions.nl/2009/10/31/FunctionalProgrammingWithTheRequestResponseServiceLayer.aspx</link>
      <pubDate>Sat, 31 Oct 2009 16:54:24 GMT</pubDate>
      <description>&lt;p&gt;
At my current project we are using a dispatcher class to batch requests to our WCF
layer based on the &lt;a href="http://davybrion.com/blog/2009/11/requestresponse-service-layer-series/"&gt;Request/Response
Service Layer&lt;/a&gt; created by Davy Brion. Once you call the Get&amp;lt;TResponseType&amp;gt;()
on the dispatcher the WCF layer starts working on the requests one by one and return
them in one roundtrip.
&lt;/p&gt;
&lt;p&gt;
Adding requests to the dispatcher is easy:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;saveDocumentRequest = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SaveDocumentRequest&lt;/span&gt;(); &lt;span style="color: blue"&gt;var &lt;/span&gt;getRemainingDocuments
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;GetRemainingDocumentsRequest&lt;/span&gt;();
dispatcher.Add(saveDocumentRequest, getRemainingDocuments);&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;
Where the signature of the Add method in Dispatcher is like;
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public void &lt;/span&gt;Add(&lt;span style="color: blue"&gt;params &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Request&lt;/span&gt;[]
requests)&lt;/pre&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Although this syntax is not bad, it feels like so much like code from last month:
not very sexy. Wouldn't be more fun if we could spice it up a bit using generics and
a bit of Func?
&lt;/p&gt;
&lt;p&gt;
I'd love to get rid of instantiating a variable just for the sake of adding it to
my dispatcher. After a bit of fiddling around with my unit tests I came up with the
following solution.
&lt;/p&gt;
&lt;p&gt;
What I want is to call the Add method in a generic way and set the parameters on my
request object inline, using a Func. This is best illustrated with a test:
&lt;/p&gt;
&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;Test&lt;/span&gt;] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;can_assign_value_using_a_func_in_a_generic_method_call() { &lt;span style="color: #2b91af"&gt;Guid &lt;/span&gt;id
= &lt;span style="color: #2b91af"&gt;Guid&lt;/span&gt;.NewGuid(); &lt;span style="color: blue"&gt;var &lt;/span&gt;dispatcher
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Dispatcher&lt;/span&gt;();
dispatcher.Add&amp;lt;&lt;span style="color: #2b91af"&gt;GetPersonsRequest&lt;/span&gt;&amp;gt;(s =&amp;gt;
s.ID = id); &lt;span style="color: green"&gt;//disregard ugly cast; this is only for test
purposes &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;request = (&lt;span style="color: #2b91af"&gt;GetPersonsRequest&lt;/span&gt;)dispatcher.Requests.First();
request.ID.ShouldEqual(id); }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Where the Request and dispatcher have
these signatures: 
&lt;br /&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;GetPersonsRequest&lt;/span&gt;:&lt;span style="color: #2b91af"&gt;Request &lt;/span&gt;{ &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Guid &lt;/span&gt;ID
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; } } &lt;span style="color: blue"&gt;public
class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Dispatcher &lt;/span&gt;{ &lt;span style="color: blue"&gt;public &lt;/span&gt;Dispatcher()
{ Requests = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Request&lt;/span&gt;&amp;gt;();
} &lt;span style="color: blue"&gt;public void &lt;/span&gt;Add&amp;lt;TRequestType&amp;gt;(&lt;span style="color: blue"&gt;params &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TRequestType, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;[]
funcs)&lt;br /&gt;
&lt;span style="color: blue"&gt;where &lt;/span&gt;TRequestType : &lt;span style="color: #2b91af"&gt;Request&lt;/span&gt;, &lt;span style="color: blue"&gt;new&lt;/span&gt;()
{ &lt;span style="color: blue"&gt;var &lt;/span&gt;request = &lt;span style="color: blue"&gt;new &lt;/span&gt;TRequestType(); &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;func &lt;span style="color: blue"&gt;in &lt;/span&gt;funcs)
{ func(request); } Requests.Add(request); } &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Request&lt;/span&gt;&amp;gt;
Requests { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&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;
I can now set only property in my Request type, what if it has more properties that
need to be set? Just introduce the infamous params to the method signature like so:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public void &lt;/span&gt;Add&amp;lt;TRequestType&amp;gt;(&lt;span style="color: blue"&gt;params &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TRequestType, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;[]
funcs) 
&lt;br /&gt;
&lt;span style="color: blue"&gt;where &lt;/span&gt;TRequestType:&lt;span style="color: #2b91af"&gt;Request&lt;/span&gt;, &lt;span style="color: blue"&gt;new&lt;/span&gt;()
{ &lt;span style="color: blue"&gt;var &lt;/span&gt;request = &lt;span style="color: blue"&gt;new &lt;/span&gt;TRequestType(); &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;func &lt;span style="color: blue"&gt;in &lt;/span&gt;funcs)
{ func(request); } Requests.Add(request); }&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
And again the test:
&lt;/p&gt;
&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;Test&lt;/span&gt;] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;can_assign_two_values_using_a_func_in_a_generic_method_call() { &lt;span style="color: blue"&gt;var &lt;/span&gt;dispatcher
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Dispatcher&lt;/span&gt;(); &lt;span style="color: #2b91af"&gt;Guid &lt;/span&gt;id
= &lt;span style="color: #2b91af"&gt;Guid&lt;/span&gt;.NewGuid(); dispatcher.Add&amp;lt;&lt;span style="color: #2b91af"&gt;GetPersonsRequest&lt;/span&gt;&amp;gt;(s
=&amp;gt; s.ID = id, t=&amp;gt; t.GetAllDetails = &lt;span style="color: blue"&gt;true&lt;/span&gt;); &lt;span style="color: green"&gt;//disregard
ugly cast; this is only for test purposes &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;request
= (&lt;span style="color: #2b91af"&gt;GetPersonsRequest&lt;/span&gt;)dispatcher.Requests.First();
request.ID.ShouldEqual(id); request.GetAllDetails.ShouldBeTrue(); }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
When we change the return type of the Add method to the dispatcher itself, we can
then use a fluent interface to get a very friendly syntax&amp;#160; for making calls:
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green"&gt;//input parameters for first request &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;document
= &lt;span style="color: blue"&gt;new object&lt;/span&gt;(); &lt;span style="color: blue"&gt;var &lt;/span&gt;userID
= &lt;span style="color: #2b91af"&gt;Guid&lt;/span&gt;.NewGuid(); &lt;span style="color: green"&gt;//input
parameters for second request &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;dossierID
= &lt;span style="color: #2b91af"&gt;Guid&lt;/span&gt;.NewGuid(); dispatcher .Add&amp;lt;&lt;span style="color: #2b91af"&gt;SaveDocumentRequest&lt;/span&gt;&amp;gt;(s
=&amp;gt; s.Document = document, t =&amp;gt; t.UserID = userID) .Add&amp;lt;&lt;span style="color: #2b91af"&gt;GetZaakRequest&lt;/span&gt;&amp;gt;(s
=&amp;gt; s.DossierId = dossierID);&lt;/pre&gt;
&lt;p&gt;
I really like the fact that I don't have to initialize a variable and still have access
to all the properties in the request object..
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=da163853-8db7-45bf-a157-9c0058030aa3" /&gt;</description>
      <comments>http://www.tjsolutions.nl/CommentView,guid,da163853-8db7-45bf-a157-9c0058030aa3.aspx</comments>
      <category>.NET</category>
      <category>Functional</category>
      <category>Linq</category>
    </item>
    <item>
      <trackback:ping>http://www.tjsolutions.nl/Trackback.aspx?guid=0b3e5fae-eebf-45b7-a6f9-e08ec39200af</trackback:ping>
      <pingback:server>http://www.tjsolutions.nl/pingback.aspx</pingback:server>
      <pingback:target>http://www.tjsolutions.nl/PermaLink,guid,0b3e5fae-eebf-45b7-a6f9-e08ec39200af.aspx</pingback:target>
      <dc:creator>Tijmen</dc:creator>
      <wfw:comment>http://www.tjsolutions.nl/CommentView,guid,0b3e5fae-eebf-45b7-a6f9-e08ec39200af.aspx</wfw:comment>
      <wfw:commentRss>http://www.tjsolutions.nl/SyndicationService.asmx/GetEntryCommentsRss?guid=0b3e5fae-eebf-45b7-a6f9-e08ec39200af</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
At my current project we needed a quick sort of throw away app that was going to be
used by a small group of people. Without going into much details the app basically
is a planning and analysis module for the allocation of employees to divisions. 
</p>
        <p>
Since development speed is crucial we decided to work with Linq2sql and winforms.
No need for all sorts of layers; just get it done.
</p>
        <p>
 
</p>
        <p>
We needed to fill a combobox with values. This should be trivial, but was a bit more
trouble than expected. I ended up using the excellent description found <a href="http://www.madprops.org/blog/Bind-a-ComboBox-to-a-generic-Dictionary/" target="_blank">here</a>.
Basically all you need to do is:
</p>
        <pre class="code">
          <span style="color: blue;">var </span>comboBox = <span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">ComboBox</span>(); <span style="color: blue;">var </span>choices
= <span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">Dictionary</span>&lt;<span style="color: blue;">string</span>, <span style="color: blue;">string</span>&gt;();
choices[<span style="color: rgb(163, 21, 21);">"A"</span>] = <span style="color: rgb(163, 21, 21);">"Arthur"</span>;
choices[<span style="color: rgb(163, 21, 21);">"F"</span>] = <span style="color: rgb(163, 21, 21);">"Ford"</span>;
choices[<span style="color: rgb(163, 21, 21);">"T"</span>] = <span style="color: rgb(163, 21, 21);">"Trillian"</span>;
choices[<span style="color: rgb(163, 21, 21);">"Z"</span>] = <span style="color: rgb(163, 21, 21);">"Zaphod"</span>;
comboBox.DataSource = <span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">BindingSource</span>(choices, <span style="color: blue;">null</span>);
comboBox.DisplayMember = <span style="color: rgb(163, 21, 21);">"Value"</span>; comboBox.ValueMember
= <span style="color: rgb(163, 21, 21);">"Key"</span>; </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
 
</p>
        <p>
Notice the clever use of <span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">BindingSource</span>(choices, <span style="color: blue;">null</span>); 
</p>
        <p>
 
</p>
        <p>
Nothing complex here, but this is static and I wanted to get the values from the db.
Next to that it would be nice if we could have some sort of generic function that
could take a type of TEntity (table) and some hints to which properties in TEntity
it needed as Key and Value.
</p>
        <p>
I ended up with this:
</p>
        <pre class="code">
          <span style="color: blue;">public interface </span>
          <span style="color: rgb(43, 145, 175);">IComboBoxHelper </span>{ <span style="color: blue;">void </span>SetComboBoxContent&lt;TEntity,
TKey, TValue&gt;( <span style="color: rgb(43, 145, 175);">Func</span>&lt;TEntity,
TKey&gt; key, <span style="color: rgb(43, 145, 175);">Func</span>&lt;TEntity, TValue&gt;
value, <span style="color: rgb(43, 145, 175);">ComboBox </span>comboBox) <span style="color: blue;">where </span>TEntity
: <span style="color: blue;">class</span>; }</pre>
        <p>
Usage becomes pretty straightforward: register the IComboBoxHelper and it's implementation
to the Dependency injection container. Add a constructor dependency and use the helper
variable:
</p>
        <pre class="code">comboBoxHelper.SetComboBoxContent&lt;<span style="color: rgb(43, 145, 175);">Divisie</span>, <span style="color: blue;">int</span>, <span style="color: blue;">string</span>&gt;(s=&gt;
s.Divisie_PK, s=&gt; s.Divisienaam, comboBox);</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
The implementation of the IComboBoxHelper takes a datacontext and is responsible for
looking up the required entity.
</p>
        <p>
Let's have a look at the actual implementation:
</p>
        <pre class="code">
          <span style="color: blue;">public class </span>
          <span style="color: rgb(43, 145, 175);">ComboBoxHelper </span>: <span style="color: rgb(43, 145, 175);">IComboBoxHelper </span>{ <span style="color: blue;">readonly </span><span style="color: rgb(43, 145, 175);">HRDataContext </span>dataContext; <span style="color: blue;">public </span>ComboBoxHelper(<span style="color: rgb(43, 145, 175);">HRDataContext </span>dataContext)
{ <span style="color: blue;">this</span>.dataContext = dataContext; } <span style="color: blue;">public
void </span>SetComboBoxContent&lt;TEntity, TKey, TValue&gt;( <span style="color: rgb(43, 145, 175);">Func</span>&lt;TEntity,
TKey&gt; key, <span style="color: rgb(43, 145, 175);">Func</span>&lt;TEntity, TValue&gt;
value, <span style="color: rgb(43, 145, 175);">ComboBox </span>comboBox) <span style="color: blue;">where </span>TEntity
: <span style="color: blue;">class </span>{ <span style="color: rgb(43, 145, 175);">Dictionary</span>&lt;TKey,
TValue&gt; dictionary = <span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">Dictionary</span>&lt;TKey,
TValue&gt;(); <span style="color: rgb(43, 145, 175);">IQueryable</span>&lt;TEntity&gt;
entities = dataContext.GetTable&lt;TEntity&gt;().Select(s =&gt; s); entities.ForEach(s
=&gt; dictionary.Add(key(s), value(s))); <span style="color: blue;">var </span>source
= <span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">BindingSource</span>(dictionary, <span style="color: blue;">null</span>);
comboBox.DataSource = source; comboBox.DisplayMember = <span style="color: rgb(163, 21, 21);">"Value"</span>;
comboBox.ValueMember = <span style="color: rgb(163, 21, 21);">"Key"</span>; } }</pre>
        <p>
Nothing too complicated, but the use of Func's to get to the properties in the required
Entity is pretty nice. 
</p>
        <div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:a3eb325c-878e-46ed-9bc7-f14715e8caca" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/Linq" rel="tag">Linq</a>,<a href="http://technorati.com/tags/Linq2Sql" rel="tag">Linq2Sql</a>,<a href="http://technorati.com/tags/Winforms" rel="tag">Winforms</a></div>
        <img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=0b3e5fae-eebf-45b7-a6f9-e08ec39200af" />
      </body>
      <title>Fun with Linq to sql and Comboboxes</title>
      <guid isPermaLink="false">http://www.tjsolutions.nl/PermaLink,guid,0b3e5fae-eebf-45b7-a6f9-e08ec39200af.aspx</guid>
      <link>http://www.tjsolutions.nl/2009/08/08/FunWithLinqToSqlAndComboboxes.aspx</link>
      <pubDate>Sat, 08 Aug 2009 13:10:40 GMT</pubDate>
      <description>&lt;p&gt;
At my current project we needed a quick sort of throw away app that was going to be
used by a small group of people. Without going into much details the app basically
is a planning and analysis module for the allocation of employees to divisions. 
&lt;/p&gt;
&lt;p&gt;
Since development speed is crucial we decided to work with Linq2sql and winforms.
No need for all sorts of layers; just get it done.
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
We needed to fill a combobox with values. This should be trivial, but was a bit more
trouble than expected. I ended up using the excellent description found &lt;a href="http://www.madprops.org/blog/Bind-a-ComboBox-to-a-generic-Dictionary/" target="_blank"&gt;here&lt;/a&gt;.
Basically all you need to do is:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;var &lt;/span&gt;comboBox = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ComboBox&lt;/span&gt;(); &lt;span style="color: blue;"&gt;var &lt;/span&gt;choices
= &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;, &lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;gt;();
choices[&lt;span style="color: rgb(163, 21, 21);"&gt;"A"&lt;/span&gt;] = &lt;span style="color: rgb(163, 21, 21);"&gt;"Arthur"&lt;/span&gt;;
choices[&lt;span style="color: rgb(163, 21, 21);"&gt;"F"&lt;/span&gt;] = &lt;span style="color: rgb(163, 21, 21);"&gt;"Ford"&lt;/span&gt;;
choices[&lt;span style="color: rgb(163, 21, 21);"&gt;"T"&lt;/span&gt;] = &lt;span style="color: rgb(163, 21, 21);"&gt;"Trillian"&lt;/span&gt;;
choices[&lt;span style="color: rgb(163, 21, 21);"&gt;"Z"&lt;/span&gt;] = &lt;span style="color: rgb(163, 21, 21);"&gt;"Zaphod"&lt;/span&gt;;
comboBox.DataSource = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;BindingSource&lt;/span&gt;(choices, &lt;span style="color: blue;"&gt;null&lt;/span&gt;);
comboBox.DisplayMember = &lt;span style="color: rgb(163, 21, 21);"&gt;"Value"&lt;/span&gt;; comboBox.ValueMember
= &lt;span style="color: rgb(163, 21, 21);"&gt;"Key"&lt;/span&gt;; &lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
Notice the clever use of &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;BindingSource&lt;/span&gt;(choices, &lt;span style="color: blue;"&gt;null&lt;/span&gt;); 
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
Nothing complex here, but this is static and I wanted to get the values from the db.
Next to that it would be nice if we could have some sort of generic function that
could take a type of TEntity (table) and some hints to which properties in TEntity
it needed as Key and Value.
&lt;/p&gt;
&lt;p&gt;
I ended up with this:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public interface &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IComboBoxHelper &lt;/span&gt;{ &lt;span style="color: blue;"&gt;void &lt;/span&gt;SetComboBoxContent&amp;lt;TEntity,
TKey, TValue&amp;gt;( &lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&amp;lt;TEntity,
TKey&amp;gt; key, &lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&amp;lt;TEntity, TValue&amp;gt;
value, &lt;span style="color: rgb(43, 145, 175);"&gt;ComboBox &lt;/span&gt;comboBox) &lt;span style="color: blue;"&gt;where &lt;/span&gt;TEntity
: &lt;span style="color: blue;"&gt;class&lt;/span&gt;; }&lt;/pre&gt;
&lt;p&gt;
Usage becomes pretty straightforward: register the IComboBoxHelper and it's implementation
to the Dependency injection container. Add a constructor dependency and use the helper
variable:
&lt;/p&gt;
&lt;pre class="code"&gt;comboBoxHelper.SetComboBoxContent&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Divisie&lt;/span&gt;, &lt;span style="color: blue;"&gt;int&lt;/span&gt;, &lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;gt;(s=&amp;gt;
s.Divisie_PK, s=&amp;gt; s.Divisienaam, comboBox);&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;
The implementation of the IComboBoxHelper takes a datacontext and is responsible for
looking up the required entity.
&lt;/p&gt;
&lt;p&gt;
Let's have a look at the actual implementation:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ComboBoxHelper &lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;IComboBoxHelper &lt;/span&gt;{ &lt;span style="color: blue;"&gt;readonly &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;HRDataContext &lt;/span&gt;dataContext; &lt;span style="color: blue;"&gt;public &lt;/span&gt;ComboBoxHelper(&lt;span style="color: rgb(43, 145, 175);"&gt;HRDataContext &lt;/span&gt;dataContext)
{ &lt;span style="color: blue;"&gt;this&lt;/span&gt;.dataContext = dataContext; } &lt;span style="color: blue;"&gt;public
void &lt;/span&gt;SetComboBoxContent&amp;lt;TEntity, TKey, TValue&amp;gt;( &lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&amp;lt;TEntity,
TKey&amp;gt; key, &lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&amp;lt;TEntity, TValue&amp;gt;
value, &lt;span style="color: rgb(43, 145, 175);"&gt;ComboBox &lt;/span&gt;comboBox) &lt;span style="color: blue;"&gt;where &lt;/span&gt;TEntity
: &lt;span style="color: blue;"&gt;class &lt;/span&gt;{ &lt;span style="color: rgb(43, 145, 175);"&gt;Dictionary&lt;/span&gt;&amp;lt;TKey,
TValue&amp;gt; dictionary = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Dictionary&lt;/span&gt;&amp;lt;TKey,
TValue&amp;gt;(); &lt;span style="color: rgb(43, 145, 175);"&gt;IQueryable&lt;/span&gt;&amp;lt;TEntity&amp;gt;
entities = dataContext.GetTable&amp;lt;TEntity&amp;gt;().Select(s =&amp;gt; s); entities.ForEach(s
=&amp;gt; dictionary.Add(key(s), value(s))); &lt;span style="color: blue;"&gt;var &lt;/span&gt;source
= &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;BindingSource&lt;/span&gt;(dictionary, &lt;span style="color: blue;"&gt;null&lt;/span&gt;);
comboBox.DataSource = source; comboBox.DisplayMember = &lt;span style="color: rgb(163, 21, 21);"&gt;"Value"&lt;/span&gt;;
comboBox.ValueMember = &lt;span style="color: rgb(163, 21, 21);"&gt;"Key"&lt;/span&gt;; } }&lt;/pre&gt;
&lt;p&gt;
Nothing too complicated, but the use of Func's to get to the properties in the required
Entity is pretty nice. 
&lt;/p&gt;
&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:a3eb325c-878e-46ed-9bc7-f14715e8caca" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/Linq" rel="tag"&gt;Linq&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Linq2Sql" rel="tag"&gt;Linq2Sql&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Winforms" rel="tag"&gt;Winforms&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=0b3e5fae-eebf-45b7-a6f9-e08ec39200af" /&gt;</description>
      <comments>http://www.tjsolutions.nl/CommentView,guid,0b3e5fae-eebf-45b7-a6f9-e08ec39200af.aspx</comments>
      <category>.NET</category>
    </item>
  </channel>
</rss>