title: Implementing conditional in a fluent interface
body: <p>I've been trying to implement a fluent interface for a set of rules in my system. What I am trying to accomplish is this</p>
  
  <pre><code>TicketRules
  .RequireValidation()
  .When(quartType =&gt; quartType == QuartType.Before).TotalMilageIs(64)
  .When(quartType =&gt; quartType == QuartType.After).TotalMilageIs(128);
  </code></pre>
  
  <p>However, I have trouble implementing the When conditional how I intended to be. Currently, I need to call When() twice like in this snippet:</p>
  
  <pre><code>rules.When(param =&gt; param.Remarque == "Test").TotalMilageIs(100);
  rules.When(param =&gt; param.Remarque == "Other").TotalMilageIs(50);
  
  var params1 = new AddTicketParameters() { Remarque = "Test" };
  var params2 = new AddTicketParameters() { Remarque = "Other" };
  
  rules.ExecuteWith(params1);
  
  Assert.That(ticket.TotalMilage, Is.EqualTo(100));
  
  rules.ExecuteWith(params2);
  
  Assert.That(ticket.TotalMilage, Is.EqualTo(50));
  </code></pre>
  
  <p>My TicketRules class looks this:</p>
  
  <pre><code>[EditorBrowsable(EditorBrowsableState.Never)]
  public class TicketRules : ITicketRule, IHideObjectMembers
  {
      private Ticket theTicket;
  
      public Ticket Ticket
      {
          set
          {
              theTicket = value;
          }
      }
  
      private List&lt;ITicketRule&gt; allRules = new List&lt;ITicketRule&gt;();
  
      public TicketRules()
      {
      }
  
      public TicketRules(Ticket ticket)
      {
          theTicket = ticket;
      }
  
      public void Execute()
      {
          ExecuteWith(null, null);
      }
  
      public void ExecuteWith(AddTicketParameters param)
      {
          ExecuteWith(param, null);
      }
  
      public virtual void ExecuteWith(AddTicketParameters param, Ticket outsideTicket)
      {
          foreach (ITicketRule rule in allRules)
          {
              rule.ExecuteWith(param, theTicket ?? outsideTicket);
          }
      }
  
      public TicketRules RequireValidation()
      {
          CreateModifierRule(ticket =&gt; ticket.NeedValidation = true);
          return this;
      }
  
      public TicketRules TotalMilageIs(int milage)
      {
          CreateModifierRule(ticket =&gt; ticket.TotalMilage = milage);
          return this;
      }
  
      private void CreateModifierRule(Action&lt;Ticket&gt; function)
      {
          AddRule(new ModifierTicketRule(function));
      }
  
      internal void AddRule(ITicketRule rule)
      {
          allRules.Add(rule);
      }
  
      public WhenClauseTicketRule When(Predicate&lt;AddTicketParameters&gt; predicate)
      {
          WhenClauseTicketRule whenClause = new WhenClauseTicketRule();
          whenClause.Predicate = predicate;
  
          AddRule(whenClause);
  
          return whenClause;
      }
  
      public TicketRules UseStandardFormulaForTotalMilageAndTime()
      {
          AddRule(new StandardFormulaTicketRule());
          return this;
      }
  
      public TicketRules EnsureMinimumMilageIs(int milage)
      {
          AddRule(new EnsureMinimumMilageTicketRule(milage));
          return this;
      }
  }
  </code></pre>
  
  <p>the ITicketRules</p>
  
  <pre><code>internal interface ITicketRule : IHideObjectMembers
  {
      void ExecuteWith(AddTicketParameters param, Ticket ticket);
  }
  </code></pre>
  
  <p>I also need to support the subclasses of AddTicketParameters in the When clause (I've though maybe using generics for that part). I'm posting here because I'm all confused in my design and the Martin Fowler <a href="http://martinfowler.com/dslwip/" rel="noreferrer">articles</a> confuse me even more.</p>