title: Understanding of How to Create a Fluent Interface
body: <p>Hi i'm trying to understand how i could build a readable and also error preventing Fluent-API without to much restriction for the User</p>
  
  <p>to hold it simple let's say we want to change the following class to be fluent</p>
  
  <pre><code>public class Car
  {
      public int Gallons { get; private set; }
      public int Tons { get; private set; }
      public int Bhp { get; private set; }
      public string Make { get; private set; }
      public string Model { get; private set; }
  
      public Car(string make, string model)
      {
          Make = make;
          Model = model;
      }
  
      public void WithHorsePower(int bhp)
      {
          Bhp = bhp;
          return this;
      }
  
      public void WithFuel(int gallons)
      {
          Gallons = gallons;
      }
  
      public void WithWeight(int tons)
      {
          Tons = tons;
      }
  
      public int Mpg()
      {
          return Gallons/Tons;
      }
  }
  </code></pre>
  
  <p>the problem in this case the user should only be able to access <code>Mpg()</code> if <code>Weight()</code> and <code>Fuel()</code> got called first also the position of <code>HorsePower()</code> is irrelevant.</p>
  
  <p>Samples:  </p>
  
  <pre><code>int mpg =Car.Create().HorsePower().Fuel().Weight().Mpg();
  int mpg =Car.Create().Fuel().HorsePower().Weight().Mpg();
  int mpg =Car.Create().HorsePower().Fuel().HorsePower().Weight().Mpg();// &lt;- no typo
  int mpg =Car.Create().Fuel().Weight().HorsePower().Mpg();
  int mpg =Car.Create().Weight().HorsePower().Fuel().Mpg();
  int mpg =Car.Create().Weight().Fuel().Mpg();
  </code></pre>
  
  <p>Is there a easy way to do this without a big bunch of interfaces?<br/>
  I also doesn't how to implement this nested interfaces in the right way</p>
  
  <p>Here are the interfaces i currently created</p>
  
  <pre><code>interface Start
  {
      IFuelWeight1 HorsePower();
  
      IHorsePowerWeight1 Fuel();
  
      IHorsePowerFuel1 Weight();
  }
  
  interface IFuelWeight1 // Start.HorsePower()
  {
      IHorsePowerWeight1 Fuel();
  
      IHorsePowerFuel1 Weight();
  }
  
  interface IHorsePowerWeight1 // Start.Fuel()
  {
      IHorsePowerWeight1 HorsePower();
  
      IHorsePowerFuelMpg Weight();
  }
  
  interface IHorsePowerFuel1 // Start.Weight()
  {
      IHorsePowerFuel1 HorsePower();
  
      IHorsePowerWeightMpg Fuel();
  }
  
  #region End
  
  interface IHorsePowerFuelMpg
  {
      IFuelWeightMpg HorsePower();
  
      IHorsePowerWeightMpg Fuel();
  
      int Mpg();
  }
  
  interface IHorsePowerWeightMpg
  {
      IFuelWeightMpg HorsePower();
  
      IHorsePowerFuelMpg Weight();
  
      int Mpg();
  }
  
  interface IFuelWeightMpg
  {
      IHorsePowerWeightMpg Fuel();
  
      IHorsePowerFuelMpg Weight();
  
      int Mpg();
  }
  
  #endregion
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT</strong> for Adam Houldsworth :-)</p>
  
  <ul>
  <li>Is the Interface above a good one or is there a easier way to do this
  <strong>but</strong> hold the restriction for Mpg()?</li>
  <li><p>How to Implement the interface above to do this?:</p>
  
  <pre><code>    var k = myMiracle as Start;
      k.Fuel().Weight();
      k.Weight().Fuel();
      k.HorsePower().Fuel().Weight();
      k.HorsePower().Weight().Fuel();
      k.Fuel().HorsePower().Weight();
      k.Weight().HorsePower().Fuel();
  </code></pre></li>
  </ul>