<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><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>Chris Rauber</title>
    <link>http://www.chrisrauber.com/blog/</link>
    <description>this.Blog.Find(entry =&gt; entry.IsHelpful);</description>
    <language>en-us</language>
    <copyright>Chris Rauber</copyright>
    <lastBuildDate>Wed, 10 Jun 2009 04:02:10 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>chris.rauber@gmail.com</managingEditor>
    <webMaster>chris.rauber@gmail.com</webMaster>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/ChrisRauber" type="application/rss+xml" /><item>
      <trackback:ping>http://www.chrisrauber.com/blog/Trackback.aspx?guid=87864caa-72fb-4f07-a5bc-ff00ba91170c</trackback:ping>
      <pingback:server>http://www.chrisrauber.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.chrisrauber.com/blog/PermaLink,guid,87864caa-72fb-4f07-a5bc-ff00ba91170c.aspx</pingback:target>
      <dc:creator>Chris Rauber</dc:creator>
      <wfw:comment>http://www.chrisrauber.com/blog/CommentView,guid,87864caa-72fb-4f07-a5bc-ff00ba91170c.aspx</wfw:comment>
      <wfw:commentRss>http://www.chrisrauber.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=87864caa-72fb-4f07-a5bc-ff00ba91170c</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
If you’re like me (and apparently <a href="http://www.bing.com/search?q=wcf+IDisposable" target="_blank">many
other people</a>), when you start using (no pun intended) WCF by creating a client
channel, it’s only a matter of time before you realize that you’re probably doing
it wrong.  
</p>
        <p>
Problem number one is that if you don’t explicitly close the connection, it’s only
a matter of time before the connection starts faulting.  So you say to yourself,
“Aha!  I need to close the damn connection.  And since I’m a good .Net programmer,
I see the IChannelListener interface extends the IDisposable interface so I can just
wrap my call in a ‘using’ statement which will explicitly clean up all my resources
no matter what happens during the method execution.”
</p>
        <p>
And then an exception happens.  Even though you wrap your call in the using statement,
you see this weird exception about the channel being in the faulted state (or something
like that).  Wait – I wrapped my call in the using statement.  This should
work!  So you fire up <strike>Google</strike><a href="http://www.bing.com" target="_blank">Bing</a> and
quickly learn the WCF team made the decision to implement IDisposable <strike>wrong</strike> uniquely.  
</p>
        <p>
In order to get a WCF client to work as expected, you need to workaround the decision
made by the WCF team and wrap all your WCF calls in blocks similar to this one:
</p>
        <pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; background-color: rgb(251, 251, 251); min-height: 40px; width: 750px;">
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  1: var channelFactory = <span style="color: rgb(0, 0, 255);">new</span> ChannelFactory(binding,
endpointAddress); </pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  2: 
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  3: var proxy = channelFactory.CreateChannel&lt;ICustomerService&gt;();
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  4: IClientChannel clientChannel = (IClientChannel)proxy;
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  5: <span style="color: rgb(0, 0, 255);">bool</span> isSuccessful
= <span style="color: rgb(0, 0, 255);">false</span>; </pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  6: <span style="color: rgb(0, 0, 255);">try</span></pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  7: {
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  8:   proxy.AddCustomer(customer);
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  9:   clientChannel.Close();
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 10:   isSuccessful = <span style="color: rgb(0, 0, 255);">true</span>; </pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 11: }
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 12: <span style="color: rgb(0, 0, 255);">finally</span></pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 13: {
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 14:   <span style="color: rgb(0, 0, 255);">if</span> (
!isSuccessful ) </pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 15:   {
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 16:     clientChannel.Abort();
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 17:   }
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 18: }</pre>
        </pre>
        <p>
As you can see here, you must explicitly call Close() on the client channel unless
an Exception is thrown, in which case you need to call the Abort() method.
</p>
        <p>
This code doesn’t just emit a <a href="http://en.wikipedia.org/wiki/Code_smell" target="_blank">code
smell</a> of violating the <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" target="_blank">DRY</a> principle,
it absolutely reeks of it.  <a href="http://stevesmithblog.com/blog/idisposable-and-wcf/" target="_blank">Steve
Smith</a> (and others) have come up with extension/utility methods aimed at simplifying
the call and removing the repetitive boiler plate code from the above example. 
Inspired by these solutions, I’d thought I’d throw out my own answer to this problem:
</p>
        <pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; background-color: rgb(251, 251, 251); min-height: 40px; width: 750px;">
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  1: <span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 255);">class</span> WcfClient&lt;TInterface&gt; </pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  2: {
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  3:   <span style="color: rgb(0, 0, 255);">public</span> WcfClient(Uri
uri) : <span style="color: rgb(0, 0, 255);">this</span>(uri, <span style="color: rgb(0, 0, 255);">new</span> NetTcpBinding(SecurityMode.None)) </pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  4:   {
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  5:     Uri = uri;
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  6:   }
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  7:   
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  8:   <span style="color: rgb(0, 0, 255);">public</span> WcfClient(Uri
uri, Binding binding) </pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  9:   {
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 10:     <span style="color: rgb(0, 0, 255);">this</span>.Uri
= uri; </pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 11:     <span style="color: rgb(0, 0, 255);">this</span>.Binding
= binding; </pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 12:   }
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 13:   
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 14:   <span style="color: rgb(0, 0, 255);">public</span> Uri
Uri </pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 15:   {
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 16:     <span style="color: rgb(0, 0, 255);">get</span>; </pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 17:     <span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 255);">set</span>; </pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 18:   }
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 19:   
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 20:   <span style="color: rgb(0, 0, 255);">public</span> Binding
Binding </pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 21:   {
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 22:     <span style="color: rgb(0, 0, 255);">get</span></pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 23:     <span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 255);">set</span>; </pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 24:   }
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 25: 
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 26:   <span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 255);">void</span> Using(Action&lt;TInterface&gt;
serviceAction) </pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 27:   {
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 28:     TInterface proxy = CreateChannelFactory().CreateChannel();
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 29:     IClientChannel clientChannel = (IClientChannel)proxy;
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 30:     <span style="color: rgb(0, 0, 255);">bool</span> isSuccessful
= <span style="color: rgb(0, 0, 255);">false</span>; </pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 31:     <span style="color: rgb(0, 0, 255);">try</span></pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 32:     {
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 33:       serviceAction(proxy);
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 34:       clientChannel.Close();
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 35:       isSuccessful = <span style="color: rgb(0, 0, 255);">true</span>; </pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 36:     }
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 37:     <span style="color: rgb(0, 0, 255);">finally</span></pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 38:     {
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 39:       <span style="color: rgb(0, 0, 255);">if</span> (
!isSuccessful ) </pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 40:       {
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 41:         clientChannel.Abort();
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 42:       }
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 43:     }
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 44:   }
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 45: 
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 46:   <span style="color: rgb(0, 0, 255);">public</span> TResult
Using&lt;TResult&gt;(Func&lt;TInterface, TResult&gt; serviceAction) </pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 47:   {
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 48:     TResult result;
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 49:     TInterface proxy = CreateChannelFactory().CreateChannel();
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 50:     IClientChannel clientChannel = (IClientChannel)proxy;
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 51:     <span style="color: rgb(0, 0, 255);">bool</span> isSuccessful
= <span style="color: rgb(0, 0, 255);">false</span>; </pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 52:     <span style="color: rgb(0, 0, 255);">try</span></pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 53:     {
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 54:       result = serviceAction(proxy);
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 55:       clientChannel.Close();
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 56:       isSuccessful = <span style="color: rgb(0, 0, 255);">true</span>; </pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 57:     }
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 58:     <span style="color: rgb(0, 0, 255);">finally</span></pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 59:     {
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 60:       <span style="color: rgb(0, 0, 255);">if</span> (
!isSuccessful ) </pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 61:       {
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 62:         clientChannel.Abort();
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 63:       }
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 64:     }
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 65: 
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 66:     <span style="color: rgb(0, 0, 255);">return</span> result; </pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 67:   }
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 68:   
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 69:   <span style="color: rgb(0, 0, 255);">private</span> ChannelFactory<tinterface>
CreateChannelFactory() 
</tinterface></pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 70:   {
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 71:     var endpoint = <span style="color: rgb(0, 0, 255);">new</span> EndpointAddress(Uri); </pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 72:     <span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 255);">new</span> ChannelFactory<tinterface>
(Binding, endpoint); 
</tinterface></pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 73:   }
</pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 74: }</pre>
        </pre>
        <p>
The two Using() methods offer an overloaded way to invoke a method on the service
proxy.  In the first version, you can invoke an Action<t>
call which doesn’t expect a returned value.  In the second version, the TResult
generic type determines the type of value expected from the invocation of the proxy
object.  The return value will flow through from the proxy call execution to
the caller.  Think of these two methods as being akin to the differences between
a Sub or a Function in VB (my God, did I just admit to knowing VB?!?!?!).
</t></p>
        <p>
The WcfClient code is responsible for creating the client proxy and returns the proxy
back to the calling code.  As the caller, here is how you would execute the methods:
</p>
        <pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; background-color: rgb(251, 251, 251); min-height: 40px; width: 750px;">
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  1: <span style="color: rgb(0, 0, 255);">string</span> wcfUri
= ConfigurationManager.AppSettings["<span style="color: rgb(139, 0, 0);">wcfUri</span>"]; </pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  2: 
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  3: var wcfClient = <span style="color: rgb(0, 0, 255);">new</span> WcfClient&lt;ICustomerService&gt;(wcfUri); </pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  4: 
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  5: wcfClient.Using(proxy =&gt; proxy.AddCustomer(<span style="color: rgb(0, 0, 255);">new</span> Customer())); </pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  6: 
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  7: <span style="color: rgb(0, 128, 0);">//
...</span></pre>
          <pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  8: 
</pre>
          <pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  9: Customer[] customers = wcfClient.Using(proxy =&gt; proxy.GetCustomers());</pre>
        </pre>
        <p>
The nice thing about this solution is that we take care of the exception handling
and correctly clean up our resources in one place, and that logic is completely shielded
from the calling code.  Plus, we can account for situations where a return value
is expected versus when one is not.
</p>
        <img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=87864caa-72fb-4f07-a5bc-ff00ba91170c" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/ChrisRauber/~4/KWjzIw3-JxE" height="1" width="1" /></body>
      <title>WCF and IDisposable</title>
      <guid isPermaLink="false">http://www.chrisrauber.com/blog/PermaLink,guid,87864caa-72fb-4f07-a5bc-ff00ba91170c.aspx</guid>
      <link>http://www.chrisrauber.com/blog/2009/06/10/WCFAndIDisposable.aspx</link>
      <pubDate>Wed, 10 Jun 2009 04:02:10 GMT</pubDate>
      <description>&lt;p&gt;
If you’re like me (and apparently &lt;a href="http://www.bing.com/search?q=wcf+IDisposable" target="_blank"&gt;many
other people&lt;/a&gt;), when you start using (no pun intended) WCF by creating a client
channel, it’s only a matter of time before you realize that you’re probably doing
it wrong.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
Problem number one is that if you don’t explicitly close the connection, it’s only
a matter of time before the connection starts faulting.&amp;nbsp; So you say to yourself,
“Aha!&amp;nbsp; I need to close the damn connection.&amp;nbsp; And since I’m a good .Net programmer,
I see the IChannelListener interface extends the IDisposable interface so I can just
wrap my call in a ‘using’ statement which will explicitly clean up all my resources
no matter what happens during the method execution.”
&lt;/p&gt;
&lt;p&gt;
And then an exception happens.&amp;nbsp; Even though you wrap your call in the using statement,
you see this weird exception about the channel being in the faulted state (or something
like that).&amp;nbsp; Wait – I wrapped my call in the using statement.&amp;nbsp; This should
work!&amp;nbsp; So you fire up &lt;strike&gt;Google&lt;/strike&gt; &lt;a href="http://www.bing.com" target="_blank"&gt;Bing&lt;/a&gt; and
quickly learn the WCF team made the decision to implement IDisposable &lt;strike&gt;wrong&lt;/strike&gt; uniquely.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
In order to get a WCF client to work as expected, you need to workaround the decision
made by the WCF team and wrap all your WCF calls in blocks similar to this one:
&lt;/p&gt;
&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; background-color: rgb(251, 251, 251); min-height: 40px; width: 750px;"&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  1: var channelFactory = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; ChannelFactory(binding,
endpointAddress); &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  2: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  3: var proxy = channelFactory.CreateChannel&amp;lt;ICustomerService&amp;gt;();
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  4: IClientChannel clientChannel = (IClientChannel)proxy;
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  5: &lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt; isSuccessful
= &lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  6: &lt;span style="color: rgb(0, 0, 255);"&gt;try&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  7: {
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  8:   proxy.AddCustomer(customer);
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  9:   clientChannel.Close();
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 10:   isSuccessful = &lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 11: }
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 12: &lt;span style="color: rgb(0, 0, 255);"&gt;finally&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 13: {
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 14:   &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (
!isSuccessful ) &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 15:   {
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 16:     clientChannel.Abort();
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 17:   }
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 18: }&lt;/pre&gt;
&lt;/pre&gt;
&lt;p&gt;
As you can see here, you must explicitly call Close() on the client channel unless
an Exception is thrown, in which case you need to call the Abort() method.
&lt;/p&gt;
&lt;p&gt;
This code doesn’t just emit a &lt;a href="http://en.wikipedia.org/wiki/Code_smell" target="_blank"&gt;code
smell&lt;/a&gt; of violating the &lt;a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" target="_blank"&gt;DRY&lt;/a&gt; principle,
it absolutely reeks of it.&amp;nbsp; &lt;a href="http://stevesmithblog.com/blog/idisposable-and-wcf/" target="_blank"&gt;Steve
Smith&lt;/a&gt; (and others) have come up with extension/utility methods aimed at simplifying
the call and removing the repetitive boiler plate code from the above example.&amp;nbsp;
Inspired by these solutions, I’d thought I’d throw out my own answer to this problem:
&lt;/p&gt;
&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; background-color: rgb(251, 251, 251); min-height: 40px; width: 750px;"&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  1: &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; WcfClient&amp;lt;TInterface&amp;gt; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  2: {
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  3:   &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; WcfClient(Uri
uri) : &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;(uri, &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; NetTcpBinding(SecurityMode.None)) &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  4:   {
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  5:     Uri = uri;
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  6:   }
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  7:   
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  8:   &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; WcfClient(Uri
uri, Binding binding) &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  9:   {
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 10:     &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.Uri
= uri; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 11:     &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.Binding
= binding; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 12:   }
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 13:   
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 14:   &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; Uri
Uri &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 15:   {
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 16:     &lt;span style="color: rgb(0, 0, 255);"&gt;get&lt;/span&gt;; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 17:     &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;set&lt;/span&gt;; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 18:   }
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 19:   
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 20:   &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; Binding
Binding &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 21:   {
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 22:     &lt;span style="color: rgb(0, 0, 255);"&gt;get&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 23:     &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;set&lt;/span&gt;; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 24:   }
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 25: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 26:   &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Using(Action&amp;lt;TInterface&amp;gt;
serviceAction) &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 27:   {
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 28:     TInterface proxy = CreateChannelFactory().CreateChannel();
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 29:     IClientChannel clientChannel = (IClientChannel)proxy;
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 30:     &lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt; isSuccessful
= &lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 31:     &lt;span style="color: rgb(0, 0, 255);"&gt;try&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 32:     {
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 33:       serviceAction(proxy);
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 34:       clientChannel.Close();
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 35:       isSuccessful = &lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 36:     }
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 37:     &lt;span style="color: rgb(0, 0, 255);"&gt;finally&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 38:     {
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 39:       &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (
!isSuccessful ) &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 40:       {
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 41:         clientChannel.Abort();
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 42:       }
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 43:     }
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 44:   }
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 45: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 46:   &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; TResult
Using&amp;lt;TResult&amp;gt;(Func&amp;lt;TInterface, TResult&amp;gt; serviceAction) &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 47:   {
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 48:     TResult result;
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 49:     TInterface proxy = CreateChannelFactory().CreateChannel();
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 50:     IClientChannel clientChannel = (IClientChannel)proxy;
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 51:     &lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt; isSuccessful
= &lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 52:     &lt;span style="color: rgb(0, 0, 255);"&gt;try&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 53:     {
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 54:       result = serviceAction(proxy);
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 55:       clientChannel.Close();
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 56:       isSuccessful = &lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 57:     }
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 58:     &lt;span style="color: rgb(0, 0, 255);"&gt;finally&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 59:     {
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 60:       &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (
!isSuccessful ) &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 61:       {
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 62:         clientChannel.Abort();
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 63:       }
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 64:     }
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 65: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 66:     &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; result; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 67:   }
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 68:   
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 69:   &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; ChannelFactory&lt;tinterface&gt;
CreateChannelFactory() 
&lt;/tinterface&gt;
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 70:   {
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 71:     var endpoint = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; EndpointAddress(Uri); &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 72:     &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; ChannelFactory&lt;tinterface&gt;
(Binding, endpoint); 
&lt;/tinterface&gt;
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 73:   }
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt; 74: }&lt;/pre&gt;
&lt;/pre&gt;
&lt;p&gt;
The two Using() methods offer an overloaded way to invoke a method on the service
proxy.&amp;nbsp; In the first version, you can invoke an Action&lt;t&gt;
call which doesn’t expect a returned value.&amp;nbsp; In the second version, the TResult
generic type determines the type of value expected from the invocation of the proxy
object.&amp;nbsp; The return value will flow through from the proxy call execution to
the caller.&amp;nbsp; Think of these two methods as being akin to the differences between
a Sub or a Function in VB (my God, did I just admit to knowing VB?!?!?!).
&lt;/t&gt;
&lt;/p&gt;
&lt;p&gt;
The WcfClient code is responsible for creating the client proxy and returns the proxy
back to the calling code.&amp;nbsp; As the caller, here is how you would execute the methods:
&lt;/p&gt;
&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; background-color: rgb(251, 251, 251); min-height: 40px; width: 750px;"&gt;&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  1: &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; wcfUri
= ConfigurationManager.AppSettings["&lt;span style="color: rgb(139, 0, 0);"&gt;wcfUri&lt;/span&gt;"]; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  2: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  3: var wcfClient = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; WcfClient&amp;lt;ICustomerService&amp;gt;(wcfUri); &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  4: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  5: wcfClient.Using(proxy =&amp;gt; proxy.AddCustomer(&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Customer())); &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  6: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  7: &lt;span style="color: rgb(0, 128, 0);"&gt;//
...&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(255, 255, 255); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  8: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; background-color: rgb(251, 251, 251); width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"&gt;  9: Customer[] customers = wcfClient.Using(proxy =&amp;gt; proxy.GetCustomers());&lt;/pre&gt;
&lt;/pre&gt;
&lt;p&gt;
The nice thing about this solution is that we take care of the exception handling
and correctly clean up our resources in one place, and that logic is completely shielded
from the calling code.&amp;nbsp; Plus, we can account for situations where a return value
is expected versus when one is not.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=87864caa-72fb-4f07-a5bc-ff00ba91170c" /&gt;</description>
      <comments>http://www.chrisrauber.com/blog/CommentView,guid,87864caa-72fb-4f07-a5bc-ff00ba91170c.aspx</comments>
      <category>.Net</category>
      <category>C#</category>
      <category>WCF</category>
    </item>
    <item>
      <trackback:ping>http://www.chrisrauber.com/blog/Trackback.aspx?guid=ccfc08cb-55f6-4f97-91c7-ecebe80b44be</trackback:ping>
      <pingback:server>http://www.chrisrauber.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.chrisrauber.com/blog/PermaLink,guid,ccfc08cb-55f6-4f97-91c7-ecebe80b44be.aspx</pingback:target>
      <dc:creator>Chris Rauber</dc:creator>
      <wfw:comment>http://www.chrisrauber.com/blog/CommentView,guid,ccfc08cb-55f6-4f97-91c7-ecebe80b44be.aspx</wfw:comment>
      <wfw:commentRss>http://www.chrisrauber.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=ccfc08cb-55f6-4f97-91c7-ecebe80b44be</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In my last post, I discussed how to use DataSet objects with Miado.  In this
post, I will tackle a common problem – building dynamic queries.
</p>
        <h5>ISqlQuery Interface
</h5>
        <p>
I have created an ISqlQuery interface that can be used to run dynamic queries. 
The interface is quite simple.  It exposes three properties – IsBuilt, Parameters,
and Sql – and one method – Build().  
</p>
        <pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 750px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">public
interface</span> ISqlQuery </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: {
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3:     <span style="color: #0000ff">bool</span> IsBuilt
{ <span style="color: #0000ff">get</span>; } </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4:     <span style="color: #0000ff">string</span> Sql
{ <span style="color: #0000ff">get</span>; } </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5:     IDictionary&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">object</span>&gt;
Parameters { <span style="color: #0000ff">get</span>; } </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6:     <span style="color: #0000ff">void</span> Build(); </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7: }    </pre>
        </pre>
        <p>
The idea behind this design is for the Sql property represents the SQL that will eventually
be run against the database.  It can be built up dynamically based on the filtering
that is applied to the object.  The same goes for the Parameters property. 
It represents the DB parameters that will be bound to the Sql statement.  Parameters
can be added to this property in the same way they are added to a IDbStatement. 
By doing so, you can still safely use parameterized queries (as opposed to dynamically
substituting the variables straight in the SQL string – i.e. String.Format(“and Name=’{0}’”,
name), which exposes a risk for <a href="http://en.wikipedia.org/wiki/SQL_injection" target="_blank">SQL
Injection attacks</a>).
</p>
        <p>
The Build() method should be the place where you put your logic to dynamically create
the SQL and populate the parameters.  
</p>
        <pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 750px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">public
class</span> CustomerQueryByCityAndState : ISqlQuery </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: {
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3:     <span style="color: #0000ff">private</span><span style="color: #0000ff">const</span><span style="color: #0000ff">string</span> SELECT_CUSTOMERS_BASE_SQL
= </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4:         "<span style="color: #8b0000">select
CustomerID, Name, Address1, Address2, City, County, </span>" + </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5:         "<span style="color: #8b0000">StateCode,
ZipCode </span>" + </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6:         "<span style="color: #8b0000">from
Customers </span>" + </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7:         "<span style="color: #8b0000">where
1=1 </span>"; <span style="color: #008000">// etc...</span></pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8: 
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  9:     <span style="color: #0000ff">public</span> CustomerQueryByCityAndState(<span style="color: #0000ff">string</span> city, <span style="color: #0000ff">string</span> state) </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10:     {
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11:         City = city;
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12:         State = state;
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 13:         Parameters = <span style="color: #0000ff">new</span> Dictionary&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">object</span>&gt;(); </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 14:     }
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 15: 
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 16:     <span style="color: #0000ff">public</span><span style="color: #0000ff">string</span> City
{ <span style="color: #0000ff">get</span>; <span style="color: #0000ff">private</span><span style="color: #0000ff">set</span>;
} </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 17:     <span style="color: #0000ff">public</span><span style="color: #0000ff">string</span> State
{ <span style="color: #0000ff">get</span>; <span style="color: #0000ff">private</span><span style="color: #0000ff">set</span>;
} </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 18: 
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 19:     <span style="color: #0000ff">public</span><span style="color: #0000ff">bool</span> IsBuilt
{ <span style="color: #0000ff">get</span>; <span style="color: #0000ff">private</span><span style="color: #0000ff">set</span>;
} </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 20:     <span style="color: #0000ff">public</span><span style="color: #0000ff">string</span> Sql
{ <span style="color: #0000ff">get</span>; <span style="color: #0000ff">private</span><span style="color: #0000ff">set</span>;
} </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 21:     <span style="color: #0000ff">public</span> IDictionary&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">object</span>&gt;
Parameters { <span style="color: #0000ff">get</span>; <span style="color: #0000ff">private</span><span style="color: #0000ff">set</span>;
} </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 22: 
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 23:     <span style="color: #0000ff">public</span><span style="color: #0000ff">void</span> Build() </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 24:     {
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 25:         var sb = <span style="color: #0000ff">new</span> StringBuilder(); </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 26:         sb.Append(SELECT_CUSTOMERS_BASE_SQL);
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 27: 
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 28:         <span style="color: #008000">//
add City</span></pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 29:         sb.Append("<span style="color: #8b0000"> and
City = @City </span>"); </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 30:         Parameters.Add("<span style="color: #8b0000">City</span>",
City); </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 31: 
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 32:         <span style="color: #008000">//
add State</span></pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 33:         sb.Append("<span style="color: #8b0000"> and
StateCode = @State </span>"); </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 34:         Parameters.Add("<span style="color: #8b0000">State</span>",
State); </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 35: 
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 36:         <span style="color: #008000">//
Populate the SQL</span></pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 37:         Sql = sb.ToString();
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 38:     
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 39:         <span style="color: #008000">//
info</span></pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 40:         IsBuilt = <span style="color: #0000ff">true</span>; </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 41:     }
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 42: }</pre>
        </pre>
        <p>
Though it’s not actually being for used anything now, the last step of the Build()
method should flip the IsBuilt property to True.  This is purely for information
purposes at this point, but it could be informative to the calling class.
</p>
        <h5>SqlQuery Abstract Class
</h5>
        <p>
To simplify things, I have created a base class you can use that implements the ISqlQuery
interface.  It manages the Sql and the Parameters, and will automatically flip
the IsBuilt property when the Build() method is called.  The only thing a derived
class must do is implement the DoBuild() method, which is a template method to actual
perform the logic that was previously implemented in Build().  It should contain
the logic of dynamically creating the SQL and Parameters.
</p>
        <pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 750px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">protected</span><span style="color: #0000ff">abstract</span><span style="color: #0000ff">void</span> DoBuild(); </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: 
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3: <span style="color: #0000ff">public</span><span style="color: #0000ff">virtual</span><span style="color: #0000ff">void</span> Build() </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4: {
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5:     DoBuild();
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6:     IsBuilt = <span style="color: #0000ff">true</span>; </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7: }</pre>
        </pre>
        <p>
So the previous example above becomes much simpler…
</p>
        <pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 750px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">public
class</span> CustomerQueryByCityAndState : SqlQuery </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: {
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3:     <span style="color: #0000ff">private</span><span style="color: #0000ff">const</span><span style="color: #0000ff">string</span> SELECT_CUSTOMERS_BASE_SQL
= ... </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4: 
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5:     <span style="color: #0000ff">public</span> CustomerQueryByCityAndState(<span style="color: #0000ff">string</span> city, <span style="color: #0000ff">string</span> state) </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6:     {
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7:        City = city;  
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8:        State = state;
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  9:     }
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10: 
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11:     <span style="color: #0000ff">public</span><span style="color: #0000ff">string</span> City
{ <span style="color: #0000ff">get</span>; <span style="color: #0000ff">private</span><span style="color: #0000ff">set</span>;
} </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12:     <span style="color: #0000ff">public</span><span style="color: #0000ff">string</span> State
{ <span style="color: #0000ff">get</span>; <span style="color: #0000ff">private</span><span style="color: #0000ff">set</span>;
} </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 13: 
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 14:     <span style="color: #0000ff">public</span><span style="color: #0000ff">override</span><span style="color: #0000ff">void</span> DoBuild() </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 15:     {
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 16:         var sb = <span style="color: #0000ff">new</span> StringBuilder(); </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 17:         sb.Append(SELECT_CUSTOMERS_BASE_SQL);
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 18:    
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 19:         sb.Append("<span style="color: #8b0000"> and
City = @City </span>"); </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 20:         Parameters.Add("<span style="color: #8b0000">City</span>",
City); </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 21: 
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 22:         sb.Append("<span style="color: #8b0000"> and
State = @State </span>"); </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 23:         Parameters.Add("<span style="color: #8b0000">State</span>",
State); </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 24: 
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 25:         Sql = sb.ToString();
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 26:     }
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 27: }</pre>
        </pre>
        <p>
        </p>
        <p>
The disadvantage is that inheriting from this class binds you into an inheritance
hierarchy, which may not be a good thing (see <a href="http://www.chrisrauber.com/blog/2008/09/25/FavorCompositionOverInheritance.aspx" target="_blank">inheritance
vs. composition</a>).  If you need to maintain a pure inheritance hierarchy,
use the interface instead, or hide the implementation of this class in an internal
Property.
</p>
        <h5>Filter Objects
</h5>
        <p>
In many cases, you probably expose a common lightweight object to the UI layer for
representing a “filter” to the database.  In reality, this is nothing more than
a simple <a href="http://en.wikipedia.org/wiki/Data_Transfer_Object" target="_blank">Data
Transfer Object (DTO)</a> that has properties representing all the ways the data can
be filtered.
</p>
        <p>
        </p>
        <pre>
        </pre>
        <p>
A common pattern I use is to take a Filter DTO object into the Query object’s constructor
and build the SQL from it dynamically:
</p>
        <pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 750px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">public</span><span style="color: #0000ff">class</span> SiteLocatorQuery
: SqlQuery </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: {
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3:     <span style="color: #0000ff">private</span><span style="color: #0000ff">const</span><span style="color: #0000ff">string</span> SELECT_SITES_SQL
= .. </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4: 
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5:     <span style="color: #0000ff">public</span> SiteLocatorQuery(SiteLocatorFilter
filter) </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6:     {
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7:         Filter = filter;
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8:     }
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  9: 
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10:     <span style="color: #0000ff">public</span> SiteLocatorFilter
Filter { <span style="color: #0000ff">get</span>; <span style="color: #0000ff">private</span><span style="color: #0000ff">set</span>;
} </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11: 
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12:     <span style="color: #0000ff">public</span><span style="color: #0000ff">override</span><span style="color: #0000ff">void</span> DoBuild() </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 13:     {
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 14:         var sb = <span style="color: #0000ff">new</span> StringBuilder(); </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 15:         sb.Append(SELECT_SITES_SQL);
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 16:       
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 17:         <span style="color: #0000ff">if</span> (
Filter.IsRecent ) </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 18:         { 
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 19:            sb.Append("<span style="color: #8b0000"> and
IsRecent = @IsRecent </span>"); </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 20:            Parameters.Add("<span style="color: #8b0000">IsRecent</span>",
1); </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 21:         }
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 22:         <span style="color: #0000ff">if</span> (
Filter.BrandID &gt; 0 ) </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 23:         {
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 24:            sb.Append("<span style="color: #8b0000"> and
BrandID = @BrandID </span>"); </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 25:            Parameters.Add("<span style="color: #8b0000">BrandID</span>",
filter.BrandID); </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 26:         }
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 27: 
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 28:         <span style="color: #008000">//
etc...</span></pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 29: 
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 30:         Sql = sb.ToString();
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 31:     }    
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 32: }</pre>
        </pre>
        <p>
This has the advantage of hiding the complexity of how the SQL is built up (or the
fact that we are using raw SQL at all) from the UI layer.  I normally isolate
my “Query” objects to a namespace in my MyCompany.Data dll, requiring a reference
to the Filter objects (and not the other way around).  Plus, the light-weight
DTOs can be used to pass data in and out of the UI layer very easily, as in the case
of using messaging or web services.
</p>
        <h5>So I’ve got my ISqlQuery interface coded … how do I use it?
</h5>
        <p>
Once you have a reference to the IDatabase interface, you can simply call the RunningQuery(ISqlQuery
query) method to get a reference to an IDbStatement interface.  The method will
call the Build() method on your interface and use the Sql and Parameters properties
to populate the IDbStatement appropriately.  Once you get the IDbStatement back,
you can then proceed just as you would have otherwise.
</p>
        <pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 750px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">public</span> IEnumerable&lt;Customer&gt;
FindCustomersByFilter(CustomerFilter filter) </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: {
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3:     var query = <span style="color: #0000ff">new</span> CustomerQuery(filter); </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4:     <span style="color: #0000ff">this</span>.Database </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5:         .RunningQuery(query)
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6:         .QueryForResults(row =&gt; <span style="color: #0000ff">new</span> Customer()); <span style="color: #008000">//
row mapping omitted</span></pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7: }</pre>
        </pre>
        <img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=ccfc08cb-55f6-4f97-91c7-ecebe80b44be" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/ChrisRauber/~4/uZk6WQlnfpI" height="1" width="1" /></body>
      <title>Using Miado &amp;ndash; Part 6</title>
      <guid isPermaLink="false">http://www.chrisrauber.com/blog/PermaLink,guid,ccfc08cb-55f6-4f97-91c7-ecebe80b44be.aspx</guid>
      <link>http://www.chrisrauber.com/blog/2009/05/17/UsingMiadoNdashPart6.aspx</link>
      <pubDate>Sun, 17 May 2009 03:13:47 GMT</pubDate>
      <description>&lt;p&gt;
In my last post, I discussed how to use DataSet objects with Miado.&amp;nbsp; In this
post, I will tackle a common problem – building dynamic queries.
&lt;/p&gt;
&lt;h5&gt;ISqlQuery Interface
&lt;/h5&gt;
&lt;p&gt;
I have created an ISqlQuery interface that can be used to run dynamic queries.&amp;nbsp;
The interface is quite simple.&amp;nbsp; It exposes three properties – IsBuilt, Parameters,
and Sql – and one method – Build().&amp;nbsp; 
&lt;/p&gt;
&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 750px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  1: &lt;span style="color: #0000ff"&gt;public
interface&lt;/span&gt; ISqlQuery &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  2: {
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  3:     &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; IsBuilt
{ &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; } &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  4:     &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Sql
{ &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; } &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  5:     IDictionary&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&amp;gt;
Parameters { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; } &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  6:     &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Build(); &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  7: }    &lt;/pre&gt;
&lt;/pre&gt;
&lt;p&gt;
The idea behind this design is for the Sql property represents the SQL that will eventually
be run against the database.&amp;nbsp; It can be built up dynamically based on the filtering
that is applied to the object.&amp;nbsp; The same goes for the Parameters property.&amp;nbsp;
It represents the DB parameters that will be bound to the Sql statement.&amp;nbsp; Parameters
can be added to this property in the same way they are added to a IDbStatement.&amp;nbsp;
By doing so, you can still safely use parameterized queries (as opposed to dynamically
substituting the variables straight in the SQL string – i.e. String.Format(“and Name=’{0}’”,
name), which exposes a risk for &lt;a href="http://en.wikipedia.org/wiki/SQL_injection" target="_blank"&gt;SQL
Injection attacks&lt;/a&gt;).
&lt;/p&gt;
&lt;p&gt;
The Build() method should be the place where you put your logic to dynamically create
the SQL and populate the parameters.&amp;nbsp; 
&lt;/p&gt;
&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 750px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  1: &lt;span style="color: #0000ff"&gt;public
class&lt;/span&gt; CustomerQueryByCityAndState : ISqlQuery &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  2: {
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  3:     &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;const&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; SELECT_CUSTOMERS_BASE_SQL
= &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  4:         "&lt;span style="color: #8b0000"&gt;select
CustomerID, Name, Address1, Address2, City, County, &lt;/span&gt;" + &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  5:         "&lt;span style="color: #8b0000"&gt;StateCode,
ZipCode &lt;/span&gt;" + &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  6:         "&lt;span style="color: #8b0000"&gt;from
Customers &lt;/span&gt;" + &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  7:         "&lt;span style="color: #8b0000"&gt;where
1=1 &lt;/span&gt;"; &lt;span style="color: #008000"&gt;// etc...&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  8: 
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  9:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; CustomerQueryByCityAndState(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; city, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; state) &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 10:     {
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 11:         City = city;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 12:         State = state;
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 13:         Parameters = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&amp;gt;(); &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 14:     }
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 15: 
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 16:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; City
{ &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;;
} &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 17:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; State
{ &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;;
} &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 18: 
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 19:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; IsBuilt
{ &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;;
} &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 20:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Sql
{ &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;;
} &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 21:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IDictionary&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&amp;gt;
Parameters { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;;
} &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 22: 
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 23:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Build() &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 24:     {
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 25:         var sb = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; StringBuilder(); &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 26:         sb.Append(SELECT_CUSTOMERS_BASE_SQL);
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 27: 
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 28:         &lt;span style="color: #008000"&gt;//
add City&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 29:         sb.Append("&lt;span style="color: #8b0000"&gt; and
City = @City &lt;/span&gt;"); &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 30:         Parameters.Add("&lt;span style="color: #8b0000"&gt;City&lt;/span&gt;",
City); &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 31: 
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 32:         &lt;span style="color: #008000"&gt;//
add State&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 33:         sb.Append("&lt;span style="color: #8b0000"&gt; and
StateCode = @State &lt;/span&gt;"); &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 34:         Parameters.Add("&lt;span style="color: #8b0000"&gt;State&lt;/span&gt;",
State); &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 35: 
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 36:         &lt;span style="color: #008000"&gt;//
Populate the SQL&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 37:         Sql = sb.ToString();
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 38:     
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 39:         &lt;span style="color: #008000"&gt;//
info&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 40:         IsBuilt = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;; &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 41:     }
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 42: }&lt;/pre&gt;
&lt;/pre&gt;
&lt;p&gt;
Though it’s not actually being for used anything now, the last step of the Build()
method should flip the IsBuilt property to True.&amp;nbsp; This is purely for information
purposes at this point, but it could be informative to the calling class.
&lt;/p&gt;
&lt;h5&gt;SqlQuery Abstract Class
&lt;/h5&gt;
&lt;p&gt;
To simplify things, I have created a base class you can use that implements the ISqlQuery
interface.&amp;nbsp; It manages the Sql and the Parameters, and will automatically flip
the IsBuilt property when the Build() method is called.&amp;nbsp; The only thing a derived
class must do is implement the DoBuild() method, which is a template method to actual
perform the logic that was previously implemented in Build().&amp;nbsp; It should contain
the logic of dynamically creating the SQL and Parameters.
&lt;/p&gt;
&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 750px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  1: &lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;abstract&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; DoBuild(); &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  2: 
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  3: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;virtual&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Build() &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  4: {
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  5:     DoBuild();
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  6:     IsBuilt = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;; &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  7: }&lt;/pre&gt;
&lt;/pre&gt;
&lt;p&gt;
So the previous example above becomes much simpler…
&lt;/p&gt;
&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 750px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  1: &lt;span style="color: #0000ff"&gt;public
class&lt;/span&gt; CustomerQueryByCityAndState : SqlQuery &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  2: {
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  3:     &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;const&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; SELECT_CUSTOMERS_BASE_SQL
= ... &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  4: 
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  5:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; CustomerQueryByCityAndState(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; city, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; state) &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  6:     {
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  7:        City = city;  
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  8:        State = state;
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  9:     }
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 10: 
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 11:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; City
{ &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;;
} &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 12:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; State
{ &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;;
} &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 13: 
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 14:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; DoBuild() &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 15:     {
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 16:         var sb = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; StringBuilder(); &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 17:         sb.Append(SELECT_CUSTOMERS_BASE_SQL);
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 18:    
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 19:         sb.Append("&lt;span style="color: #8b0000"&gt; and
City = @City &lt;/span&gt;"); &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 20:         Parameters.Add("&lt;span style="color: #8b0000"&gt;City&lt;/span&gt;",
City); &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 21: 
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 22:         sb.Append("&lt;span style="color: #8b0000"&gt; and
State = @State &lt;/span&gt;"); &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 23:         Parameters.Add("&lt;span style="color: #8b0000"&gt;State&lt;/span&gt;",
State); &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 24: 
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 25:         Sql = sb.ToString();
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 26:     }
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 27: }&lt;/pre&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
The disadvantage is that inheriting from this class binds you into an inheritance
hierarchy, which may not be a good thing (see &lt;a href="http://www.chrisrauber.com/blog/2008/09/25/FavorCompositionOverInheritance.aspx" target="_blank"&gt;inheritance
vs. composition&lt;/a&gt;).&amp;nbsp; If you need to maintain a pure inheritance hierarchy,
use the interface instead, or hide the implementation of this class in an internal
Property.
&lt;/p&gt;
&lt;h5&gt;Filter Objects
&lt;/h5&gt;
&lt;p&gt;
In many cases, you probably expose a common lightweight object to the UI layer for
representing a “filter” to the database.&amp;nbsp; In reality, this is nothing more than
a simple &lt;a href="http://en.wikipedia.org/wiki/Data_Transfer_Object" target="_blank"&gt;Data
Transfer Object (DTO)&lt;/a&gt; that has properties representing all the ways the data can
be filtered.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;p&gt;
A common pattern I use is to take a Filter DTO object into the Query object’s constructor
and build the SQL from it dynamically:
&lt;/p&gt;
&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 750px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  1: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; SiteLocatorQuery
: SqlQuery &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  2: {
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  3:     &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;const&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; SELECT_SITES_SQL
= .. &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  4: 
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  5:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; SiteLocatorQuery(SiteLocatorFilter
filter) &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  6:     {
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  7:         Filter = filter;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  8:     }
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  9: 
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 10:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; SiteLocatorFilter
Filter { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;;
} &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 11: 
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 12:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; DoBuild() &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 13:     {
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 14:         var sb = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; StringBuilder(); &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 15:         sb.Append(SELECT_SITES_SQL);
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 16:       
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 17:         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (
Filter.IsRecent ) &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 18:         { 
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 19:            sb.Append("&lt;span style="color: #8b0000"&gt; and
IsRecent = @IsRecent &lt;/span&gt;"); &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 20:            Parameters.Add("&lt;span style="color: #8b0000"&gt;IsRecent&lt;/span&gt;",
1); &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 21:         }
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 22:         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (
Filter.BrandID &amp;gt; 0 ) &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 23:         {
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 24:            sb.Append("&lt;span style="color: #8b0000"&gt; and
BrandID = @BrandID &lt;/span&gt;"); &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 25:            Parameters.Add("&lt;span style="color: #8b0000"&gt;BrandID&lt;/span&gt;",
filter.BrandID); &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 26:         }
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 27: 
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 28:         &lt;span style="color: #008000"&gt;//
etc...&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 29: 
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 30:         Sql = sb.ToString();
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 31:     }    
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt; 32: }&lt;/pre&gt;
&lt;/pre&gt;
&lt;p&gt;
This has the advantage of hiding the complexity of how the SQL is built up (or the
fact that we are using raw SQL at all) from the UI layer.&amp;nbsp; I normally isolate
my “Query” objects to a namespace in my MyCompany.Data dll, requiring a reference
to the Filter objects (and not the other way around).&amp;nbsp; Plus, the light-weight
DTOs can be used to pass data in and out of the UI layer very easily, as in the case
of using messaging or web services.
&lt;/p&gt;
&lt;h5&gt;So I’ve got my ISqlQuery interface coded … how do I use it?
&lt;/h5&gt;
&lt;p&gt;
Once you have a reference to the IDatabase interface, you can simply call the RunningQuery(ISqlQuery
query) method to get a reference to an IDbStatement interface.&amp;nbsp; The method will
call the Build() method on your interface and use the Sql and Parameters properties
to populate the IDbStatement appropriately.&amp;nbsp; Once you get the IDbStatement back,
you can then proceed just as you would have otherwise.
&lt;/p&gt;
&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 750px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  1: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IEnumerable&amp;lt;Customer&amp;gt;
FindCustomersByFilter(CustomerFilter filter) &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  2: {
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  3:     var query = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; CustomerQuery(filter); &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  4:     &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Database &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  5:         .RunningQuery(query)
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  6:         .QueryForResults(row =&amp;gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Customer()); &lt;span style="color: #008000"&gt;//
row mapping omitted&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  7: }&lt;/pre&gt;
&lt;/pre&gt;
&lt;img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=ccfc08cb-55f6-4f97-91c7-ecebe80b44be" /&gt;</description>
      <comments>http://www.chrisrauber.com/blog/CommentView,guid,ccfc08cb-55f6-4f97-91c7-ecebe80b44be.aspx</comments>
      <category>Miado</category>
    </item>
    <item>
      <trackback:ping>http://www.chrisrauber.com/blog/Trackback.aspx?guid=ee9720a2-9a2f-40c3-b532-a5083507d376</trackback:ping>
      <pingback:server>http://www.chrisrauber.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.chrisrauber.com/blog/PermaLink,guid,ee9720a2-9a2f-40c3-b532-a5083507d376.aspx</pingback:target>
      <dc:creator>Chris Rauber</dc:creator>
      <wfw:comment>http://www.chrisrauber.com/blog/CommentView,guid,ee9720a2-9a2f-40c3-b532-a5083507d376.aspx</wfw:comment>
      <wfw:commentRss>http://www.chrisrauber.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=ee9720a2-9a2f-40c3-b532-a5083507d376</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Better late than never, eh?  Due to a crazy series of events in my life (both
personally and professionally), I’ve had to put blogging on a hold for a short time. 
Well, I’m back baby!  Today I’m going to continue my series on the latest <a href="http://miado.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=23278" target="_blank">Miado</a> release.
</p>
        <p>
In this post, I will discuss the use of <a href="http://msdn.microsoft.com/en-us/library/system.data.dataset.aspx" target="_blank">DataSet</a> (both
generic and typed) with Miado.
</p>
        <h5>Plain ol’ DataSet
</h5>
        <p>
If you really <em>want </em>to use a DataSet object, it’s dead-simple easy with Miado. 
It’s just like using the Miado API for the IDbStatement, except that you don’t need
to map the Result Set to a custom business object.  You just need to call the
QueryForDataSet() method on the IDbStatement.
</p>
        <pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 750px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">public</span> DataSet
FindBlogEntriesByBlog(<span style="color: #0000ff">int</span> blogID) </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: {
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3:     <span style="color: #0000ff">return</span></pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4:         <span style="color: #0000ff">this</span>.Database </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5:             .ExecutingSql(SELECT_BLOG_ENTRIES_BY_BLOG_ID_SQL)
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6:             .AddParameter("<span style="color: #8b0000">BlogID</span>",
blogID) </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7:             .QueryForDataSet();
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8: }</pre>
        </pre>
        <h5>Typed DataSets
</h5>
        <p>
If you weren’t aware, Typed DataSet classes were introduced in 2.0 version of the
.Net framework to compensate for some of the shortcomings of the original DataSet
object.  Typed DataSets inherit from the DataSet class, but allow for strong
typing on the rows and columns contained in the DataSet.  So instead of having
to access the column values in the rows through a Dictionary key lookup, the column
values now are exposed as strongly-typed properties on the Typed DataSet object itself.
</p>
        <p>
On the QueryForDataSet() method detailed above, Miado exposes an optional generic
typed version of the method that let’s you define the type of DataSet that will be
returned.  QueryForDataSet&lt;T&gt;() can be used to return a strongly-typed
DataSet object since T is constrained to be a subclass of DataSet.
</p>
        <pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 750px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">public</span> BlogEntryDataSet
FindBlogEntriesByBlog(<span style="color: #0000ff">int</span> blogID) </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: {
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3:     <span style="color: #0000ff">return</span></pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4:         <span style="color: #0000ff">this</span>.Database </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5:             .ExecutingSql(SELECT_BLOG_ENTRIES_BY_BLOG_ID)
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6:             .AddParameter("<span style="color: #8b0000">BlogID</span>",
blogID) </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7:             .QueryForDataSet&lt;BlogEntryDataSet&gt;();
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8: }</pre>
        </pre>
        <h5>DataTable
</h5>
        <p>
Not surprisingly, you can also retrieve a DataTable from the IDbStatement instead
of a DataSet if you are only interested in a single table’s result set.  Simply
call QueryForDataTable() instead of QueryForDataSet().
</p>
        <pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 750px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">public</span> DataTable
FindBlogEntriesByBlog(<span style="color: #0000ff">int</span> blogID) </pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: {
</pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3:     <span style="color: #0000ff">return</span></pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4:         <span style="color: #0000ff">this</span>.Database </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5:             .ExecutingSql(SELECT_BLOG_ENTRIES_BY_BLOG_ID_SQL)
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6:             .AddParameter("<span style="color: #8b0000">BlogID</span>",
blogID) </pre>
          <pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7:             .QueryForDataTable();
</pre>
          <pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8: }</pre>
        </pre>
        <h5>Thoughts on DataSet
</h5>
        <p>
I consider myself to be <a href="http://altnetpedia.com/Default.aspx?AspxAutoDetectCookieSupport=1" target="_blank">Alt.Net</a>,
so by law I’m required to tell you <em><strong>not</strong></em> to use DataSet. 
It’s funny though – I like to play devil’s advocate a lot and ask the question as
to why we don’t use it more often.  I’m just as guilty as the next person in
creating my share of dumb <a href="http://en.wikipedia.org/wiki/Data_Transfer_Object" target="_blank">Data
Transfer Objects (DTO</a>s) that directly represent database tables.  In doing
so, I am forced to manually map the columns in the result set to properties on the
DTO.  Why not skip that effort and just let the DataSet (typed or regular) handle
that?  It’s already built into the framework after all.  And if you’ve ever
played with <a href="http://rubyonrails.org/" target="_blank">Rails</a> – isn’t an <a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html" target="_blank">ActiveRecord</a> object
effectively the same thing as a Typed DataSet???
</p>
        <p>
I guess it boils down to control.  I don’t like letting go of the classes I create
and can easily refactor and manipulate without having to employ or depending on tooling
to do so.  It’s a personal choice.  I don’t think I’d turn my nose down
at a developer who chooses to use DataSets.  That being said, I can’t remember
the last time I’ve done so.
</p>
        <h5>Coming up next!
</h5>
        <p>
In my next post (whenever that may be), I will discuss how you can use Miado to build
dynamic queries.
</p>
        <img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=ee9720a2-9a2f-40c3-b532-a5083507d376" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/ChrisRauber/~4/xan1boeCVqA" height="1" width="1" /></body>
      <title>Using Miado &amp;ndash; Part 5</title>
      <guid isPermaLink="false">http://www.chrisrauber.com/blog/PermaLink,guid,ee9720a2-9a2f-40c3-b532-a5083507d376.aspx</guid>
      <link>http://www.chrisrauber.com/blog/2009/05/02/UsingMiadoNdashPart5.aspx</link>
      <pubDate>Sat, 02 May 2009 03:22:07 GMT</pubDate>
      <description>&lt;p&gt;
Better late than never, eh?&amp;nbsp; Due to a crazy series of events in my life (both
personally and professionally), I’ve had to put blogging on a hold for a short time.&amp;nbsp;
Well, I’m back baby!&amp;nbsp; Today I’m going to continue my series on the latest &lt;a href="http://miado.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=23278" target="_blank"&gt;Miado&lt;/a&gt; release.
&lt;/p&gt;
&lt;p&gt;
In this post, I will discuss the use of &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.dataset.aspx" target="_blank"&gt;DataSet&lt;/a&gt; (both
generic and typed) with Miado.
&lt;/p&gt;
&lt;h5&gt;Plain ol’ DataSet
&lt;/h5&gt;
&lt;p&gt;
If you really &lt;em&gt;want &lt;/em&gt;to use a DataSet object, it’s dead-simple easy with Miado.&amp;nbsp;
It’s just like using the Miado API for the IDbStatement, except that you don’t need
to map the Result Set to a custom business object.&amp;nbsp; You just need to call the
QueryForDataSet() method on the IDbStatement.
&lt;/p&gt;
&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 750px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  1: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; DataSet
FindBlogEntriesByBlog(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; blogID) &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  2: {
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  3:     &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  4:         &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Database &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  5:             .ExecutingSql(SELECT_BLOG_ENTRIES_BY_BLOG_ID_SQL)
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  6:             .AddParameter("&lt;span style="color: #8b0000"&gt;BlogID&lt;/span&gt;",
blogID) &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  7:             .QueryForDataSet();
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  8: }&lt;/pre&gt;
&lt;/pre&gt;
&lt;h5&gt;Typed DataSets
&lt;/h5&gt;
&lt;p&gt;
If you weren’t aware, Typed DataSet classes were introduced in 2.0 version of the
.Net framework to compensate for some of the shortcomings of the original DataSet
object.&amp;nbsp; Typed DataSets inherit from the DataSet class, but allow for strong
typing on the rows and columns contained in the DataSet.&amp;nbsp; So instead of having
to access the column values in the rows through a Dictionary key lookup, the column
values now are exposed as strongly-typed properties on the Typed DataSet object itself.
&lt;/p&gt;
&lt;p&gt;
On the QueryForDataSet() method detailed above, Miado exposes an optional generic
typed version of the method that let’s you define the type of DataSet that will be
returned.&amp;nbsp; QueryForDataSet&amp;lt;T&amp;gt;() can be used to return a strongly-typed
DataSet object since T is constrained to be a subclass of DataSet.
&lt;/p&gt;
&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 750px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  1: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; BlogEntryDataSet
FindBlogEntriesByBlog(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; blogID) &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  2: {
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  3:     &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  4:         &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Database &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  5:             .ExecutingSql(SELECT_BLOG_ENTRIES_BY_BLOG_ID)
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  6:             .AddParameter("&lt;span style="color: #8b0000"&gt;BlogID&lt;/span&gt;",
blogID) &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  7:             .QueryForDataSet&amp;lt;BlogEntryDataSet&amp;gt;();
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  8: }&lt;/pre&gt;
&lt;/pre&gt;
&lt;h5&gt;DataTable
&lt;/h5&gt;
&lt;p&gt;
Not surprisingly, you can also retrieve a DataTable from the IDbStatement instead
of a DataSet if you are only interested in a single table’s result set.&amp;nbsp; Simply
call QueryForDataTable() instead of QueryForDataSet().
&lt;/p&gt;
&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 750px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  1: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; DataTable
FindBlogEntriesByBlog(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; blogID) &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  2: {
&lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  3:     &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  4:         &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Database &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  5:             .ExecutingSql(SELECT_BLOG_ENTRIES_BY_BLOG_ID_SQL)
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  6:             .AddParameter("&lt;span style="color: #8b0000"&gt;BlogID&lt;/span&gt;",
blogID) &lt;/pre&gt;
&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  7:             .QueryForDataTable();
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"&gt;  8: }&lt;/pre&gt;
&lt;/pre&gt;
&lt;h5&gt;Thoughts on DataSet
&lt;/h5&gt;
&lt;p&gt;
I consider myself to be &lt;a href="http://altnetpedia.com/Default.aspx?AspxAutoDetectCookieSupport=1" target="_blank"&gt;Alt.Net&lt;/a&gt;,
so by law I’m required to tell you &lt;em&gt;&lt;strong&gt;not&lt;/strong&gt;&lt;/em&gt; to use DataSet.&amp;nbsp;
It’s funny though – I like to play devil’s advocate a lot and ask the question as
to why we don’t use it more often.&amp;nbsp; I’m just as guilty as the next person in
creating my share of dumb &lt;a href="http://en.wikipedia.org/wiki/Data_Transfer_Object" target="_blank"&gt;Data
Transfer Objects (DTO&lt;/a&gt;s) that directly represent database tables.&amp;nbsp; In doing
so, I am forced to manually map the columns in the result set to properties on the
DTO.&amp;nbsp; Why not skip that effort and just let the DataSet (typed or regular) handle
that?&amp;nbsp; It’s already built into the framework after all.&amp;nbsp; And if you’ve ever
played with &lt;a href="http://rubyonrails.org/" target="_blank"&gt;Rails&lt;/a&gt; – isn’t an &lt;a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html" target="_blank"&gt;ActiveRecord&lt;/a&gt; object
effectively the same thing as a Typed DataSet???
&lt;/p&gt;
&lt;p&gt;
I guess it boils down to control.&amp;nbsp; I don’t like letting go of the classes I create
and can easily refactor and manipulate without having to employ or depending on tooling
to do so.&amp;nbsp; It’s a personal choice.&amp;nbsp; I don’t think I’d turn my nose down
at a developer who chooses to use DataSets.&amp;nbsp; That being said, I can’t remember
the last time I’ve done so.
&lt;/p&gt;
&lt;h5&gt;Coming up next!
&lt;/h5&gt;
&lt;p&gt;
In my next post (whenever that may be), I will discuss how you can use Miado to build
dynamic queries.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=ee9720a2-9a2f-40c3-b532-a5083507d376" /&gt;</description>
      <comments>http://www.chrisrauber.com/blog/CommentView,guid,ee9720a2-9a2f-40c3-b532-a5083507d376.aspx</comments>
      <category>Miado</category>
    </item>
    <item>
      <trackback:ping>http://www.chrisrauber.com/blog/Trackback.aspx?guid=d6882220-c0ef-4efa-95a0-b888ceac36a3</trackback:ping>
      <pingback:server>http://www.chrisrauber.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.chrisrauber.com/blog/PermaLink,guid,d6882220-c0ef-4efa-95a0-b888ceac36a3.aspx</pingback:target>
      <dc:creator>Chris Rauber</dc:creator>
      <wfw:comment>http://www.chrisrauber.com/blog/CommentView,guid,d6882220-c0ef-4efa-95a0-b888ceac36a3.aspx</wfw:comment>
      <wfw:commentRss>http://www.chrisrauber.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=d6882220-c0ef-4efa-95a0-b888ceac36a3</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This is part 4 in a series about the latest release of <a href="http://miado.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=23278" target="_blank">Miado</a>. 
In my <a href="http://www.chrisrauber.com/blog/2009/02/17/UsingMiadoNdashPart3.aspx" target="_blank">last
post</a>, I discussed about creating an IDbStatement from the IDatabase interface. 
In this post, I will start to discuss the main methods exposed by the IDbStatement
interface.
</p>
        <h5>Adding Parameters
</h5>
        <p>
Adding parameters to the IDbStatement is really easy.  It is accomplished simply
by calling the AddParameter() method with a name/value pair.  The name should
match the parameter name declared in the SQL statement and the value is what will
be substituted during the query execution.  Optionally, you can also set the
DBType and/or parameter direction.  If the DBType is not set, it will be inferred
by the DbProvider implementation based the type of the parameter value.  The
parameter direction is assumed to be an input parameter unless explicitly declared. 
This routine should be very familiar to anyone who has ever done traditional ADO.Net
development.
</p>
        <p>
Like most other methods on the IDbStatement interface, the AddParameter() method returns
a reference to itself so it can be chained with other method calls on the IDbStatement
interface.  Example:
</p>
        <pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 750px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb">
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  1: <span style="color: #0000ff">private</span><span style="color: #0000ff">static</span><span style="color: #0000ff">readonly</span><span style="color: #0000ff">string</span> INSERT_ADDRESS_SQL
= </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  2:     "<span style="color: #8b0000">insert
into Address(Address1, Address2, City, State, ZipCode) </span>" + </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  3:     "<span style="color: #8b0000">values(@Addr1,
@Addr2, @City, @State, @ZipCode)</span>"; </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  4: 
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  5: <span style="color: #0000ff">public</span><span style="color: #0000ff">void</span> SaveAddress(Address
addr) </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  6: {
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  7:     <span style="color: #0000ff">this</span>.Database </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  8:         .ExecutingSql(INSERT_ADDRESS_SQL)
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  9:         .AddParameter("<span style="color: #8b0000">Addr1</span>",
addr.Address1) </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 10:         .AddParameter("<span style="color: #8b0000">Addr2</span>",
addr.Address2) </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 11:         .AddParameter("<span style="color: #8b0000">City</span>",
addr.City, DbType.String) <span style="color: #008000">// explicitly set DbType</span></pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 12:         .AddParameter("<span style="color: #8b0000">State</span>",
addr.State) </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 13:         .AddParameter("<span style="color: #8b0000">ZipCode</span>",
addr.ZipCode) </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 14:         .ExecuteNonQuery();
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 15: }</pre>
        </pre>
        <p>
If you really want total control over the parameter, you can get a reference to the
actual DbParameter object using the Action&lt;DbParameter&gt; method signature of
AddParameter().
</p>
        <pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 750px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb">
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  1: <span style="color: #0000ff">public</span><span style="color: #0000ff">void</span> UpdateUser(User
user) </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  2: {
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  3:     <span style="color: #0000ff">this</span>.Database </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  4:         .ExecutingSql(UPDATE_USER_SQL)
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  5:         .AddParameter(dbParm =&gt; 
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  6:         {
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  7:             dbParm.ParameterName = "<span style="color: #8b0000">Email</span>"; </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  8:             dbParm.Value = user.Email;
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  9:         }) <span style="color: #008000">//
AddParameter(Action&lt;DbParameter&gt; dbParmAction)</span></pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 10:         .AddParameter(dbParm =&gt; 
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 11:         { 
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 12:             dbParm.ParameterName = "<span style="color: #8b0000">UserID</span>"; </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 13:             dbParm.Value = user.UserID;
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 14:             dbParm.DbType = DbType.Int32;
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 15:             dbParm.ParameterDirection.Input;
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 16:         }) <span style="color: #008000">//
dbParm is a DbParameter</span></pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 17:         .ExecuteNonQuery();
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 18: }</pre>
        </pre>
        <h5>Insert/Update/Delete
</h5>
        <p>
Anytime you want to execute a SQL statement that does not return a result set, you
can call the ExecuteNonQuery() method (you probably already realized this fact from
previous examples).  I’ve thought about adding explicit Save(), Update(), and
Delete() methods to the API for clarity sake, but since I’m not providing an ORM,
these methods would only be information and could not be guaranteed to perform their
exact intended action.  In other words, they would merely wrap the ExecuteNonQuery()
method, so you could call the Save() method to in fact execute a “delete from User
where UserID = @ID” SQL statement with no problems.  I would be interested to
see what other people thought about this change in the comments.  Does the readability
of Save(), Update(), and Delete() outweigh the need to validate whether the right
one is called?  Should there be validation against the raw SQL when one of these
methods is called (e.g. explicitly checking the SQL string for “INSERT” when Save()
is called)?  The validation method option is the route I’m leaning.
</p>
        <h5>Creating Custom Business Objects
</h5>
        <p>
When you call QueryForResults() or QueryForOne(), you must use a generic type to declare
the type of object that will be created from each row in the result set.  Your
job will be to create an object for each row as the cursor moves forward in each iteration
by mapping the values from the columns in the result set row.  Most of the time,
this will be a custom business object.  However, it could be something as simple
as an int if you are just returning a count(*).  Both these method types take
a delegate that passes in an IResultSetRow interface (which merely wraps a DbDataReader)
and return an instance of the generic type you declared.  The IResultSetRow interface
has both typed  - using generics GetValue&lt;T&gt;() - and non-typed - GetValue()
- methods that can be used to retrieve values using ordinals or by column name.
</p>
        <p>
If your return type is simple or will not likely be re-used, you can use a lambda
expression to create the returned object.
</p>
        <pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 750px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb">
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  1: <span style="color: #0000ff">private</span><span style="color: #0000ff">static</span><span style="color: #0000ff">readonly</span> SELECT_USER_COUNT_SQL
= "<span style="color: #8b0000">select count(*) from Users</span>"; </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  2: 
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  3: <span style="color: #0000ff">public</span><span style="color: #0000ff">int</span> FindUserCount() </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  4: {
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  5:     <span style="color: #0000ff">return</span></pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  6:         <span style="color: #0000ff">this</span>.Database </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  7:             .ExecutingSql(SELECT_USER_COUNT_SQL)
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  8:             .QueryForOne&lt;<span style="color: #0000ff">int</span>&gt;(row
=&gt; row.GetValue&lt;<span style="color: #0000ff">int</span>&gt;(0)); </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  9: }</pre>
        </pre>
        <p>
Alternatively, you can create a true delegate method that satisfies the signature
required by the QueryForOne() and QueryForResults() methods and pass a reference to
the delegate.
</p>
        <pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 750px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb">
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  1: <span style="color: #0000ff">public</span> IEnumerable&lt;BlogEntry&gt;
FindBlogEntriesByBlog(<span style="color: #0000ff">int</span> blogID) </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  2: {
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  3:     <span style="color: #0000ff">return</span></pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  4:         <span style="color: #0000ff">this</span>.Database </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  5:             .ExecutingSql(SELECT_BLOG_ENTRIES_BY_BLOG_ID_SQL)
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  6:             .AddParameter("<span style="color: #8b0000">BlogID</span>",
blogID) </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  7:             .QueryForResults&lt;BlogEntry&gt;(CreateBlogEntry);
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  8: }
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  9: 
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 10: <span style="color: #0000ff">private</span> BlogEntry
CreateBlogEntry(IResultSetRow row) </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 11: {
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 12:     <span style="color: #0000ff">return</span><span style="color: #0000ff">new</span> BlogEntry </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 13:     {
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 14:         BlogEntryID = row.GetValue&lt;<span style="color: #0000ff">int</span>&gt;("<span style="color: #8b0000">BlogEntryID</span>"), </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 15:         Text = row.GetValue&lt;<span style="color: #0000ff">string</span>&gt;("<span style="color: #8b0000">BlogText</span>") </pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 16:         <span style="color: #008000">//
rest of mapping ommitted</span></pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 17:     };
</pre>
          <pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 18: }</pre>
        </pre>
        <p>
In my next post, I will describe how to use the IDbStatement to return our old friend
DataSet.
</p>
        <img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=d6882220-c0ef-4efa-95a0-b888ceac36a3" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/ChrisRauber/~4/L8finRXc67A" height="1" width="1" /></body>
      <title>Using Miado &amp;ndash; Part 4</title>
      <guid isPermaLink="false">http://www.chrisrauber.com/blog/PermaLink,guid,d6882220-c0ef-4efa-95a0-b888ceac36a3.aspx</guid>
      <link>http://www.chrisrauber.com/blog/2009/03/01/UsingMiadoNdashPart4.aspx</link>
      <pubDate>Sun, 01 Mar 2009 22:29:02 GMT</pubDate>
      <description>&lt;p&gt;
This is part 4 in a series about the latest release of &lt;a href="http://miado.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=23278" target="_blank"&gt;Miado&lt;/a&gt;.&amp;nbsp;
In my &lt;a href="http://www.chrisrauber.com/blog/2009/02/17/UsingMiadoNdashPart3.aspx" target="_blank"&gt;last
post&lt;/a&gt;, I discussed about creating an IDbStatement from the IDatabase interface.&amp;nbsp;
In this post, I will start to discuss the main methods exposed by the IDbStatement
interface.
&lt;/p&gt;
&lt;h5&gt;Adding Parameters
&lt;/h5&gt;
&lt;p&gt;
Adding parameters to the IDbStatement is really easy.&amp;nbsp; It is accomplished simply
by calling the AddParameter() method with a name/value pair.&amp;nbsp; The name should
match the parameter name declared in the SQL statement and the value is what will
be substituted during the query execution.&amp;nbsp; Optionally, you can also set the
DBType and/or parameter direction.&amp;nbsp; If the DBType is not set, it will be inferred
by the DbProvider implementation based the type of the parameter value.&amp;nbsp; The
parameter direction is assumed to be an input parameter unless explicitly declared.&amp;nbsp;
This routine should be very familiar to anyone who has ever done traditional ADO.Net
development.
&lt;/p&gt;
&lt;p&gt;
Like most other methods on the IDbStatement interface, the AddParameter() method returns
a reference to itself so it can be chained with other method calls on the IDbStatement
interface.&amp;nbsp; Example:
&lt;/p&gt;
&lt;pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 750px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb"&gt;&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  1: &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; INSERT_ADDRESS_SQL
= &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt;  2:     "&lt;span style="color: #8b0000"&gt;insert
into Address(Address1, Address2, City, State, ZipCode) &lt;/span&gt;" + &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  3:     "&lt;span style="color: #8b0000"&gt;values(@Addr1,
@Addr2, @City, @State, @ZipCode)&lt;/span&gt;"; &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt;  4: 
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  5: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SaveAddress(Address
addr) &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt;  6: {
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  7:     &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Database &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt;  8:         .ExecutingSql(INSERT_ADDRESS_SQL)
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  9:         .AddParameter("&lt;span style="color: #8b0000"&gt;Addr1&lt;/span&gt;",
addr.Address1) &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt; 10:         .AddParameter("&lt;span style="color: #8b0000"&gt;Addr2&lt;/span&gt;",
addr.Address2) &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt; 11:         .AddParameter("&lt;span style="color: #8b0000"&gt;City&lt;/span&gt;",
addr.City, DbType.String) &lt;span style="color: #008000"&gt;// explicitly set DbType&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt; 12:         .AddParameter("&lt;span style="color: #8b0000"&gt;State&lt;/span&gt;",
addr.State) &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt; 13:         .AddParameter("&lt;span style="color: #8b0000"&gt;ZipCode&lt;/span&gt;",
addr.ZipCode) &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt; 14:         .ExecuteNonQuery();
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt; 15: }&lt;/pre&gt;
&lt;/pre&gt;
&lt;p&gt;
If you really want total control over the parameter, you can get a reference to the
actual DbParameter object using the Action&amp;lt;DbParameter&amp;gt; method signature of
AddParameter().
&lt;/p&gt;
&lt;pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 750px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb"&gt;&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  1: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; UpdateUser(User
user) &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt;  2: {
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  3:     &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Database &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt;  4:         .ExecutingSql(UPDATE_USER_SQL)
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  5:         .AddParameter(dbParm =&amp;gt; 
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt;  6:         {
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  7:             dbParm.ParameterName = "&lt;span style="color: #8b0000"&gt;Email&lt;/span&gt;"; &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt;  8:             dbParm.Value = user.Email;
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  9:         }) &lt;span style="color: #008000"&gt;//
AddParameter(Action&amp;lt;DbParameter&amp;gt; dbParmAction)&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt; 10:         .AddParameter(dbParm =&amp;gt; 
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt; 11:         { 
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt; 12:             dbParm.ParameterName = "&lt;span style="color: #8b0000"&gt;UserID&lt;/span&gt;"; &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt; 13:             dbParm.Value = user.UserID;
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt; 14:             dbParm.DbType = DbType.Int32;
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt; 15:             dbParm.ParameterDirection.Input;
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt; 16:         }) &lt;span style="color: #008000"&gt;//
dbParm is a DbParameter&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt; 17:         .ExecuteNonQuery();
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt; 18: }&lt;/pre&gt;
&lt;/pre&gt;
&lt;h5&gt;Insert/Update/Delete
&lt;/h5&gt;
&lt;p&gt;
Anytime you want to execute a SQL statement that does not return a result set, you
can call the ExecuteNonQuery() method (you probably already realized this fact from
previous examples).&amp;nbsp; I’ve thought about adding explicit Save(), Update(), and
Delete() methods to the API for clarity sake, but since I’m not providing an ORM,
these methods would only be information and could not be guaranteed to perform their
exact intended action.&amp;nbsp; In other words, they would merely wrap the ExecuteNonQuery()
method, so you could call the Save() method to in fact execute a “delete from User
where UserID = @ID” SQL statement with no problems.&amp;nbsp; I would be interested to
see what other people thought about this change in the comments.&amp;nbsp; Does the readability
of Save(), Update(), and Delete() outweigh the need to validate whether the right
one is called?&amp;nbsp; Should there be validation against the raw SQL when one of these
methods is called (e.g. explicitly checking the SQL string for “INSERT” when Save()
is called)?&amp;nbsp; The validation method option is the route I’m leaning.
&lt;/p&gt;
&lt;h5&gt;Creating Custom Business Objects
&lt;/h5&gt;
&lt;p&gt;
When you call QueryForResults() or QueryForOne(), you must use a generic type to declare
the type of object that will be created from each row in the result set.&amp;nbsp; Your
job will be to create an object for each row as the cursor moves forward in each iteration
by mapping the values from the columns in the result set row.&amp;nbsp; Most of the time,
this will be a custom business object.&amp;nbsp; However, it could be something as simple
as an int if you are just returning a count(*).&amp;nbsp; Both these method types take
a delegate that passes in an IResultSetRow interface (which merely wraps a DbDataReader)
and return an instance of the generic type you declared.&amp;nbsp; The IResultSetRow interface
has both typed&amp;nbsp; - using generics GetValue&amp;lt;T&amp;gt;() - and non-typed - GetValue()
- methods that can be used to retrieve values using ordinals or by column name.
&lt;/p&gt;
&lt;p&gt;
If your return type is simple or will not likely be re-used, you can use a lambda
expression to create the returned object.
&lt;/p&gt;
&lt;pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 750px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb"&gt;&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  1: &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt; SELECT_USER_COUNT_SQL
= "&lt;span style="color: #8b0000"&gt;select count(*) from Users&lt;/span&gt;"; &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt;  2: 
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  3: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; FindUserCount() &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt;  4: {
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  5:     &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt;  6:         &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Database &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  7:             .ExecutingSql(SELECT_USER_COUNT_SQL)
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt;  8:             .QueryForOne&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt;(row
=&amp;gt; row.GetValue&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt;(0)); &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  9: }&lt;/pre&gt;
&lt;/pre&gt;
&lt;p&gt;
Alternatively, you can create a true delegate method that satisfies the signature
required by the QueryForOne() and QueryForResults() methods and pass a reference to
the delegate.
&lt;/p&gt;
&lt;pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 750px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb"&gt;&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  1: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IEnumerable&amp;lt;BlogEntry&amp;gt;
FindBlogEntriesByBlog(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; blogID) &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt;  2: {
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  3:     &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt;  4:         &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Database &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  5:             .ExecutingSql(SELECT_BLOG_ENTRIES_BY_BLOG_ID_SQL)
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt;  6:             .AddParameter("&lt;span style="color: #8b0000"&gt;BlogID&lt;/span&gt;",
blogID) &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  7:             .QueryForResults&amp;lt;BlogEntry&amp;gt;(CreateBlogEntry);
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt;  8: }
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt;  9: 
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt; 10: &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; BlogEntry
CreateBlogEntry(IResultSetRow row) &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt; 11: {
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt; 12:     &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; BlogEntry &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt; 13:     {
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt; 14:         BlogEntryID = row.GetValue&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt;("&lt;span style="color: #8b0000"&gt;BlogEntryID&lt;/span&gt;"), &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt; 15:         Text = row.GetValue&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;("&lt;span style="color: #8b0000"&gt;BlogText&lt;/span&gt;") &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt; 16:         &lt;span style="color: #008000"&gt;//
rest of mapping ommitted&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"&gt; 17:     };
&lt;/pre&gt;
&lt;pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"&gt; 18: }&lt;/pre&gt;
&lt;/pre&gt;
&lt;p&gt;
In my next post, I will describe how to use the IDbStatement to return our old friend
DataSet.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=d6882220-c0ef-4efa-95a0-b888ceac36a3" /&gt;</description>
      <comments>http://www.chrisrauber.com/blog/CommentView,guid,d6882220-c0ef-4efa-95a0-b888ceac36a3.aspx</comments>
      <category>Miado</category>
    </item>
    <item>
      <trackback:ping>http://www.chrisrauber.com/blog/Trackback.aspx?guid=9b602341-2e1a-4ba2-aac9-8681f4d778c2</trackback:ping>
      <pingback:server>http://www.chrisrauber.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.chrisrauber.com/blog/PermaLink,guid,9b602341-2e1a-4ba2-aac9-8681f4d778c2.aspx</pingback:target>
      <dc:creator>Chris Rauber</dc:creator>
      <wfw:comment>http://www.chrisrauber.com/blog/CommentView,guid,9b602341-2e1a-4ba2-aac9-8681f4d778c2.aspx</wfw:comment>
      <wfw:commentRss>http://www.chrisrauber.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=9b602341-2e1a-4ba2-aac9-8681f4d778c2</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This is part 3 in a series about the <a href="http://www.codeplex.com/miado/Release/ProjectReleases.aspx?ReleaseId=23278" target="_blank">new
release of Miado</a>.  If you want, you can go back and see <a href="http://www.chrisrauber.com/blog/2009/02/15/UsingMiadoNdashPart1.aspx" target="_blank">Part
1</a> and <a href="http://www.chrisrauber.com/blog/2009/02/16/UsingMiadoPart2.aspx" target="_blank">Part
2</a> where I talk about creating an IDatabase reference.  In this part, I will
discuss how to use the IDatabase to create an IDbStatement.
</p>
        <h5>IDbStatement
</h5>
        <p>
The IDbStatement interface is the main working component of the Miado API.  You
need to use this interface to run raw SQL or to execute a stored procedure. 
Once you get a handle on the interface (from the IDatabase interface), you can begin
“building” the statement that will be run against the DB.  Most of the methods
on the interface return a reference to itself, so the method calls can be chained
to form a fluent-type interface.
</p>
        <h5>Executing SQL
</h5>
        <p>
Most of the time, I use raw SQL to execute my DB statements.  The ExecutingSql()
method on the IDatabase interface returns an IDbStatement reference.
</p>
        <pre style="BORDER-RIGHT: rgb(206,206,206) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(206,206,206) 1px solid; PADDING-LEFT: 5px; MIN-HEIGHT: 40px; PADDING-BOTTOM: 5px; OVERFLOW: auto; BORDER-LEFT: rgb(206,206,206) 1px solid; WIDTH: 750px; PADDING-TOP: 5px; BORDER-BOTTOM: rgb(206,206,206) 1px solid; BACKGROUND-COLOR: rgb(251,251,251)">
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)">  1: <span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,255)">class</span> ProductRepository
: IProductRepository </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)">  2: {
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)">  3:     <span style="COLOR: rgb(0,0,255)">private</span><span style="COLOR: rgb(0,0,255)">static</span><span style="COLOR: rgb(0,0,255)">readonly</span><span style="COLOR: rgb(0,0,255)">string</span> SELECT_PRODUCT_BY_ID
= </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)">  4:         "<span style="COLOR: rgb(139,0,0)">select
ProductID, Name, ProductNumber, Color, ListPrice, ProductModel </span>" + </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)">  5:         "<span style="COLOR: rgb(139,0,0)">from
Products </span>" + </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)">  6:         "<span style="COLOR: rgb(139,0,0)">where
ProductID = @ID</span>"; </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)">  7: 
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)">  8:     <span style="COLOR: rgb(0,128,0)">//
more SQL </span></pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)">  9: 
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 10:     <span style="COLOR: rgb(0,0,255)">public</span> ProductRepository(IDatabase
db) </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 11:     {
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 12:         <span style="COLOR: rgb(0,0,255)">this</span>.Database
= db; </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 13:     }
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 14: 
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 15:     <span style="COLOR: rgb(0,0,255)">public</span> IDatabase
Database </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 16:     {
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 17:         <span style="COLOR: rgb(0,0,255)">get</span>; </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 18:         <span style="COLOR: rgb(0,0,255)">private</span><span style="COLOR: rgb(0,0,255)">set</span>; </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 19:     }
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 20: 
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 21:     <span style="COLOR: rgb(0,0,255)">public</span> Product
Load(<span style="COLOR: rgb(0,0,255)">int</span> productID) </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 22:     {
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 23:         IDbStatement dbStmt = <span style="COLOR: rgb(0,0,255)">this</span>.Database.ExecutingSql(SELECT_PRODUCT_BY_ID); </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 24:         dbStmt.AddParameter("<span style="COLOR: rgb(139,0,0)">ID</span>",
productID); </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 25:         <span style="COLOR: rgb(0,0,255)">return</span> dbStmt.QueryForOne&lt;Product&gt;(CreateProduct); </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 26:     }
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 27: 
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 28:     <span style="COLOR: rgb(0,128,0)">//
more CRUD methods</span></pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 29: 
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 30:     <span style="COLOR: rgb(0,0,255)">private</span> Product
CreateProduct(IResultSetRow row) </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 31:     {
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 32:         <span style="COLOR: rgb(0,128,0)">//
implementation omitted...</span></pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 33:     }
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 34: } </pre>
        </pre>
        <p>
As I said before, since the methods on the IDbStatement return themselves, you can
chain the calls.  In line 21-26, you could change the Load() method to read like
this:
</p>
        <pre style="BORDER-RIGHT: rgb(206,206,206) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(206,206,206) 1px solid; PADDING-LEFT: 5px; MIN-HEIGHT: 40px; PADDING-BOTTOM: 5px; OVERFLOW: auto; BORDER-LEFT: rgb(206,206,206) 1px solid; WIDTH: 750px; PADDING-TOP: 5px; BORDER-BOTTOM: rgb(206,206,206) 1px solid; BACKGROUND-COLOR: rgb(251,251,251)">
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)">  1: <span style="COLOR: rgb(0,0,255)">public</span> Product
Load(<span style="COLOR: rgb(0,0,255)">int</span> productID) </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)">  2: {
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)">  3:     <span style="COLOR: rgb(0,0,255)">return</span></pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)">  4:         <span style="COLOR: rgb(0,0,255)">this</span>.Database </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)">  5:             .ExecutingSql(SELECT_PRODUCT_BY_ID)
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)">  6:             .AddParameter("<span style="COLOR: rgb(139,0,0)">ID</span>",
productID) </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)">  7:             .QueryForOne&lt;Product&gt;(CreateProduct);
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)">  8: }</pre>
        </pre>
        <h5>Stored Procedures
</h5>
        <p>
Stored Procedures are built up in a similar fashion, except you need to call the CallingStoredProcedureNamed()
method to load the SP.
</p>
        <pre style="BORDER-RIGHT: rgb(206,206,206) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(206,206,206) 1px solid; PADDING-LEFT: 5px; MIN-HEIGHT: 40px; PADDING-BOTTOM: 5px; OVERFLOW: auto; BORDER-LEFT: rgb(206,206,206) 1px solid; WIDTH: 750px; PADDING-TOP: 5px; BORDER-BOTTOM: rgb(206,206,206) 1px solid; BACKGROUND-COLOR: rgb(251,251,251)">
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)">  1: <span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,255)">class</span> EmployeeRepository
: IEmployeeRepository </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)">  2: {
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)">  3:     <span style="COLOR: rgb(0,0,255)">public</span> EmployeeRepository(IDatabase
db) </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)">  4:     { 
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)">  5:         <span style="COLOR: rgb(0,0,255)">this</span>.Database
= db; </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)">  6:     }
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)">  7:  
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)">  8:     <span style="COLOR: rgb(0,0,255)">public</span> IDatabase
Database </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)">  9:     {
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 10:         <span style="COLOR: rgb(0,0,255)">get</span>; </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 11:         <span style="COLOR: rgb(0,0,255)">private</span><span style="COLOR: rgb(0,0,255)">set</span>; </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 12:     }
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 13: 
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 14:     <span style="COLOR: rgb(0,0,255)">public</span> IEnumerable&lt;Employee&gt;
FindEmployeesByManager(<span style="COLOR: rgb(0,0,255)">int</span> mgrID) </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 15:     {
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 16:        <span style="COLOR: rgb(0,0,255)">return</span></pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 17:           <span style="COLOR: rgb(0,0,255)">this</span>.Database </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 18:               .CallingStoredProcedureNamed("<span style="COLOR: rgb(139,0,0)">uspGetManagerEmployees</span>") </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 19:               .AddParameter("<span style="COLOR: rgb(139,0,0)">ManagerID</span>",
mgrID) </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 20:               .QueryForResults&lt;Employee&gt;(CreateEmployee);
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 21:     }
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 22: 
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 23:     <span style="COLOR: rgb(0,0,255)">private</span> Employee
CreateEmployee(IResultSetRow row) </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 24:     {
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 25:         <span style="COLOR: rgb(0,128,0)">//
implementation omitted</span></pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 26:     }
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 27: }</pre>
        </pre>
        <h5>Registered Queries
</h5>
        <p>
I showed in <a href="http://www.chrisrauber.com/blog/2009/02/16/UsingMiadoPart2.aspx">my
last post</a> how you can use the IDatabase interface to “register” queries. 
You can easily retrieve them in the IDbStatement interface by using LoadQueryRegisteredAs()
method, which will load the corresponding SQL.
</p>
        <pre style="BORDER-RIGHT: rgb(206,206,206) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(206,206,206) 1px solid; PADDING-LEFT: 5px; MIN-HEIGHT: 40px; PADDING-BOTTOM: 5px; OVERFLOW: auto; BORDER-LEFT: rgb(206,206,206) 1px solid; WIDTH: 750px; PADDING-TOP: 5px; BORDER-BOTTOM: rgb(206,206,206) 1px solid; BACKGROUND-COLOR: rgb(251,251,251)">
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)">  1: <span style="COLOR: rgb(0,0,255)">public</span> IEnumerable&lt;State&gt;
FindAllStates() </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)">  2: {
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)">  3:     <span style="COLOR: rgb(0,0,255)">return</span></pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)">  4:         <span style="COLOR: rgb(0,0,255)">this</span>.Database </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)">  5:             .LoadQueryRegisteredAs("<span style="COLOR: rgb(139,0,0)">SelectStates</span>") </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)">  6:             .QueryForResults&lt;State&gt;(
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)">  7:                 row =&gt; <span style="COLOR: rgb(0,0,255)">new</span> State </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)">  8:                 { 
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)">  9:                     Abbreviation = row.GetValue&lt;<span style="COLOR: rgb(0,0,255)">string</span>&gt;("<span style="COLOR: rgb(139,0,0)">StateCode</span>"), </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 10:                     Name = row.GetValue&lt;<span style="COLOR: rgb(0,0,255)">string</span>&gt;("<span style="COLOR: rgb(139,0,0)">Name</span>") </pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"> 11:                 });
</pre>
          <pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"> 12: }                </pre>
        </pre>
        <h5>Running Custom Queries
</h5>
        <p>
There is one more way to “load” a query in the IDbStatement interface, and that is
by calling the RunningQuery() method, and passing in an object that implements the
ISqlQuery interface.  That is new functionality to this release and probably
deserves its own blog post, so I will cover that later.
</p>
        <p>
In my <a href="http://www.chrisrauber.com/blog/2009/03/01/UsingMiadoNdashPart4.aspx">next
post</a>, I will discuss how the IDbStatement executes and discuss the different kinds
of return types available, and how you can create custom business objects from the
query result set.
</p>
        <img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=9b602341-2e1a-4ba2-aac9-8681f4d778c2" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/ChrisRauber/~4/TdQ1nDMQ4rM" height="1" width="1" /></body>
      <title>Using Miado &amp;ndash; Part 3</title>
      <guid isPermaLink="false">http://www.chrisrauber.com/blog/PermaLink,guid,9b602341-2e1a-4ba2-aac9-8681f4d778c2.aspx</guid>
      <link>http://www.chrisrauber.com/blog/2009/02/17/UsingMiadoNdashPart3.aspx</link>
      <pubDate>Tue, 17 Feb 2009 21:40:52 GMT</pubDate>
      <description>&lt;p&gt;
This is part 3 in a series about the &lt;a href="http://www.codeplex.com/miado/Release/ProjectReleases.aspx?ReleaseId=23278" target=_blank&gt;new
release of Miado&lt;/a&gt;.&amp;nbsp; If you want, you can go back and see &lt;a href="http://www.chrisrauber.com/blog/2009/02/15/UsingMiadoNdashPart1.aspx" target=_blank&gt;Part
1&lt;/a&gt; and &lt;a href="http://www.chrisrauber.com/blog/2009/02/16/UsingMiadoPart2.aspx" target=_blank&gt;Part
2&lt;/a&gt; where I talk about creating an IDatabase reference.&amp;nbsp; In this part, I will
discuss how to use the IDatabase to create an IDbStatement.
&lt;/p&gt;
&lt;h5&gt;IDbStatement
&lt;/h5&gt;
&lt;p&gt;
The IDbStatement interface is the main working component of the Miado API.&amp;nbsp; You
need to use this interface to run raw SQL or to execute a stored procedure.&amp;nbsp;
Once you get a handle on the interface (from the IDatabase interface), you can begin
“building” the statement that will be run against the DB.&amp;nbsp; Most of the methods
on the interface return a reference to itself, so the method calls can be chained
to form a fluent-type interface.
&lt;/p&gt;
&lt;h5&gt;Executing SQL
&lt;/h5&gt;
&lt;p&gt;
Most of the time, I use raw SQL to execute my DB statements.&amp;nbsp; The ExecutingSql()
method on the IDatabase interface returns an IDbStatement reference.
&lt;/p&gt;
&lt;pre style="BORDER-RIGHT: rgb(206,206,206) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(206,206,206) 1px solid; PADDING-LEFT: 5px; MIN-HEIGHT: 40px; PADDING-BOTTOM: 5px; OVERFLOW: auto; BORDER-LEFT: rgb(206,206,206) 1px solid; WIDTH: 750px; PADDING-TOP: 5px; BORDER-BOTTOM: rgb(206,206,206) 1px solid; BACKGROUND-COLOR: rgb(251,251,251)"&gt;&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt;  1: &lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;class&lt;/span&gt; ProductRepository
: IProductRepository &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt;  2: {
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt;  3:     &lt;span style="COLOR: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;readonly&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;string&lt;/span&gt; SELECT_PRODUCT_BY_ID
= &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt;  4:         "&lt;span style="COLOR: rgb(139,0,0)"&gt;select
ProductID, Name, ProductNumber, Color, ListPrice, ProductModel &lt;/span&gt;" + &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt;  5:         "&lt;span style="COLOR: rgb(139,0,0)"&gt;from
Products &lt;/span&gt;" + &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt;  6:         "&lt;span style="COLOR: rgb(139,0,0)"&gt;where
ProductID = @ID&lt;/span&gt;"; &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt;  7: 
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt;  8:     &lt;span style="COLOR: rgb(0,128,0)"&gt;//
more SQL &lt;/span&gt; &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt;  9: 
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 10:     &lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; ProductRepository(IDatabase
db) &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 11:     {
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 12:         &lt;span style="COLOR: rgb(0,0,255)"&gt;this&lt;/span&gt;.Database
= db; &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 13:     }
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 14: 
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 15:     &lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; IDatabase
Database &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 16:     {
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 17:         &lt;span style="COLOR: rgb(0,0,255)"&gt;get&lt;/span&gt;; &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 18:         &lt;span style="COLOR: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;set&lt;/span&gt;; &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 19:     }
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 20: 
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 21:     &lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; Product
Load(&lt;span style="COLOR: rgb(0,0,255)"&gt;int&lt;/span&gt; productID) &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 22:     {
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 23:         IDbStatement dbStmt = &lt;span style="COLOR: rgb(0,0,255)"&gt;this&lt;/span&gt;.Database.ExecutingSql(SELECT_PRODUCT_BY_ID); &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 24:         dbStmt.AddParameter("&lt;span style="COLOR: rgb(139,0,0)"&gt;ID&lt;/span&gt;",
productID); &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 25:         &lt;span style="COLOR: rgb(0,0,255)"&gt;return&lt;/span&gt; dbStmt.QueryForOne&amp;lt;Product&amp;gt;(CreateProduct); &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 26:     }
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 27: 
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 28:     &lt;span style="COLOR: rgb(0,128,0)"&gt;//
more CRUD methods&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 29: 
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 30:     &lt;span style="COLOR: rgb(0,0,255)"&gt;private&lt;/span&gt; Product
CreateProduct(IResultSetRow row) &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 31:     {
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 32:         &lt;span style="COLOR: rgb(0,128,0)"&gt;//
implementation omitted...&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 33:     }
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 34: } &lt;/pre&gt;
&lt;/pre&gt;
&lt;p&gt;
As I said before, since the methods on the IDbStatement return themselves, you can
chain the calls.&amp;nbsp; In line 21-26, you could change the Load() method to read like
this:
&lt;/p&gt;
&lt;pre style="BORDER-RIGHT: rgb(206,206,206) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(206,206,206) 1px solid; PADDING-LEFT: 5px; MIN-HEIGHT: 40px; PADDING-BOTTOM: 5px; OVERFLOW: auto; BORDER-LEFT: rgb(206,206,206) 1px solid; WIDTH: 750px; PADDING-TOP: 5px; BORDER-BOTTOM: rgb(206,206,206) 1px solid; BACKGROUND-COLOR: rgb(251,251,251)"&gt;&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt;  1: &lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; Product
Load(&lt;span style="COLOR: rgb(0,0,255)"&gt;int&lt;/span&gt; productID) &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt;  2: {
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt;  3:     &lt;span style="COLOR: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt;  4:         &lt;span style="COLOR: rgb(0,0,255)"&gt;this&lt;/span&gt;.Database &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt;  5:             .ExecutingSql(SELECT_PRODUCT_BY_ID)
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt;  6:             .AddParameter("&lt;span style="COLOR: rgb(139,0,0)"&gt;ID&lt;/span&gt;",
productID) &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt;  7:             .QueryForOne&amp;lt;Product&amp;gt;(CreateProduct);
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt;  8: }&lt;/pre&gt;
&lt;/pre&gt;
&lt;h5&gt;Stored Procedures
&lt;/h5&gt;
&lt;p&gt;
Stored Procedures are built up in a similar fashion, except you need to call the CallingStoredProcedureNamed()
method to load the SP.
&lt;/p&gt;
&lt;pre style="BORDER-RIGHT: rgb(206,206,206) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(206,206,206) 1px solid; PADDING-LEFT: 5px; MIN-HEIGHT: 40px; PADDING-BOTTOM: 5px; OVERFLOW: auto; BORDER-LEFT: rgb(206,206,206) 1px solid; WIDTH: 750px; PADDING-TOP: 5px; BORDER-BOTTOM: rgb(206,206,206) 1px solid; BACKGROUND-COLOR: rgb(251,251,251)"&gt;&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt;  1: &lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;class&lt;/span&gt; EmployeeRepository
: IEmployeeRepository &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt;  2: {
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt;  3:     &lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; EmployeeRepository(IDatabase
db) &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt;  4:     { 
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt;  5:         &lt;span style="COLOR: rgb(0,0,255)"&gt;this&lt;/span&gt;.Database
= db; &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt;  6:     }
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt;  7:  
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt;  8:     &lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; IDatabase
Database &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt;  9:     {
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 10:         &lt;span style="COLOR: rgb(0,0,255)"&gt;get&lt;/span&gt;; &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 11:         &lt;span style="COLOR: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;set&lt;/span&gt;; &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 12:     }
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 13: 
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 14:     &lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; IEnumerable&amp;lt;Employee&amp;gt;
FindEmployeesByManager(&lt;span style="COLOR: rgb(0,0,255)"&gt;int&lt;/span&gt; mgrID) &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 15:     {
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 16:        &lt;span style="COLOR: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 17:           &lt;span style="COLOR: rgb(0,0,255)"&gt;this&lt;/span&gt;.Database &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 18:               .CallingStoredProcedureNamed("&lt;span style="COLOR: rgb(139,0,0)"&gt;uspGetManagerEmployees&lt;/span&gt;") &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 19:               .AddParameter("&lt;span style="COLOR: rgb(139,0,0)"&gt;ManagerID&lt;/span&gt;",
mgrID) &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 20:               .QueryForResults&amp;lt;Employee&amp;gt;(CreateEmployee);
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 21:     }
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 22: 
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 23:     &lt;span style="COLOR: rgb(0,0,255)"&gt;private&lt;/span&gt; Employee
CreateEmployee(IResultSetRow row) &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 24:     {
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 25:         &lt;span style="COLOR: rgb(0,128,0)"&gt;//
implementation omitted&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 26:     }
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 27: }&lt;/pre&gt;
&lt;/pre&gt;
&lt;h5&gt;Registered Queries
&lt;/h5&gt;
&lt;p&gt;
I showed in &lt;a href="http://www.chrisrauber.com/blog/2009/02/16/UsingMiadoPart2.aspx"&gt;my
last post&lt;/a&gt; how you can use the IDatabase interface to “register” queries.&amp;nbsp;
You can easily retrieve them in the IDbStatement interface by using LoadQueryRegisteredAs()
method, which will load the corresponding SQL.
&lt;/p&gt;
&lt;pre style="BORDER-RIGHT: rgb(206,206,206) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(206,206,206) 1px solid; PADDING-LEFT: 5px; MIN-HEIGHT: 40px; PADDING-BOTTOM: 5px; OVERFLOW: auto; BORDER-LEFT: rgb(206,206,206) 1px solid; WIDTH: 750px; PADDING-TOP: 5px; BORDER-BOTTOM: rgb(206,206,206) 1px solid; BACKGROUND-COLOR: rgb(251,251,251)"&gt;&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt;  1: &lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; IEnumerable&amp;lt;State&amp;gt;
FindAllStates() &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt;  2: {
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt;  3:     &lt;span style="COLOR: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt;  4:         &lt;span style="COLOR: rgb(0,0,255)"&gt;this&lt;/span&gt;.Database &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt;  5:             .LoadQueryRegisteredAs("&lt;span style="COLOR: rgb(139,0,0)"&gt;SelectStates&lt;/span&gt;") &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt;  6:             .QueryForResults&amp;lt;State&amp;gt;(
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt;  7:                 row =&amp;gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;new&lt;/span&gt; State &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt;  8:                 { 
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt;  9:                     Abbreviation = row.GetValue&amp;lt;&lt;span style="COLOR: rgb(0,0,255)"&gt;string&lt;/span&gt;&amp;gt;("&lt;span style="COLOR: rgb(139,0,0)"&gt;StateCode&lt;/span&gt;"), &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 10:                     Name = row.GetValue&amp;lt;&lt;span style="COLOR: rgb(0,0,255)"&gt;string&lt;/span&gt;&amp;gt;("&lt;span style="COLOR: rgb(139,0,0)"&gt;Name&lt;/span&gt;") &lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(251,251,251)"&gt; 11:                 });
&lt;/pre&gt;
&lt;pre style="FONT-SIZE: 12px; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; BACKGROUND-COLOR: rgb(255,255,255)"&gt; 12: }                &lt;/pre&gt;
&lt;/pre&gt;
&lt;h5&gt;Running Custom Queries
&lt;/h5&gt;
&lt;p&gt;
There is one more way to “load” a query in the IDbStatement interface, and that is
by calling the RunningQuery() method, and passing in an object that implements the
ISqlQuery interface.&amp;nbsp; That is new functionality to this release and probably
deserves its own blog post, so I will cover that later.
&lt;/p&gt;
&lt;p&gt;
In my &lt;a href="http://www.chrisrauber.com/blog/2009/03/01/UsingMiadoNdashPart4.aspx"&gt;next
post&lt;/a&gt;, I will discuss how the IDbStatement executes and discuss the different kinds
of return types available, and how you can create custom business objects from the
query result set.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=9b602341-2e1a-4ba2-aac9-8681f4d778c2" /&gt;</description>
      <comments>http://www.chrisrauber.com/blog/CommentView,guid,9b602341-2e1a-4ba2-aac9-8681f4d778c2.aspx</comments>
      <category>Miado</category>
    </item>
    <item>
      <trackback:ping>http://www.chrisrauber.com/blog/Trackback.aspx?guid=800f722a-fd6f-4a6f-8ea7-89f6569fb92a</trackback:ping>
      <pingback:server>http://www.chrisrauber.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.chrisrauber.com/blog/PermaLink,guid,800f722a-fd6f-4a6f-8ea7-89f6569fb92a.aspx</pingback:target>
      <dc:creator>Chris Rauber</dc:creator>
      <wfw:comment>http://www.chrisrauber.com/blog/CommentView,guid,800f722a-fd6f-4a6f-8ea7-89f6569fb92a.aspx</wfw:comment>
      <wfw:commentRss>http://www.chrisrauber.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=800f722a-fd6f-4a6f-8ea7-89f6569fb92a</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This is part 2 in a series on the new updates to the <a href="http://www.chrisrauber.com/blog/ct.ashx?id=32b78461-2695-47e5-83be-59a99c89e9b4&amp;url=http%3a%2f%2fwww.codeplex.com%2fmiado%2fRelease%2fProjectReleases.aspx%3fReleaseId%3d23278">Miado</a> API. 
You can read the first part in the series <a href="http://www.chrisrauber.com/blog/2009/02/15/UsingMiadoNdashPart1.aspx">here</a>. 
In this post, I am going to discuss some of the new features available when configuring
Miado.
</p>
        <h5>Query Registration
</h5>
        <p>
I’ve had a couple of clients who’ve had a requirement where they wanted to store their
SQL in a single location rather than spread across the Repository objects as embedded
SQL.  In fact, one client put all their SQL in .sql files and stored them on
the file system.  I had never seen this before, but after working with it for
a while I definitely warmed up on the idea.  I definitely saw some advantages
for doing this:
</p>
        <ul>
          <li>
Versioning – it’s very easy to isolate changes to individual SQL statements, and you
can take advantage of all the benefits of using a version control system, like labeling,
diffs, branching, etc. 
</li>
          <li>
Testing - you can open the SQL files directly in your DB management software to test
the queries (as opposed to cutting and pasting out your source code, where you have
to remove code formatting like string concatenations) 
</li>
          <li>
Collaboration – you can easily share the queries with a DBA, who can help tweak them
or run profiling tools against them 
</li>
          <li>
Isolation – all your queries are stored in a common well-known place</li>
        </ul>
        <p>
So I added the capability in Miado to “register” queries by name.  You can either
do this manually or by pointing to files or a directory on the file system. 
The IQueryRegistry interface controls the registration and retrieval of stored queries. 
To get an access to this interface, you need to declare an Action&lt;IQueryRegistry&gt;
delegate to configure itself.  The delegate is passed in via a method on the
IDatabase interface. 
</p>
        <h5>Registering Queries Manually
</h5>
        <p>
The easiest way to register queries is by setting name/value pairs:
</p>
        <pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 750px; background-color: rgb(251, 251, 251);">
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  1: var db = <span style="color: rgb(0, 0, 255);">new</span> SqlServerDatabase(connString); </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  2: db.ConfigureQueryRegistry(queryRegistry =&gt; 
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  3:     {
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  4:         <span style="color: rgb(0, 0, 255);">string</span> name
= "<span style="color: rgb(139, 0, 0);">SelectStates</span>"; </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  5:         <span style="color: rgb(0, 0, 255);">string</span> sql
= "<span style="color: rgb(139, 0, 0);">select StateCode, Name from States</span>"; </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  6:         queryRegistry.RegisterQuery(name, sql);
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  7: 
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  8:         <span style="color: rgb(0, 0, 255);">string</span> name1
= "<span style="color: rgb(139, 0, 0);">SelectUserByID</span>"; </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  9:         <span style="color: rgb(0, 0, 255);">string</span> sql1
= "<span style="color: rgb(139, 0, 0);">select UserID, Email </span>" + </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 10:                       "<span style="color: rgb(139, 0, 0);">from
Users </span>" + </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 11:                       "<span style="color: rgb(139, 0, 0);">where
UserID = @ID</span>"; </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 12:         <span style="color: rgb(0, 128, 0);">//
NamedQuery is a simple name/value pair object</span></pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 13:         var namedQuery = <span style="color: rgb(0, 0, 255);">new</span> NamedQuery(name1,
sql1); </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 14:         queryRegistry.RegisterQuery(namedQuery);
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 15:     });</pre>
        </pre>
        <h5>Registering Queries From Files
</h5>
        <p>
Alternately, you can register queries from files on the file system.  There are
multiple ways to do this.  First of all, you can start at a selected directory
and optionally pass a Predicate to select only certain files.  From the resulting
files, you need to build a NamedQuery list from each individual file.
</p>
        <pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 750px; background-color: rgb(251, 251, 251);">
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  1: var db = <span style="color: rgb(0, 0, 255);">new</span> SqlServerDatabase(connString); </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  2: db.ConfigureQueryRegistry(queryRegistry =&gt;
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  3:     {
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  4:         <span style="color: rgb(0, 128, 0);">//
start looking in the "sql" directory for files named *.sql</span></pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  5:         queryRegistry.RegisterQueriesFromPath(
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  6:             "<span style="color: rgb(139, 0, 0);">sql</span>", </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  7:             fileInfo =&gt; String.Compare(fileInfo.Extension, "<span style="color: rgb(139, 0, 0);">.sql</span>")
== 0, </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  8:             fileInfo =&gt; 
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  9:             {
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 10:                 <span style="color: rgb(0, 0, 255);">string</span> queryName
= fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.')); </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 11:                 <span style="color: rgb(0, 0, 255);">string</span> sql
= File.ReadAllText(fileInfo.FullName); </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 12:                 <span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 255);">new</span> NamedQuery[]
{ <span style="color: rgb(0, 0, 255);">new</span> NamedQuery(queryName, sql) }; </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 13:             });
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 14:     });</pre>
        </pre>
        <p>
You can also pass in the specific files you want processed:
</p>
        <pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 750px; background-color: rgb(251, 251, 251);">
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  1: var db = <span style="color: rgb(0, 0, 255);">new</span> SqlServerDatabase(connString); </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  2: db.ConfigureQueryRegistry(queryRegistry =&gt; 
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  3:     {
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  4:         <span style="color: rgb(0, 128, 0);">//
find the files yourself</span></pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  5:         <span style="color: rgb(0, 0, 255);">string</span>[]
files = Directory.GetFiles("<span style="color: rgb(139, 0, 0);">sql</span>", "<span style="color: rgb(139, 0, 0);">*.sql</span>",
SearchOption.AllDirectories); </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  6:         queryRegistry.RegisterQueriesFromFiles(
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  7:             files, 
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  8:             fileInfo =&gt;
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  9:             {
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 10:                 <span style="color: rgb(0, 0, 255);">string</span> queryName
= fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.')); </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 11:                 <span style="color: rgb(0, 0, 255);">string</span> sql
= File.ReadAllText(fileInfo.FullName); </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 12:                 <span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 255);">new</span> NamedQuery[]
{ <span style="color: rgb(0, 0, 255);">new</span> NamedQuery(queryName, sql) }; </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 13:             });
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 14:     });</pre>
        </pre>
        <h5>Be Smart
</h5>
        <p>
Obviously, you need to be careful about exposing the raw SQL in any directory that
can be compromised.  If you are using a web app, make sure it is not in a publicly
visible folder.  If you are using a rich client app, you might want to think
about using Isolated Storage as your underlying query repository.
</p>
        <p>
In my next post, I will show how to use the IDbStatement to run queries, including
those registered in the IQueryRegistry.
</p>
        <img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=800f722a-fd6f-4a6f-8ea7-89f6569fb92a" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/ChrisRauber/~4/QhfQAkWQ1sk" height="1" width="1" /></body>
      <title>Using Miado - Part 2</title>
      <guid isPermaLink="false">http://www.chrisrauber.com/blog/PermaLink,guid,800f722a-fd6f-4a6f-8ea7-89f6569fb92a.aspx</guid>
      <link>http://www.chrisrauber.com/blog/2009/02/16/UsingMiadoPart2.aspx</link>
      <pubDate>Mon, 16 Feb 2009 18:00:13 GMT</pubDate>
      <description>&lt;p&gt;
This is part 2 in a series on the new updates to the &lt;a href="http://www.chrisrauber.com/blog/ct.ashx?id=32b78461-2695-47e5-83be-59a99c89e9b4&amp;amp;url=http%3a%2f%2fwww.codeplex.com%2fmiado%2fRelease%2fProjectReleases.aspx%3fReleaseId%3d23278"&gt;Miado&lt;/a&gt; API.&amp;nbsp;
You can read the first part in the series &lt;a href="http://www.chrisrauber.com/blog/2009/02/15/UsingMiadoNdashPart1.aspx"&gt;here&lt;/a&gt;.&amp;nbsp;
In this post, I am going to discuss some of the new features available when configuring
Miado.
&lt;/p&gt;
&lt;h5&gt;Query Registration
&lt;/h5&gt;
&lt;p&gt;
I’ve had a couple of clients who’ve had a requirement where they wanted to store their
SQL in a single location rather than spread across the Repository objects as embedded
SQL.&amp;nbsp; In fact, one client put all their SQL in .sql files and stored them on
the file system.&amp;nbsp; I had never seen this before, but after working with it for
a while I definitely warmed up on the idea.&amp;nbsp; I definitely saw some advantages
for doing this:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Versioning – it’s very easy to isolate changes to individual SQL statements, and you
can take advantage of all the benefits of using a version control system, like labeling,
diffs, branching, etc. 
&lt;/li&gt;
&lt;li&gt;
Testing - you can open the SQL files directly in your DB management software to test
the queries (as opposed to cutting and pasting out your source code, where you have
to remove code formatting like string concatenations) 
&lt;/li&gt;
&lt;li&gt;
Collaboration – you can easily share the queries with a DBA, who can help tweak them
or run profiling tools against them 
&lt;/li&gt;
&lt;li&gt;
Isolation – all your queries are stored in a common well-known place&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
So I added the capability in Miado to “register” queries by name.&amp;nbsp; You can either
do this manually or by pointing to files or a directory on the file system.&amp;nbsp;
The IQueryRegistry interface controls the registration and retrieval of stored queries.&amp;nbsp;
To get an access to this interface, you need to declare an Action&amp;lt;IQueryRegistry&amp;gt;
delegate to configure itself.&amp;nbsp; The delegate is passed in via a method on the
IDatabase interface. 
&lt;/p&gt;
&lt;h5&gt;Registering Queries Manually
&lt;/h5&gt;
&lt;p&gt;
The easiest way to register queries is by setting name/value pairs:
&lt;/p&gt;
&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 750px; background-color: rgb(251, 251, 251);"&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  1: var db = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; SqlServerDatabase(connString); &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  2: db.ConfigureQueryRegistry(queryRegistry =&amp;gt; 
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  3:     {
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  4:         &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; name
= "&lt;span style="color: rgb(139, 0, 0);"&gt;SelectStates&lt;/span&gt;"; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  5:         &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; sql
= "&lt;span style="color: rgb(139, 0, 0);"&gt;select StateCode, Name from States&lt;/span&gt;"; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  6:         queryRegistry.RegisterQuery(name, sql);
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  7: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  8:         &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; name1
= "&lt;span style="color: rgb(139, 0, 0);"&gt;SelectUserByID&lt;/span&gt;"; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  9:         &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; sql1
= "&lt;span style="color: rgb(139, 0, 0);"&gt;select UserID, Email &lt;/span&gt;" + &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 10:                       "&lt;span style="color: rgb(139, 0, 0);"&gt;from
Users &lt;/span&gt;" + &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 11:                       "&lt;span style="color: rgb(139, 0, 0);"&gt;where
UserID = @ID&lt;/span&gt;"; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 12:         &lt;span style="color: rgb(0, 128, 0);"&gt;//
NamedQuery is a simple name/value pair object&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 13:         var namedQuery = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; NamedQuery(name1,
sql1); &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 14:         queryRegistry.RegisterQuery(namedQuery);
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 15:     });&lt;/pre&gt;
&lt;/pre&gt;
&lt;h5&gt;Registering Queries From Files
&lt;/h5&gt;
&lt;p&gt;
Alternately, you can register queries from files on the file system.&amp;nbsp; There are
multiple ways to do this.&amp;nbsp; First of all, you can start at a selected directory
and optionally pass a Predicate to select only certain files.&amp;nbsp; From the resulting
files, you need to build a NamedQuery list from each individual file.
&lt;/p&gt;
&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 750px; background-color: rgb(251, 251, 251);"&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  1: var db = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; SqlServerDatabase(connString); &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  2: db.ConfigureQueryRegistry(queryRegistry =&amp;gt;
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  3:     {
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  4:         &lt;span style="color: rgb(0, 128, 0);"&gt;//
start looking in the "sql" directory for files named *.sql&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  5:         queryRegistry.RegisterQueriesFromPath(
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  6:             "&lt;span style="color: rgb(139, 0, 0);"&gt;sql&lt;/span&gt;", &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  7:             fileInfo =&amp;gt; String.Compare(fileInfo.Extension, "&lt;span style="color: rgb(139, 0, 0);"&gt;.sql&lt;/span&gt;")
== 0, &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  8:             fileInfo =&amp;gt; 
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  9:             {
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 10:                 &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; queryName
= fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.')); &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 11:                 &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; sql
= File.ReadAllText(fileInfo.FullName); &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 12:                 &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; NamedQuery[]
{ &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; NamedQuery(queryName, sql) }; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 13:             });
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 14:     });&lt;/pre&gt;
&lt;/pre&gt;
&lt;p&gt;
You can also pass in the specific files you want processed:
&lt;/p&gt;
&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 750px; background-color: rgb(251, 251, 251);"&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  1: var db = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; SqlServerDatabase(connString); &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  2: db.ConfigureQueryRegistry(queryRegistry =&amp;gt; 
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  3:     {
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  4:         &lt;span style="color: rgb(0, 128, 0);"&gt;//
find the files yourself&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  5:         &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;[]
files = Directory.GetFiles("&lt;span style="color: rgb(139, 0, 0);"&gt;sql&lt;/span&gt;", "&lt;span style="color: rgb(139, 0, 0);"&gt;*.sql&lt;/span&gt;",
SearchOption.AllDirectories); &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  6:         queryRegistry.RegisterQueriesFromFiles(
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  7:             files, 
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  8:             fileInfo =&amp;gt;
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  9:             {
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 10:                 &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; queryName
= fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.')); &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 11:                 &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; sql
= File.ReadAllText(fileInfo.FullName); &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 12:                 &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; NamedQuery[]
{ &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; NamedQuery(queryName, sql) }; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 13:             });
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 14:     });&lt;/pre&gt;
&lt;/pre&gt;
&lt;h5&gt;Be Smart
&lt;/h5&gt;
&lt;p&gt;
Obviously, you need to be careful about exposing the raw SQL in any directory that
can be compromised.&amp;nbsp; If you are using a web app, make sure it is not in a publicly
visible folder.&amp;nbsp; If you are using a rich client app, you might want to think
about using Isolated Storage as your underlying query repository.
&lt;/p&gt;
&lt;p&gt;
In my next post, I will show how to use the IDbStatement to run queries, including
those registered in the IQueryRegistry.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=800f722a-fd6f-4a6f-8ea7-89f6569fb92a" /&gt;</description>
      <comments>http://www.chrisrauber.com/blog/CommentView,guid,800f722a-fd6f-4a6f-8ea7-89f6569fb92a.aspx</comments>
      <category>Miado</category>
    </item>
    <item>
      <trackback:ping>http://www.chrisrauber.com/blog/Trackback.aspx?guid=32b78461-2695-47e5-83be-59a99c89e9b4</trackback:ping>
      <pingback:server>http://www.chrisrauber.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.chrisrauber.com/blog/PermaLink,guid,32b78461-2695-47e5-83be-59a99c89e9b4.aspx</pingback:target>
      <dc:creator>Chris Rauber</dc:creator>
      <wfw:comment>http://www.chrisrauber.com/blog/CommentView,guid,32b78461-2695-47e5-83be-59a99c89e9b4.aspx</wfw:comment>
      <wfw:commentRss>http://www.chrisrauber.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=32b78461-2695-47e5-83be-59a99c89e9b4</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I released a new version of <a href="http://www.codeplex.com/miado/Release/ProjectReleases.aspx?ReleaseId=23278" target="_blank">Miado</a> on <a href="http://www.codeplex.com/" target="_blank">CodePlex</a> last
night, so I thought I’d spend a couple of blog posts explaining how to use the API. 
Consider this post as the first in a multi-part series on how to use the newest version.
</p>
        <h5>Basics – Creating an IDatabase
</h5>
        <p>
The first thing you need to do is get a handle on an IDatabase.  The interface
(and its corresponding implementation) serves as the entry point of the API. 
Obviously, the easiest way to do so would be to directly new one up.  The Database
constructor takes in a DbProviderFactory object and a connection string.
</p>
        <pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 750px; background-color: rgb(251, 251, 251);">
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  1: <span style="color: rgb(0, 0, 255);">string</span> connString
= </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  2:     "<span style="color: rgb(139, 0, 0);">Data
Source=localhost;Initial Catalog=MyDatabase; User ID=MyUser; Password=Password1</span>"; </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  3: IDatabase db = <span style="color: rgb(0, 0, 255);">new</span> Database(SqlClientFactory.Instance,
connString);</pre>
        </pre>
        <p>
I’ve created a few sub-classes of the Database class that take care of the provider
initialization (using the corresponding DbProviderFactory implementations provided
in the .Net Framework) and just take in a connection string:
</p>
        <ul>
          <li>
OdbcDatabase 
</li>
          <li>
OleDatabase 
</li>
          <li>
OracleDatabase 
</li>
          <li>
SqlServerDatabase</li>
        </ul>
        <p>
e.g.
</p>
        <pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 750px; background-color: rgb(251, 251, 251);">
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  1: <span style="color: rgb(0, 0, 255);">string</span> connString
= </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  2:     "<span style="color: rgb(139, 0, 0);">Data
Source=localhost;Initial Catalog=MyDatabase; User ID=MyUser; Password=Password1</span>"; </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  3: IDatabase db = <span style="color: rgb(0, 0, 255);">new</span> SqlServerDatabase(connString);</pre>
        </pre>
        <h5>Using Miado with a Depedency Injection Container
</h5>
        <p>
I’m a big fan of Dependency Injection, so I normally create a reference to the IDatabase
in my application start event and store it in the DI container.  
</p>
        <style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
        <pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 750px; background-color: rgb(251, 251, 251);">
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  1: <span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 255);">class</span> Global
: System.Web.HttpApplication, IContainerAccessor </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  2: {
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  3:     <span style="color: rgb(0, 0, 255);">public</span> IUnityContainer
Container </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  4:     {
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  5:         <span style="color: rgb(0, 0, 255);">get</span>; </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  6:         <span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 255);">set</span>; </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  7:     }
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  8: 
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  9:     <span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 255);">void</span> Application_Start() </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 10:     {
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 11:         <span style="color: rgb(0, 0, 255);">this</span>.InitializeContainer(); </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 12:     }
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 13: 
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 14:     <span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 255);">void</span> InitializeContainer() </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 15:     {
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 16:         var container = <span style="color: rgb(0, 0, 255);">new</span> UnityContainer(); </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 17:         
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 18:         <span style="color: rgb(0, 0, 255);">string</span> connString
= ConfigurationManager.ConnectionStrings["<span style="color: rgb(139, 0, 0);">MyDatabase</span>"].ConnectionString; </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 19:         <span style="color: rgb(0, 128, 0);">//
create IDatabase instance and store it in the container</span></pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 20:         var db = <span style="color: rgb(0, 0, 255);">new</span> SqlServerDatabase(connString); </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 21: 
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 22:         container.RegisterInstance&lt;IDatabase&gt;(db);
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 23: 
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 24:         <span style="color: rgb(0, 128, 0);">//
create a Repository object - IDatabase will injected in it</span></pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 25:         container.RegisterType&lt;IUserRepository, UserRepository&gt;();
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 26: 
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 27:         <span style="color: rgb(0, 128, 0);">//
rest of the container initialization omitted ...</span></pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 28: 
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 29:         Container = container;
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 30:     }
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 31: }</pre>
        </pre>
        <p>
When I create my repository objects, I design them to take a reference to the IDatabase
in the constructor. 
</p>
        <pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 750px; background-color: rgb(251, 251, 251);">
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  1: <span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 255);">class</span> UserRepository
: IUserRepository </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  2: {
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  3:     <span style="color: rgb(0, 0, 255);">public</span> UserRepository(IDatabase
db) </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  4:     {
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  5:         <span style="color: rgb(0, 0, 255);">this</span>.Database
= db; </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  6:     }
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  7: 
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  8:     <span style="color: rgb(0, 0, 255);">public</span> IDatabase
Database </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  9:     {
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 10:         <span style="color: rgb(0, 0, 255);">get</span>; </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 11:         <span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 255);">set</span>; </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 12:     }
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 13: 
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 14:     <span style="color: rgb(0, 128, 0);">//
CRUD methods omitted</span></pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 15: 
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 16: }
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"> 17: </pre>
        </pre>
        <p>
The nice thing about the DI container is that it will automatically inject a reference
to the IDatabase instance I registered when it resolves the Repository reference.
</p>
        <pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 750px; background-color: rgb(251, 251, 251);">
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  1: IContainerAccessor accessor = HttpContext.Current.ApplicationInstance <span style="color: rgb(0, 0, 255);">as</span> IContainerAccessor; </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  2: 
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  3: <span style="color: rgb(0, 128, 0);">//
IDatabase is resolved and injected in UserRepository constructor</span></pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  4: IUserRepository userRepository = accessor.Container.Resolve&lt;IUserRepository&gt;();</pre>
        </pre>
        <p>
If you don’t want to use a DI container, you can accomplish a similar de-coupling
by hiding the IDatabase creation behind a Factory object.
</p>
        <pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 750px; background-color: rgb(251, 251, 251);">
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  1: <span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 255);">class</span> DatabaseFactory </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  2: {
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  3:     <span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 255);">readonly</span> IDatabase
_db = </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  4:         <span style="color: rgb(0, 0, 255);">new</span> SqlServerDatabase(ConfigurationManager.ConnectionStrings["<span style="color: rgb(139, 0, 0);">MyDatabase</span>"].ConnectionString); </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  5: 
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  6:     <span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 255);">static</span> IDatabase
CreateDatabase() </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  7:     {
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);">  8:         <span style="color: rgb(0, 0, 255);">return</span> _db; </pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);">  9:     }
</pre>
          <pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"> 10: }</pre>
        </pre>
        <p>
In my <a href="http://www.chrisrauber.com/blog/2009/02/16/UsingMiadoPart2.aspx">next
post</a>, I will discuss how to use the IDatabase interface to register queries (a
new feature in Miado).<br /></p>
        <img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=32b78461-2695-47e5-83be-59a99c89e9b4" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/ChrisRauber/~4/XbR4xehWVqA" height="1" width="1" /></body>
      <title>Using Miado &amp;ndash; Part 1</title>
      <guid isPermaLink="false">http://www.chrisrauber.com/blog/PermaLink,guid,32b78461-2695-47e5-83be-59a99c89e9b4.aspx</guid>
      <link>http://www.chrisrauber.com/blog/2009/02/15/UsingMiadoNdashPart1.aspx</link>
      <pubDate>Sun, 15 Feb 2009 16:23:59 GMT</pubDate>
      <description>&lt;p&gt;
I released a new version of &lt;a href="http://www.codeplex.com/miado/Release/ProjectReleases.aspx?ReleaseId=23278" target="_blank"&gt;Miado&lt;/a&gt; on &lt;a href="http://www.codeplex.com/" target="_blank"&gt;CodePlex&lt;/a&gt; last
night, so I thought I’d spend a couple of blog posts explaining how to use the API.&amp;nbsp;
Consider this post as the first in a multi-part series on how to use the newest version.
&lt;/p&gt;
&lt;h5&gt;Basics – Creating an IDatabase
&lt;/h5&gt;
&lt;p&gt;
The first thing you need to do is get a handle on an IDatabase.&amp;nbsp; The interface
(and its corresponding implementation) serves as the entry point of the API.&amp;nbsp;
Obviously, the easiest way to do so would be to directly new one up.&amp;nbsp; The Database
constructor takes in a DbProviderFactory object and a connection string.
&lt;/p&gt;
&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 750px; background-color: rgb(251, 251, 251);"&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  1: &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; connString
= &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  2:     "&lt;span style="color: rgb(139, 0, 0);"&gt;Data
Source=localhost;Initial Catalog=MyDatabase; User ID=MyUser; Password=Password1&lt;/span&gt;"; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  3: IDatabase db = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Database(SqlClientFactory.Instance,
connString);&lt;/pre&gt;
&lt;/pre&gt;
&lt;p&gt;
I’ve created a few sub-classes of the Database class that take care of the provider
initialization (using the corresponding DbProviderFactory implementations provided
in the .Net Framework) and just take in a connection string:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
OdbcDatabase 
&lt;/li&gt;
&lt;li&gt;
OleDatabase 
&lt;/li&gt;
&lt;li&gt;
OracleDatabase 
&lt;/li&gt;
&lt;li&gt;
SqlServerDatabase&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
e.g.
&lt;/p&gt;
&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 750px; background-color: rgb(251, 251, 251);"&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  1: &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; connString
= &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  2:     "&lt;span style="color: rgb(139, 0, 0);"&gt;Data
Source=localhost;Initial Catalog=MyDatabase; User ID=MyUser; Password=Password1&lt;/span&gt;"; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  3: IDatabase db = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; SqlServerDatabase(connString);&lt;/pre&gt;
&lt;/pre&gt;
&lt;h5&gt;Using Miado with a Depedency Injection Container
&lt;/h5&gt;
&lt;p&gt;
I’m a big fan of Dependency Injection, so I normally create a reference to the IDatabase
in my application start event and store it in the DI container.&amp;nbsp; 
&lt;/p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 750px; background-color: rgb(251, 251, 251);"&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  1: &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; Global
: System.Web.HttpApplication, IContainerAccessor &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  2: {
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  3:     &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; IUnityContainer
Container &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  4:     {
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  5:         &lt;span style="color: rgb(0, 0, 255);"&gt;get&lt;/span&gt;; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  6:         &lt;span style="color: rgb(0, 0, 255);"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;set&lt;/span&gt;; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  7:     }
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  8: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  9:     &lt;span style="color: rgb(0, 0, 255);"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Application_Start() &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 10:     {
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 11:         &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.InitializeContainer(); &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 12:     }
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 13: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 14:     &lt;span style="color: rgb(0, 0, 255);"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; InitializeContainer() &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 15:     {
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 16:         var container = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; UnityContainer(); &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 17:         
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 18:         &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; connString
= ConfigurationManager.ConnectionStrings["&lt;span style="color: rgb(139, 0, 0);"&gt;MyDatabase&lt;/span&gt;"].ConnectionString; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 19:         &lt;span style="color: rgb(0, 128, 0);"&gt;//
create IDatabase instance and store it in the container&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 20:         var db = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; SqlServerDatabase(connString); &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 21: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 22:         container.RegisterInstance&amp;lt;IDatabase&amp;gt;(db);
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 23: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 24:         &lt;span style="color: rgb(0, 128, 0);"&gt;//
create a Repository object - IDatabase will injected in it&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 25:         container.RegisterType&amp;lt;IUserRepository, UserRepository&amp;gt;();
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 26: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 27:         &lt;span style="color: rgb(0, 128, 0);"&gt;//
rest of the container initialization omitted ...&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 28: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 29:         Container = container;
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 30:     }
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 31: }&lt;/pre&gt;
&lt;/pre&gt;
&lt;p&gt;
When I create my repository objects, I design them to take a reference to the IDatabase
in the constructor. 
&lt;/p&gt;
&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 750px; background-color: rgb(251, 251, 251);"&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  1: &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; UserRepository
: IUserRepository &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  2: {
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  3:     &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; UserRepository(IDatabase
db) &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  4:     {
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  5:         &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;.Database
= db; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  6:     }
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  7: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  8:     &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; IDatabase
Database &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  9:     {
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 10:         &lt;span style="color: rgb(0, 0, 255);"&gt;get&lt;/span&gt;; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 11:         &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;set&lt;/span&gt;; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 12:     }
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 13: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 14:     &lt;span style="color: rgb(0, 128, 0);"&gt;//
CRUD methods omitted&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 15: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 16: }
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt; 17: &lt;/pre&gt;
&lt;/pre&gt;
&lt;p&gt;
The nice thing about the DI container is that it will automatically inject a reference
to the IDatabase instance I registered when it resolves the Repository reference.
&lt;/p&gt;
&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 750px; background-color: rgb(251, 251, 251);"&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  1: IContainerAccessor accessor = HttpContext.Current.ApplicationInstance &lt;span style="color: rgb(0, 0, 255);"&gt;as&lt;/span&gt; IContainerAccessor; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  2: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  3: &lt;span style="color: rgb(0, 128, 0);"&gt;//
IDatabase is resolved and injected in UserRepository constructor&lt;/span&gt; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  4: IUserRepository userRepository = accessor.Container.Resolve&amp;lt;IUserRepository&amp;gt;();&lt;/pre&gt;
&lt;/pre&gt;
&lt;p&gt;
If you don’t want to use a DI container, you can accomplish a similar de-coupling
by hiding the IDatabase creation behind a Factory object.
&lt;/p&gt;
&lt;pre style="border: 1px solid rgb(206, 206, 206); padding: 5px; overflow: auto; min-height: 40px; width: 750px; background-color: rgb(251, 251, 251);"&gt;&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  1: &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; DatabaseFactory &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  2: {
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  3:     &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;readonly&lt;/span&gt; IDatabase
_db = &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  4:         &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; SqlServerDatabase(ConfigurationManager.ConnectionStrings["&lt;span style="color: rgb(139, 0, 0);"&gt;MyDatabase&lt;/span&gt;"].ConnectionString); &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  5: 
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  6:     &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; IDatabase
CreateDatabase() &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  7:     {
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt;  8:         &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; _db; &lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(251, 251, 251);"&gt;  9:     }
&lt;/pre&gt;
&lt;pre style="margin: 0em; font-size: 12px; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(255, 255, 255);"&gt; 10: }&lt;/pre&gt;
&lt;/pre&gt;
&lt;p&gt;
In my &lt;a href="http://www.chrisrauber.com/blog/2009/02/16/UsingMiadoPart2.aspx"&gt;next
post&lt;/a&gt;, I will discuss how to use the IDatabase interface to register queries (a
new feature in Miado).&lt;br&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=32b78461-2695-47e5-83be-59a99c89e9b4" /&gt;</description>
      <comments>http://www.chrisrauber.com/blog/CommentView,guid,32b78461-2695-47e5-83be-59a99c89e9b4.aspx</comments>
      <category>Miado</category>
    </item>
    <item>
      <trackback:ping>http://www.chrisrauber.com/blog/Trackback.aspx?guid=280bbdaa-b30f-442e-8aef-52a9d10e532f</trackback:ping>
      <pingback:server>http://www.chrisrauber.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.chrisrauber.com/blog/PermaLink,guid,280bbdaa-b30f-442e-8aef-52a9d10e532f.aspx</pingback:target>
      <dc:creator>Chris Rauber</dc:creator>
      <wfw:comment>http://www.chrisrauber.com/blog/CommentView,guid,280bbdaa-b30f-442e-8aef-52a9d10e532f.aspx</wfw:comment>
      <wfw:commentRss>http://www.chrisrauber.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=280bbdaa-b30f-442e-8aef-52a9d10e532f</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
When I first jumped into C# programming, one of the biggest points of confusion for
me was in understanding delegates.  You see, Java had no real similar concept. 
So the idea of function pointers didn’t really register with me.  I saw some
really weird syntax for event handlers that looked like this:
</p>
        <div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4">
          <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 1:</span> myButton.Clicked
+= MyButton_Clicked;</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 2:</span>  </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 3:</span>
              <span style="color: #008000">//
...</span>
            </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 4:</span>  </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 5:</span>
              <span style="color: #0000ff">private</span> MyButton_Clicked(<span style="color: #0000ff">object</span> sender,
EventArgs e) </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 6:</span> {</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 7:</span>
              <span style="color: #008000">//
do something</span>
            </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 8:</span> }</pre>
          </div>
        </div>
        <p>
Eventually, I caught on that because the function called MyButton_Clicked satisfied
the signature required by the OnClick event, it could be added as an event handler
to the event.
</p>
        <p>
Aha!  Delegates are nothing more than <strong>“interfaces”</strong> at the method
(as opposed to class) level.  So even though I understood what delegates were,
I still didn’t see a widespread use for them outside event handlers.  Sure, I
saw examples where they were used, but they all seemed very contrived to me.  
</p>
        <h5>Enter in lambda expressions
</h5>
        <p>
          <a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx">Lambda expressions</a> were
introduced in C# 3.0.  I’ve said it before, while lambda expressions are new
to .Net, they are not even close to being a new concept in programming languages. 
They have been around forever.  Basically, it’s a terse syntax for defining anonymous
delegates.  In .Net, they were really a simpler way to interact with LINQ. 
Now you no longer had to go through the ceremony of creating an explicit method just
to satisfy the compiler.  This “inline” function call was especially useful in
simple delegate calls, as in a <a href="http://msdn.microsoft.com/en-us/library/bfcke1bz.aspx">Predicate</a>:
</p>
        <div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4">
          <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 1:</span> var
myList = <span style="color: #0000ff">new</span> List&lt;Person&gt;();</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 2:</span>
              <span style="color: #008000">//
...</span>
            </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 3:</span> Person
chris = myList.Find(person =&gt; person.FirstName == <span style="color: #006080">"Chris"</span>);</pre>
          </div>
        </div>
        <style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
        <p>
I don’t know why, but I really latched on to lambda expressions in a  way I never
did with delegates.  Which is actually a little weird, since I’ve heard numerous
complaints that the lambda expression syntax is too confusing for the average developer.  
</p>
        <h5>Changing the way I code
</h5>
        <p>
Now, lambda expressions have really changed the way I approach programming. 
I’ll give an example from <a href="http://www.codeplex.com/miado">Miado</a>. 
I wanted to add support to Miado for creating typed DataSet objects (in addition to
regular DataSet objects).  I added support for a querying for a DataSet using
generics, where the generic type had to be a class derived from DataSet:
</p>
        <div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4">
          <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 1:</span>
              <span style="color: #0000ff">public</span>
              <span style="color: #0000ff">virtual</span> T
QueryForDataSet&lt;T&gt;() <span style="color: #0000ff">where</span> T : DataSet</pre>
          </div>
        </div>
        <p>
I wanted to have one common method that handles creating both types of DataSets (the
ol’ <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a> – i.e.
“Don’t Repeat Yourself”).  Here’s the rub - when using a DataAdapter to fill
the DataSet, the method actually is just a little different from using a regular DataSet
versus a typed one.  It’s not that different, but enough to make you have to
handle it two different ways.  Previously, I probably would’ve created a template
method or maybe handled it through an interface.  But to use interfaces would
require an interface to define the method and two separate classes to implement the
varying implementations.  That’s a lot of work to accomplish this seemingly simple
task.
</p>
        <p>
Hmm … if I could just pass in the method  that I want to call, I can solve this
problem much easier.  Enter in my good friend <a href="http://msdn.microsoft.com/en-us/library/018hxwa8.aspx">Action&lt;T&gt;</a> to
the rescue!  
</p>
        <h5>Action&lt;T&gt;
</h5>
        <p>
Action&lt;T&gt; is a delegate method.  It takes in an instance of the generic
T type defined in its signature and has no return value.  So it’s a callback
method that performs some action (and thus cleverly named).  I love this delegate. 
It’s really great for implementing simple one line functions via a lambda expression. 
The syntax becomes very terse without having to build up all the ceremony just to
satisfy the compiler.
</p>
        <h5>Implementation
</h5>
        <p>
Actually, Action is overloaded so you can pass in multiple arguments.  In my
case, I am using two arguments to the method (i.e. Action&lt;T,U&gt;).  Here
is the way I implemented it:
</p>
        <div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4">
          <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 1:</span>
              <span style="color: #0000ff">private</span> T
QueryForDataSetUsingCustomDataAdapterFillFunction&lt;T&gt;(</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 2:</span> Action&lt;DbDataAdapter,
T&gt; adapterFillFunction) <span style="color: #0000ff">where</span> T : DataSet</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 3:</span> {</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 4:</span> T
ds = CreateEmptyDataSet&lt;T&gt;();</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 5:</span>  </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 6:</span>
              <span style="color: #0000ff">using</span> (
DbConnection conn = <span style="color: #0000ff">this</span>.Database.CreateConnection()
)</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 7:</span> {</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 8:</span> conn.Open();</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 9:</span>  </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 10:</span>
              <span style="color: #0000ff">using</span> (
DbCommand cmd = conn.CreateCommand() )</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 11:</span> {</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 12:</span> cmd.CommandText
= <span style="color: #0000ff">this</span>.CommandText;</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 13:</span> cmd.CommandType
= <span style="color: #0000ff">this</span>.CommandType;</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 14:</span>  </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 15:</span>
              <span style="color: #0000ff">if</span> ( <span style="color: #0000ff">this</span>.Parameters
!= <span style="color: #0000ff">null</span> )</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 16:</span> {</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 17:</span> cmd.Parameters.AddEnumeration(<span style="color: #0000ff">this</span>.Parameters);</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 18:</span> }</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 19:</span>  </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 20:</span>
              <span style="color: #0000ff">using</span> (
DbDataAdapter adapter = <span style="color: #0000ff">this</span>.Database.CreateDataAdapter()
)</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 21:</span> {</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 22:</span> adapter.SelectCommand
= cmd;</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 23:</span> adapterFillFunction(adapter,
ds);</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 24:</span> }</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 25:</span> }</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 26:</span> }</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 27:</span>  </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 28:</span>
              <span style="color: #0000ff">return</span> ds;</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 29:</span> }</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 30:</span>  </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 31:</span>
              <span style="color: #0000ff">public</span>
              <span style="color: #0000ff">virtual</span> DataSet
QueryForDataSet()</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 32:</span> {</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 33:</span>
              <span style="color: #0000ff">return</span> QueryForDataSetUsingCustomDataAdapterFillFunction&lt;DataSet&gt;(</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 34:</span> (adapter,
ds) =&gt; adapter.Fill(ds));</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 35:</span> }</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 36:</span>  </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 37:</span>
              <span style="color: #0000ff">public</span>
              <span style="color: #0000ff">virtual</span> T
QueryForDataSet&lt;T&gt;() <span style="color: #0000ff">where</span> T : DataSet</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 38:</span> {</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 39:</span>
              <span style="color: #0000ff">return</span> QueryForDataSetUsingCustomDataAdapterFillFunction&lt;T&gt;(</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 40:</span> (adapter,
ds) =&gt; adapter.Fill(ds, ds.Tables[0].TableName));</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 41:</span> }</pre>
          </div>
        </div>
        <p>
Basically, I made it so that the common method takes in a function that, given a DataAdapter
and a DataSet, does some work on those objects.  So the two public methods pass
in a different implementation of the Fill() method on the DataAdapter.  Like
I said before, it’s kind of like using interfaces for methods.  The common method
(in line 23) calls the Action method to perform its specific function by passing back
the current DataAdapter and DataSet.
</p>
        <h5>Conclusion
</h5>
        <p>
Again, lambda expressions have changed the way I approach programming.  Once
you get a handle on the syntax, it’s a great tool to have in your toolbox!
</p>
        <img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=280bbdaa-b30f-442e-8aef-52a9d10e532f" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/ChrisRauber/~4/SISZDL07PmM" height="1" width="1" /></body>
      <title>More Love For Lambda Expressions</title>
      <guid isPermaLink="false">http://www.chrisrauber.com/blog/PermaLink,guid,280bbdaa-b30f-442e-8aef-52a9d10e532f.aspx</guid>
      <link>http://www.chrisrauber.com/blog/2009/02/01/MoreLoveForLambdaExpressions.aspx</link>
      <pubDate>Sun, 01 Feb 2009 20:46:49 GMT</pubDate>
      <description>&lt;p&gt;
When I first jumped into C# programming, one of the biggest points of confusion for
me was in understanding delegates.&amp;nbsp; You see, Java had no real similar concept.&amp;nbsp;
So the idea of function pointers didn’t really register with me.&amp;nbsp; I saw some
really weird syntax for event handlers that looked like this:
&lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;
&lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 1:&lt;/span&gt; myButton.Clicked
+= MyButton_Clicked;&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 2:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 3:&lt;/span&gt; &lt;span style="color: #008000"&gt;//
...&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 4:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 5:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; MyButton_Clicked(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender,
EventArgs e) &lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 6:&lt;/span&gt; {&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 7:&lt;/span&gt; &lt;span style="color: #008000"&gt;//
do something&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 8:&lt;/span&gt; }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Eventually, I caught on that because the function called MyButton_Clicked satisfied
the signature required by the OnClick event, it could be added as an event handler
to the event.
&lt;/p&gt;
&lt;p&gt;
Aha!&amp;nbsp; Delegates are nothing more than &lt;strong&gt;“interfaces”&lt;/strong&gt; at the method
(as opposed to class) level.&amp;nbsp; So even though I understood what delegates were,
I still didn’t see a widespread use for them outside event handlers.&amp;nbsp; Sure, I
saw examples where they were used, but they all seemed very contrived to me.&amp;nbsp; 
&lt;/p&gt;
&lt;h5&gt;Enter in lambda expressions
&lt;/h5&gt;
&lt;p&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx"&gt;Lambda expressions&lt;/a&gt; were
introduced in C# 3.0.&amp;nbsp; I’ve said it before, while lambda expressions are new
to .Net, they are not even close to being a new concept in programming languages.&amp;nbsp;
They have been around forever.&amp;nbsp; Basically, it’s a terse syntax for defining anonymous
delegates.&amp;nbsp; In .Net, they were really a simpler way to interact with LINQ.&amp;nbsp;
Now you no longer had to go through the ceremony of creating an explicit method just
to satisfy the compiler.&amp;nbsp; This “inline” function call was especially useful in
simple delegate calls, as in a &lt;a href="http://msdn.microsoft.com/en-us/library/bfcke1bz.aspx"&gt;Predicate&lt;/a&gt;:
&lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;
&lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 1:&lt;/span&gt; var
myList = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; List&amp;lt;Person&amp;gt;();&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 2:&lt;/span&gt; &lt;span style="color: #008000"&gt;//
...&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 3:&lt;/span&gt; Person
chris = myList.Find(person =&amp;gt; person.FirstName == &lt;span style="color: #006080"&gt;"Chris"&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;p&gt;
I don’t know why, but I really latched on to lambda expressions in a&amp;nbsp; way I never
did with delegates.&amp;nbsp; Which is actually a little weird, since I’ve heard numerous
complaints that the lambda expression syntax is too confusing for the average developer.&amp;nbsp; 
&lt;/p&gt;
&lt;h5&gt;Changing the way I code
&lt;/h5&gt;
&lt;p&gt;
Now, lambda expressions have really changed the way I approach programming.&amp;nbsp;
I’ll give an example from &lt;a href="http://www.codeplex.com/miado"&gt;Miado&lt;/a&gt;.&amp;nbsp;
I wanted to add support to Miado for creating typed DataSet objects (in addition to
regular DataSet objects).&amp;nbsp; I added support for a querying for a DataSet using
generics, where the generic type had to be a class derived from DataSet:
&lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;
&lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;virtual&lt;/span&gt; T
QueryForDataSet&amp;lt;T&amp;gt;() &lt;span style="color: #0000ff"&gt;where&lt;/span&gt; T : DataSet&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
I wanted to have one common method that handles creating both types of DataSets (the
ol’ &lt;a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself"&gt;DRY&lt;/a&gt; – i.e.
“Don’t Repeat Yourself”).&amp;nbsp; Here’s the rub - when using a DataAdapter to fill
the DataSet, the method actually is just a little different from using a regular DataSet
versus a typed one.&amp;nbsp; It’s not that different, but enough to make you have to
handle it two different ways.&amp;nbsp; Previously, I probably would’ve created a template
method or maybe handled it through an interface.&amp;nbsp; But to use interfaces would
require an interface to define the method and two separate classes to implement the
varying implementations.&amp;nbsp; That’s a lot of work to accomplish this seemingly simple
task.
&lt;/p&gt;
&lt;p&gt;
Hmm … if I could just pass in the method&amp;nbsp; that I want to call, I can solve this
problem much easier.&amp;nbsp; Enter in my good friend &lt;a href="http://msdn.microsoft.com/en-us/library/018hxwa8.aspx"&gt;Action&amp;lt;T&amp;gt;&lt;/a&gt; to
the rescue!&amp;nbsp; 
&lt;/p&gt;
&lt;h5&gt;Action&amp;lt;T&amp;gt;
&lt;/h5&gt;
&lt;p&gt;
Action&amp;lt;T&amp;gt; is a delegate method.&amp;nbsp; It takes in an instance of the generic
T type defined in its signature and has no return value.&amp;nbsp; So it’s a callback
method that performs some action (and thus cleverly named).&amp;nbsp; I love this delegate.&amp;nbsp;
It’s really great for implementing simple one line functions via a lambda expression.&amp;nbsp;
The syntax becomes very terse without having to build up all the ceremony just to
satisfy the compiler.
&lt;/p&gt;
&lt;h5&gt;Implementation
&lt;/h5&gt;
&lt;p&gt;
Actually, Action is overloaded so you can pass in multiple arguments.&amp;nbsp; In my
case, I am using two arguments to the method (i.e. Action&amp;lt;T,U&amp;gt;).&amp;nbsp; Here
is the way I implemented it:
&lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;
&lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; T
QueryForDataSetUsingCustomDataAdapterFillFunction&amp;lt;T&amp;gt;(&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 2:&lt;/span&gt; Action&amp;lt;DbDataAdapter,
T&amp;gt; adapterFillFunction) &lt;span style="color: #0000ff"&gt;where&lt;/span&gt; T : DataSet&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 3:&lt;/span&gt; {&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 4:&lt;/span&gt; T
ds = CreateEmptyDataSet&amp;lt;T&amp;gt;();&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 5:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 6:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (
DbConnection conn = &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Database.CreateConnection()
)&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 7:&lt;/span&gt; {&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 8:&lt;/span&gt; conn.Open();&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 9:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 10:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (
DbCommand cmd = conn.CreateCommand() )&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 11:&lt;/span&gt; {&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 12:&lt;/span&gt; cmd.CommandText
= &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.CommandText;&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 13:&lt;/span&gt; cmd.CommandType
= &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.CommandType;&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 14:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 15:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; ( &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Parameters
!= &lt;span style="color: #0000ff"&gt;null&lt;/span&gt; )&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 16:&lt;/span&gt; {&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 17:&lt;/span&gt; cmd.Parameters.AddEnumeration(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Parameters);&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 18:&lt;/span&gt; }&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 19:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 20:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (
DbDataAdapter adapter = &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Database.CreateDataAdapter()
)&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 21:&lt;/span&gt; {&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 22:&lt;/span&gt; adapter.SelectCommand
= cmd;&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 23:&lt;/span&gt; adapterFillFunction(adapter,
ds);&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 24:&lt;/span&gt; }&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 25:&lt;/span&gt; }&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 26:&lt;/span&gt; }&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 27:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 28:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; ds;&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 29:&lt;/span&gt; }&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 30:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 31:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;virtual&lt;/span&gt; DataSet
QueryForDataSet()&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 32:&lt;/span&gt; {&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 33:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; QueryForDataSetUsingCustomDataAdapterFillFunction&amp;lt;DataSet&amp;gt;(&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 34:&lt;/span&gt; (adapter,
ds) =&amp;gt; adapter.Fill(ds));&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 35:&lt;/span&gt; }&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 36:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 37:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;virtual&lt;/span&gt; T
QueryForDataSet&amp;lt;T&amp;gt;() &lt;span style="color: #0000ff"&gt;where&lt;/span&gt; T : DataSet&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 38:&lt;/span&gt; {&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 39:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; QueryForDataSetUsingCustomDataAdapterFillFunction&amp;lt;T&amp;gt;(&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 40:&lt;/span&gt; (adapter,
ds) =&amp;gt; adapter.Fill(ds, ds.Tables[0].TableName));&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 41:&lt;/span&gt; }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Basically, I made it so that the common method takes in a function that, given a DataAdapter
and a DataSet, does some work on those objects.&amp;nbsp; So the two public methods pass
in a different implementation of the Fill() method on the DataAdapter.&amp;nbsp; Like
I said before, it’s kind of like using interfaces for methods.&amp;nbsp; The common method
(in line 23) calls the Action method to perform its specific function by passing back
the current DataAdapter and DataSet.
&lt;/p&gt;
&lt;h5&gt;Conclusion
&lt;/h5&gt;
&lt;p&gt;
Again, lambda expressions have changed the way I approach programming.&amp;nbsp; Once
you get a handle on the syntax, it’s a great tool to have in your toolbox!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=280bbdaa-b30f-442e-8aef-52a9d10e532f" /&gt;</description>
      <comments>http://www.chrisrauber.com/blog/CommentView,guid,280bbdaa-b30f-442e-8aef-52a9d10e532f.aspx</comments>
      <category>.Net</category>
      <category>C#</category>
      <category>Miado</category>
    </item>
    <item>
      <trackback:ping>http://www.chrisrauber.com/blog/Trackback.aspx?guid=4a30b6aa-3ac0-4619-bc7a-62fd17c0c472</trackback:ping>
      <pingback:server>http://www.chrisrauber.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.chrisrauber.com/blog/PermaLink,guid,4a30b6aa-3ac0-4619-bc7a-62fd17c0c472.aspx</pingback:target>
      <dc:creator>Chris Rauber</dc:creator>
      <wfw:comment>http://www.chrisrauber.com/blog/CommentView,guid,4a30b6aa-3ac0-4619-bc7a-62fd17c0c472.aspx</wfw:comment>
      <wfw:commentRss>http://www.chrisrauber.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=4a30b6aa-3ac0-4619-bc7a-62fd17c0c472</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <a href="http://www.chrisrauber.com/blog/content/binary/WindowsLiveWriter/AtlantaAlt.NetUserGroupMeeting_8C64/agile_book.jpg">
          <img title="agile_book" style="border-top-width: 0px; margin-top: 20px; display: inline; border-left-width: 0px; float: right; border-bottom-width: 0px; margin-left: 0px; margin-right: 0px; border-right-width: 0px" height="184" alt="agile_book" src="http://www.chrisrauber.com/blog/content/binary/WindowsLiveWriter/AtlantaAlt.NetUserGroupMeeting_8C64/agile_book_thumb.jpg" width="184" border="0" />
        </a>
        <p>
Tonight we are going to have another <a href="http://www.meetup.com/AtlAltDotNet/calendar/9525107/" target="_blank">meeting</a> of
the <a href="http://atlalt.net/screwturn/default.aspx?AspxAutoDetectCookieSupport=1" target="_blank">Atlanta
alt.net</a> user group.  We are meeting at <a href="http://www.manuelstavern.com/" target="_blank">Manuel’s
Tavern</a> again, but we are supposed to be in a different room this time.  I
know the last meeting at Manuel’s was a little hard to find (if you weren’t there,
last time they put us in a side room off another room that was holding a meeting,
which had closed its outer doors to the rest of the restaurant).  
</p>
        <p>
This week were are going to be in the “Eagle’s Nest” room, which is located on the
other side of the restaurant off the main dining area on its southern side. 
Look for the small stairs in the corner of the room.
</p>
        <p>
We are going to be discussing the book, <a href="http://www.amazon.com/Agile-Estimating-Planning-Robert-Martin/dp/0131479415" target="_blank">“Agile
Estimating and Planning”</a> by Mike Cohn.  If you haven’t read the book, it’s
no big deal.  We will be discussing lots of other things, too.   
</p>
        <p>
Hope to see you there!
</p>
        <img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=4a30b6aa-3ac0-4619-bc7a-62fd17c0c472" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/ChrisRauber/~4/vyhYacmPHvg" height="1" width="1" /></body>
      <title>Atlanta Alt.Net User Group Meeting</title>
      <guid isPermaLink="false">http://www.chrisrauber.com/blog/PermaLink,guid,4a30b6aa-3ac0-4619-bc7a-62fd17c0c472.aspx</guid>
      <link>http://www.chrisrauber.com/blog/2009/01/28/AtlantaAltNetUserGroupMeeting.aspx</link>
      <pubDate>Wed, 28 Jan 2009 15:01:32 GMT</pubDate>
      <description>&lt;a href="http://www.chrisrauber.com/blog/content/binary/WindowsLiveWriter/AtlantaAlt.NetUserGroupMeeting_8C64/agile_book.jpg"&gt;&lt;img title="agile_book" style="border-top-width: 0px; margin-top: 20px; display: inline; border-left-width: 0px; float: right; border-bottom-width: 0px; margin-left: 0px; margin-right: 0px; border-right-width: 0px" height="184" alt="agile_book" src="http://www.chrisrauber.com/blog/content/binary/WindowsLiveWriter/AtlantaAlt.NetUserGroupMeeting_8C64/agile_book_thumb.jpg" width="184" border="0"&gt;&lt;/a&gt; 
&lt;p&gt;
Tonight we are going to have another &lt;a href="http://www.meetup.com/AtlAltDotNet/calendar/9525107/" target="_blank"&gt;meeting&lt;/a&gt; of
the &lt;a href="http://atlalt.net/screwturn/default.aspx?AspxAutoDetectCookieSupport=1" target="_blank"&gt;Atlanta
alt.net&lt;/a&gt; user group.&amp;nbsp; We are meeting at &lt;a href="http://www.manuelstavern.com/" target="_blank"&gt;Manuel’s
Tavern&lt;/a&gt; again, but we are supposed to be in a different room this time.&amp;nbsp; I
know the last meeting at Manuel’s was a little hard to find (if you weren’t there,
last time they put us in a side room off another room that was holding a meeting,
which had closed its outer doors to the rest of the restaurant).&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
This week were are going to be in the “Eagle’s Nest” room, which is located on the
other side of the restaurant off the main dining area on its southern side.&amp;nbsp;
Look for the small stairs in the corner of the room.
&lt;/p&gt;
&lt;p&gt;
We are going to be discussing the book, &lt;a href="http://www.amazon.com/Agile-Estimating-Planning-Robert-Martin/dp/0131479415" target="_blank"&gt;“Agile
Estimating and Planning”&lt;/a&gt; by Mike Cohn.&amp;nbsp; If you haven’t read the book, it’s
no big deal.&amp;nbsp; We will be discussing lots of other things, too.&amp;nbsp;&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
Hope to see you there!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=4a30b6aa-3ac0-4619-bc7a-62fd17c0c472" /&gt;</description>
      <comments>http://www.chrisrauber.com/blog/CommentView,guid,4a30b6aa-3ac0-4619-bc7a-62fd17c0c472.aspx</comments>
      <category>.Net</category>
      <category>alt.net</category>
      <category>User Groups</category>
    </item>
    <item>
      <trackback:ping>http://www.chrisrauber.com/blog/Trackback.aspx?guid=09d39438-68ea-4330-b57b-e22b1357cee5</trackback:ping>
      <pingback:server>http://www.chrisrauber.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.chrisrauber.com/blog/PermaLink,guid,09d39438-68ea-4330-b57b-e22b1357cee5.aspx</pingback:target>
      <dc:creator>Chris Rauber</dc:creator>
      <wfw:comment>http://www.chrisrauber.com/blog/CommentView,guid,09d39438-68ea-4330-b57b-e22b1357cee5.aspx</wfw:comment>
      <wfw:commentRss>http://www.chrisrauber.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=09d39438-68ea-4330-b57b-e22b1357cee5</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In case you’ve been living in a cave on Mars with your fingers in your ears for the
last year, you’ve probably heard we just ended a little election here in the United
States.  I’m not big on the whole politics scene, so I’ll spare you much further
discussion on the subject, except that I just don’t *get* why people blindly pledge
their allegiance to extremist political views (no matter which side they’re on).  
Yet people by the millions are quick to eat up the junk spooned out by people like
Rush Limbaugh, (fellow Cornell alum) Keith Olberman, (fellow Cornell alum) Ann Coulter,
Sean Hannity, Bill O’Reilly, etc.  If you ask me, they’re all equally wackos.
</p>
        <p>
The same phenomena happens in the software development community.  We as an industry
are very divided on what we hold to be the best technology, whether it be operating
systems, programming languages, IDEs, frameworks, etc.  
</p>
        <p>
Maybe it’s the middle child in me or the fact that I’m a Libra, but I tend to take
a more centrist view on things. 
</p>
        <p>
I’ll share two quick items about my past:
</p>
        <ol>
          <li>
At one time, I was a snobby Java programmer at the height of the dot-com boom. 
If you told me during the early part of this decade that I would be making my living
doing (egad!) <strong><em>Microsoft</em></strong> development for a living, I would
have called you bat-shit crazy.  But a funny thing happened … I got exposed to
C# on a project I was on (i.e. it was mandated we use it), and it turned out to be
(IMO) a <em>better</em> version of Java.  And .Net was a wonderful development
environment!  It wasn’t at all like going to “the dark side” as I would have
been led to believe by my Java brethren. 
</li>
          <li>
I’ve known my friend <a href="http://boorad.weebly.com/index.html" target="_blank">Brad</a>,
another software developer, since college.  Though neither of us were CS majors,
somehow we both ended up in the software development field.  Here we are close
to 15 years removed from school and both have taken much different routes to get to
where we are in our careers.  While I have toed the corporate line and always
stuck to “enterprise” software development (i.e. Java and .Net), Brad has been fascinated
with open source technologies, functional programming languages, alternative databases,
and highly concurrent systems (way before any of these gathered mainstream momentum). 
In short, about 180 degrees from my development background.  Now that he has
moved to Atlanta, in the last few years he has been a great influence on me and my
view of the software development world.  In fact, part of the reason I so quickly
latched on to the new dynamic-language-type features introduced in .Net 3.0 (e.g.
lambda expressions, closures, extension methods) was directly because of his influence
since he had been telling me about them in other languages for so long.  Once
I finally was exposed to them first hand, I quickly understood where he was coming
from.</li>
        </ol>
        <p>
The lesson I’ve learned is to keep an open mind when it comes to software development. 
Too many times, we let pre-conceived notions and technology prejudices influence our
decisions.  (Want to know a dirty little secret?  I <em>like</em> Vista
… a lot, actually.)
</p>
        <p>
I’m finally reading the <a href="http://www.pragprog.com/titles/tpp/the-pragmatic-programmer" target="_blank">Pragmatic
Programmer</a> (I know, I know, lay off me!), and though I’ve heard it re-told many
times by many people, there is a classic adage in the book about learning a new programming
language every year.  There’s a reason it’s re-told so often.  It’s because
it’s really excellent advice.  If nothing else, it will help broaden your horizon
when it comes to tackling problems in your day-to-day programming language.  
It can open your mind to an entirely different way of thinking.  As humans, we’re
creatures of habit.  We need something to shake things up occasionally.
</p>
        <p>
The reason I’m writing this post now is that within the last few weeks, I’ve been
exposed to technologists wielding <a href="http://en.wikipedia.org/wiki/Golden_hammer" target="_blank">“Golden
Hammers”</a>.  If you’re not familiar with the term, it refers to people who
get so focused on a particular technology, they think it will solve every problem
thrown at them.  “Sharepoint can do that!”  “You need to employ a 100% SOA
so your website can scale, no matter how many users you are projecting!”  “Let’s
write an iPhone app for this niche blue-collar customer base!”  When you have
this Golden Hammer, everything looks like a nail. 
</p>
        <p>
It’s perfectly fine to have your convictions.  Being passionate about something,
whatever that thing may be, is one of the highest highs you can get in life. 
But don’t stick your head in the sand when it comes to technology.  There is
no “one technology to rule them all”.  Try to be objective in your decisions. 
Use common sense.  Weigh trade-offs for choosing one technology over another. 
Practice pragmatism.  And use the right tool for the job.
</p>
        <img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=09d39438-68ea-4330-b57b-e22b1357cee5" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/ChrisRauber/~4/w14PU1dnDwg" height="1" width="1" /></body>
      <title>Put Down That Golden Hammer!</title>
      <guid isPermaLink="false">http://www.chrisrauber.com/blog/PermaLink,guid,09d39438-68ea-4330-b57b-e22b1357cee5.aspx</guid>
      <link>http://www.chrisrauber.com/blog/2009/01/14/PutDownThatGoldenHammer.aspx</link>
      <pubDate>Wed, 14 Jan 2009 17:23:06 GMT</pubDate>
      <description>&lt;p&gt;
In case you’ve been living in a cave on Mars with your fingers in your ears for the
last year, you’ve probably heard we just ended a little election here in the United
States.&amp;nbsp; I’m not big on the whole politics scene, so I’ll spare you much further
discussion on the subject, except that I just don’t *get* why people blindly pledge
their allegiance to extremist political views (no matter which side they’re on).&amp;nbsp;&amp;nbsp;
Yet people by the millions are quick to eat up the junk spooned out by people like
Rush Limbaugh, (fellow Cornell alum) Keith Olberman, (fellow Cornell alum) Ann Coulter,
Sean Hannity, Bill O’Reilly, etc.&amp;nbsp; If you ask me, they’re all equally wackos.
&lt;/p&gt;
&lt;p&gt;
The same phenomena happens in the software development community.&amp;nbsp; We as an industry
are very divided on what we hold to be the best technology, whether it be operating
systems, programming languages, IDEs, frameworks, etc.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
Maybe it’s the middle child in me or the fact that I’m a Libra, but I tend to take
a more centrist view on things. 
&lt;/p&gt;
&lt;p&gt;
I’ll share two quick items about my past:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
At one time, I was a snobby Java programmer at the height of the dot-com boom.&amp;nbsp;
If you told me during the early part of this decade that I would be making my living
doing (egad!) &lt;strong&gt;&lt;em&gt;Microsoft&lt;/em&gt;&lt;/strong&gt; development for a living, I would
have called you bat-shit crazy.&amp;nbsp; But a funny thing happened … I got exposed to
C# on a project I was on (i.e. it was mandated we use it), and it turned out to be
(IMO) a &lt;em&gt;better&lt;/em&gt; version of Java.&amp;nbsp; And .Net was a wonderful development
environment!&amp;nbsp; It wasn’t at all like going to “the dark side” as I would have
been led to believe by my Java brethren. 
&lt;li&gt;
I’ve known my friend &lt;a href="http://boorad.weebly.com/index.html" target="_blank"&gt;Brad&lt;/a&gt;,
another software developer, since college.&amp;nbsp; Though neither of us were CS majors,
somehow we both ended up in the software development field.&amp;nbsp; Here we are close
to 15 years removed from school and both have taken much different routes to get to
where we are in our careers.&amp;nbsp; While I have toed the corporate line and always
stuck to “enterprise” software development (i.e. Java and .Net), Brad has been fascinated
with open source technologies, functional programming languages, alternative databases,
and highly concurrent systems (way before any of these gathered mainstream momentum).&amp;nbsp;
In short, about 180 degrees from my development background.&amp;nbsp; Now that he has
moved to Atlanta, in the last few years he has been a great influence on me and my
view of the software development world.&amp;nbsp; In fact, part of the reason I so quickly
latched on to the new dynamic-language-type features introduced in .Net 3.0 (e.g.
lambda expressions, closures, extension methods) was directly because of his influence
since he had been telling me about them in other languages for so long.&amp;nbsp; Once
I finally was exposed to them first hand, I quickly understood where he was coming
from.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
The lesson I’ve learned is to keep an open mind when it comes to software development.&amp;nbsp;
Too many times, we let pre-conceived notions and technology prejudices influence our
decisions.&amp;nbsp; (Want to know a dirty little secret?&amp;nbsp; I &lt;em&gt;like&lt;/em&gt; Vista
… a lot, actually.)
&lt;/p&gt;
&lt;p&gt;
I’m finally reading the &lt;a href="http://www.pragprog.com/titles/tpp/the-pragmatic-programmer" target="_blank"&gt;Pragmatic
Programmer&lt;/a&gt; (I know, I know, lay off me!), and though I’ve heard it re-told many
times by many people, there is a classic adage in the book about learning a new programming
language every year.&amp;nbsp; There’s a reason it’s re-told so often.&amp;nbsp; It’s because
it’s really excellent advice.&amp;nbsp; If nothing else, it will help broaden your horizon
when it comes to tackling problems in your day-to-day programming language.&amp;nbsp;&amp;nbsp;
It can open your mind to an entirely different way of thinking.&amp;nbsp; As humans, we’re
creatures of habit.&amp;nbsp; We need something to shake things up occasionally.
&lt;/p&gt;
&lt;p&gt;
The reason I’m writing this post now is that within the last few weeks, I’ve been
exposed to technologists wielding &lt;a href="http://en.wikipedia.org/wiki/Golden_hammer" target="_blank"&gt;“Golden
Hammers”&lt;/a&gt;.&amp;nbsp; If you’re not familiar with the term, it refers to people who
get so focused on a particular technology, they think it will solve every problem
thrown at them.&amp;nbsp; “Sharepoint can do that!”&amp;nbsp; “You need to employ a 100% SOA
so your website can scale, no matter how many users you are projecting!”&amp;nbsp; “Let’s
write an iPhone app for this niche blue-collar customer base!”&amp;nbsp; When you have
this Golden Hammer, everything looks like a nail. 
&lt;/p&gt;
&lt;p&gt;
It’s perfectly fine to have your convictions.&amp;nbsp; Being passionate about something,
whatever that thing may be, is one of the highest highs you can get in life.&amp;nbsp;
But don’t stick your head in the sand when it comes to technology.&amp;nbsp; There is
no “one technology to rule them all”.&amp;nbsp; Try to be objective in your decisions.&amp;nbsp;
Use common sense.&amp;nbsp; Weigh trade-offs for choosing one technology over another.&amp;nbsp;
Practice pragmatism.&amp;nbsp; And use the right tool for the job.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.chrisrauber.com/blog/aggbug.ashx?id=09d39438-68ea-4330-b57b-e22b1357cee5" /&gt;</description>
      <comments>http://www.chrisrauber.com/blog/CommentView,guid,09d39438-68ea-4330-b57b-e22b1357cee5.aspx</comments>
      <category>Agile</category>
      <category>Programming</category>
    </item>
  </channel>
</rss>
