title: How to avoid "too many parameters" problem in API design?
body: <p>I have this API function:</p>
  
  <pre><code>public ResultEnum DoSomeAction(string a, string b, DateTime c, OtherEnum d, 
       string e, string f, out Guid code)
  </code></pre>
  
  <p>I don't like it. Because parameter order becomes unnecessarily significant. It becomes harder to add new fields. It's harder to see what's being passed around. It's harder to refactor method into smaller parts because it creates another overhead of passing all the parameters in sub functions. Code is harder to read. </p>
  
  <p>I came up with the most obvious idea: have an object encapsulating the data and pass it around instead of passing each parameter one by one. Here is what I came up with:</p>
  
  <pre><code>public class DoSomeActionParameters
  {
      public string A;
      public string B;
      public DateTime C;
      public OtherEnum D;
      public string E;
      public string F;        
  }
  </code></pre>
  
  <p>That reduced my API declaration to:</p>
  
  <pre><code>public ResultEnum DoSomeAction(DoSomeActionParameters parameters, out Guid code)
  </code></pre>
  
  <p>Nice. Looks very innocent but we actually introduced a huge change: we introduced mutability. Because what we previously had been doing was actually to pass an anonymous immutable object: function parameters on stack. Now we created a new class which is very mutable. We created the ability to manipulate the state of the <strong>caller</strong>. That sucks. Now I want my object immutable, what do I do?</p>
  
  <pre><code>public class DoSomeActionParameters
  {
      public string A { get; private set; }
      public string B { get; private set; }
      public DateTime C { get; private set; }
      public OtherEnum D { get; private set; }
      public string E { get; private set; }
      public string F { get; private set; }        
  
      public DoSomeActionParameters(string a, string b, DateTime c, OtherEnum d, 
       string e, string f)
      {
          this.A = a;
          this.B = b;
          // ... tears erased the text here
      }
  }
  </code></pre>
  
  <p>As you can see I actually re-created my original problem: too many parameters. It's obvious that that's not the way to go. What am I going to do? The last option to achieve such immutability is to use a "readonly" struct like this:</p>
  
  <pre><code>public struct DoSomeActionParameters
  {
      public readonly string A;
      public readonly string B;
      public readonly DateTime C;
      public readonly OtherEnum D;
      public readonly string E;
      public readonly string F;        
  }
  </code></pre>
  
  <p>That allows us to avoid constructors with too many parameters and achieve immutability. Actually it fixes all the problems (parameter ordering etc). Yet:</p>
  
  <ul>
  <li>Everybody (including FXCop &amp; Jon Skeet) agree that <a href="http://csharpindepth.com/Articles/Chapter8/PropertiesMatter.aspx">exposing public fields are bad</a>.</li>
  <li>Eric Lippert et al say <a href="http://www.bluebytesoftware.com/blog/2010/07/01/WhenIsAReadonlyFieldNotReadonly.aspx">relying on readonly fields for immutability is a lie</a>.</li>
  </ul>
  
  <p>That's when I got confused and decided to write this question: What's the most straightforward way in C# to avoid "too many parameters" problem without introducing mutability? Is it possible to use a readonly struct for that purpose and yet not have a bad API design?</p>
  
  <p><strong>CLARIFICATIONS:</strong></p>
  
  <ul>
  <li>Please assume there is no violation of single responsibiltiy principle. In my original case the function just writes given parameters to a single DB record.</li>
  <li>I'm not seeking a specific solution to the given function. I'm seeking a generalized approach to such problems. I'm specifically interested in solving "too many parameters" problem without introducing mutability or a terrible design.</li>
  </ul>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The answers provided here have different advantages/disadvantages. Therefore I'd like to convert this to a community wiki. I think each answer with code sample and Pros/Cons would make a good guide for similar problems in the future. I'm now trying to find out how to do it.</p>