title: Fluent interfaces in C#
body: <p>I have a question with fluent interfaces.</p>
  
  <p>We have some objects that are used as parameter objects for a SQL interface, here's an example:</p>
  
  <pre><code>using (DatabaseCommand cmd = conn.CreateCommand(
      "SELECT A, B, C FROM tablename WHERE ID = :ID",
      SqlParameter.Int32(":ID", 1234)))
  {
      ...
  }
  </code></pre>
  
  <p>For some of these parameters, I'd like to enable some specialized options, but instead of adding more properties to the Int32 method (which is just one of many), I thought I'd look into fluent interfaces.</p>
  
  <p>Here's an example where I've added what I am looking into:</p>
  
  <pre><code>SqlParameter.Int32(":ID", 1234).With(SqlParameterOption
      .Substitute
      .Precision(15)
  )
  </code></pre>
  
  <p>I know these two options doesn't make sense for this type of parameter, but that's not what the question is about.</p>
  
  <p>In the above case, Substitute would have to be a static property (or method if I just add some parenthesis) on the SqlParameterOption class, whereas Precision would have to be an instance method.</p>
  
  <p>What if I reorder them?</p>
  
  <pre><code>SqlParameter.Int32(":ID", 1234).With(SqlParameterOption
      .Precision(15)
      .Substitute
  )
  </code></pre>
  
  <p>Then Substitute would have to be the instance property and Precision the static method. This won't compile of course, I can't have both a static and a non-static property or method with the same name.</p>
  
  <p>How do I do this? Am I completely on the wrong track here?</p>
  
  <p>While re-reading the question, I had an idea, would this different syntax below make more sense?</p>
  
  <pre><code>SqlParameter.Int32(":ID", 1234).With
      .Precision(15)
      .Substitute
  </code></pre>
  
  <p>In this case both would be instance methods on whatever With returns, which would be a specialized class or interface for SqlParameter options like this. I'm not sure I'd like to dump the <em>.With</em> part, as this would expose all methods of the object, instead of just the <em>fluent</em> ones.</p>
  
  <p>Advice and some good url's would be most welcome, I've scoured over many examples, but they tend to show examples like this:</p>
  
  <pre><code>order
      .AddFreeShipping()
      .IncludeItem(15)
          .SuppressTax();
  </code></pre>
  
  <p>(lifted from <a href="http://blog.troyd.net/PermaLink,guid,5cdd4862-857a-488d-a577-c6d21b548f19.aspx" rel="nofollow noreferrer">this page</a>)</p>
  
  <hr>
  
  <p><strong>Edit</strong>: Followup after responses
  From <a href="https://stackoverflow.com/users/1659/marxidad">@marxidad</a>:</p>
  
  <pre><code>class SqlParameterOption
  {
      public SqlParameterOption Precision(int p) {/* ... */; return this;}
      public SqlParameterOption Substitute() {/* ... */; return this;}
      /* ... */       
  }
  
  /* ... */
  SqlParameter.Int32(":ID", 1234).With(new SqlParameterOption()
                                             .Precision(15)
                                             .Substitute());
  </code></pre>
  
  <p>With this approach, With would have to take the object, and apply it to the parameter. I'm fine with that.</p>
  
  <p>If I used the syntax I added as an example, it would be like this:</p>
  
  <pre><code>SqlParameter.Int32(":ID", 1234).With
                                 .Precision(15)
                                 .Substitute());
  </code></pre>
  
  <p>In this case, With wouldn't know when the chain ended, so each option would have to apply its effect directly.</p>
  
  <p>What is preferred? That the options build up an effect object that will have to be applied later, or that each effect applies its effect directly?</p>
  
  <p><strong>My decision</strong>:
  As <a href="https://stackoverflow.com/users/1659/marxidad">@marxidad</a> says, if the changes are irreversible, and could potentially be subject to a reversal, building up state and failing at some point with an exception is the way I'll go.</p>
  
  <p>However, in this case, I'm going with a simpler approach that modifies the SqlParameter object directly.</p>
  
  <p>In this case, my code will look like this:</p>
  
  <pre><code>SqlParameter.Int32(":ID", 1234).With
                                 .Precision(15)
                                 .Substitute());
  </code></pre>
  
  <p><strong>Edit:</strong> Gah, that's how it goes when I focus on just one thing.</p>
  
  <p>I can't use that syntax, I'll go with the following, as suggested by <a href="https://stackoverflow.com/users/1659/marxidad">@marxidad</a>:</p>
  
  <pre><code>SqlParameter.Int32(":ID", 1234).With(new SqlParameterOption()
                                             .Precision(15)
                                             .Substitute());
  </code></pre>
  
  <p>The reason is of course that the method that takes the SqlParameter object as an argument is unable to cope with the object returned by With, so although the SqlParameter object is constructed and set up properly, it became incompatible with the intended usage.</p>