Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, so I figured out how to write my DSL using an additional expression builder. This is how I wanted my DSL to read:</p> <pre><code>var myPreferredCoffeeFromStarbucks = Coffee.Make.WithCream().PourIn( x =&gt; { x.ShotOfExpresso().AtTemperature(100); x.ShotOfExpresso().AtTemperature(100).OfPremiumType(); } ).ACupSizeInOunces(16); </code></pre> <p>Here's my passing test:</p> <pre><code>[TestFixture] public class CoffeeTests { [Test] public void Can_Create_A_Caramel_Macchiato() { var myPreferredCoffeeFromStarbucks = Coffee.Make.WithCream().PourIn( x =&gt; { x.ShotOfExpresso().AtTemperature(100); x.ShotOfExpresso().AtTemperature(100).OfPremiumType(); } ).ACupSizeInOunces(16); Assert.IsTrue(myPreferredCoffeeFromStarbucks.expressoExpressions[0].ExpressoShots.Count == 2); Assert.IsTrue(myPreferredCoffeeFromStarbucks.expressoExpressions[0].ExpressoShots.Dequeue().IsOfPremiumType == true); Assert.IsTrue(myPreferredCoffeeFromStarbucks.expressoExpressions[0].ExpressoShots.Dequeue().IsOfPremiumType == false); Assert.IsTrue(myPreferredCoffeeFromStarbucks.CupSizeInOunces.Equals(16)); } } </code></pre> <p>And here's my CoffeeExpressionBuilder DSL class(s):</p> <pre><code>public class Coffee { public List&lt;ExpressoExpressionBuilder&gt; expressoExpressions { get; private set; } public bool HasCream { get; private set; } public int CupSizeInOunces { get; private set; } public static Coffee Make { get { var coffee = new Coffee { expressoExpressions = new List&lt;ExpressoExpressionBuilder&gt;() }; return coffee; } } public Coffee WithCream() { HasCream = true; return this; } public Coffee ACupSizeInOunces(int ounces) { CupSizeInOunces = ounces; return this; } public Coffee PourIn(Action&lt;ExpressoExpressionBuilder&gt; action) { var expression = new ExpressoExpressionBuilder(); action.Invoke(expression); expressoExpressions.Add(expression); return this; } } public class ExpressoExpressionBuilder { public readonly Queue&lt;ExpressoExpression&gt; ExpressoShots = new Queue&lt;ExpressoExpression&gt;(); public ExpressoExpressionBuilder ShotOfExpresso() { var shot = new ExpressoExpression(); ExpressoShots.Enqueue(shot); return this; } public ExpressoExpressionBuilder AtTemperature(int temp) { var recentlyAddedShot = ExpressoShots.Peek(); recentlyAddedShot.Temperature = temp; return this; } public ExpressoExpressionBuilder OfPremiumType() { var recentlyAddedShot = ExpressoShots.Peek(); recentlyAddedShot.IsOfPremiumType = true; return this; } } public class ExpressoExpression { public int Temperature { get; set; } public bool IsOfPremiumType { get; set; } public ExpressoExpression() { Temperature = 0; IsOfPremiumType = false; } } </code></pre> <p>Any and all suggestions are welcome.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload