Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 488723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:44:09+00:00 2026-05-13T01:44:09+00:00

I’m taking a crack at writing my first DSL for a simple tool at

  • 0

I’m taking a crack at writing my first DSL for a simple tool at work. I’m using the builder pattern to setup the complex parent object but am running into brick walls for building out the child collections of the parent object. Here’s a sample:

Use:

var myMorningCoffee = Coffee.Make.WithCream().WithOuncesToServe(16);

Sample with closure (I think that’s what they’re called):

var myMorningCoffee = Coffee.Make.WithCream().PourIn( 
                        x => {
                                x.ShotOfExpresso.AtTemperature(100);
                                x.ShotOfExpresso.AtTemperature(100).OfPremiumType();
                             }
                        ).WithOuncesToServe(16);

Sample class (without the child PourIn() method as this is what I’m trying to figure out.)

 public class Coffee
 {
   private bool _cream;

   public Coffee Make { get new Coffee(); }
   public Coffee WithCream()
   {
     _cream = true;
     return this;
   }
   public Coffee WithOuncesToServe(int ounces)
   {
     _ounces = ounces;
     return this;
   }
 }

So in my app for work I have the complex object building just fine, but I can’t for the life of me figure out how to get the lambda coded for the sub collection on the parent object. (in this example it’s the shots (child collection) of expresso).

Perhaps I’m confusing concepts here and I don’t mind being set straight; however, I really like how this reads and would like to figure out how to get this working.

Thanks,
Sam

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T01:44:09+00:00Added an answer on May 13, 2026 at 1:44 am

    Ok, so I figured out how to write my DSL using an additional expression builder. This is how I wanted my DSL to read:

    var myPreferredCoffeeFromStarbucks =
                Coffee.Make.WithCream().PourIn(
                    x =>
                        {
                            x.ShotOfExpresso().AtTemperature(100);
                            x.ShotOfExpresso().AtTemperature(100).OfPremiumType();
                        }
                    ).ACupSizeInOunces(16);
    

    Here’s my passing test:

    [TestFixture]
    public class CoffeeTests
    {
        [Test]
        public void Can_Create_A_Caramel_Macchiato()
        {
            var myPreferredCoffeeFromStarbucks =
                Coffee.Make.WithCream().PourIn(
                    x =>
                        {
                            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));
        }
    }
    

    And here’s my CoffeeExpressionBuilder DSL class(s):

    public class Coffee
    {
        public List<ExpressoExpressionBuilder> 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<ExpressoExpressionBuilder>()
                                 };
    
                return coffee;
            }
        }
    
        public Coffee WithCream()
        {
            HasCream = true;
            return this;
        }
    
        public Coffee ACupSizeInOunces(int ounces)
        {
            CupSizeInOunces = ounces;
    
            return this;
        }
    
        public Coffee PourIn(Action<ExpressoExpressionBuilder> action)
        {
            var expression = new ExpressoExpressionBuilder();
            action.Invoke(expression);
            expressoExpressions.Add(expression);
    
            return this;
        }
    
        }
    
    public class ExpressoExpressionBuilder
    {
        public readonly Queue<ExpressoExpression> ExpressoShots = 
            new Queue<ExpressoExpression>();
    
        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;
        }
    }
    

    Any and all suggestions are welcome.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.