<commit_before>---
layout: post
title: A Fluent Builder in C#
author: <a href="https://arialdomartini.github.io"><NAME></a>, <a href="https://github.com/ale7canna"><NAME></a>, <a href="https://github.com/staffoleo"><NAME></a>
tags:
- C#
---
We show how we write our domain-focused, fluent builders that support nested entities by using lambdas, in C#.
<!--more-->

* [Builders with Fluent Interface](#builders-with-fluent-interface)
* [Nested Entities](#nested-entities)
* [A single, flat builder](#a-single-flat-builder)
* [Two separate builders](#two-separate-builders)
* [Lambdas](#lambdas)

## Builders with Fluent Interface
In our integration tests we make heavy use of builders designed with [Fluent Interface](https://www.martinfowler.com/bliki/FluentInterface.html).

Say we have a class `Order` with some properties:

{% highlight csharp %}
public class Order
{
    public int Number { get; set; }<commit_msg>Fluent builder post has code sample in excerpt<commit_after>---
layout: post
title: A Fluent Builder in C#
author: <a href="https://arialdomartini.github.io"><NAME></a>, <a href="https://github.com/ale7canna"><NAME></a>, <a href="https://github.com/staffoleo"><NAME></a>
tags:
- C#
---
{% highlight csharp %}
    var order = AnEmptyOrder()
        .WithNumber(10)
        .CreatedOn(new DateTime(2018, 12, 24))
        .ByACustomer(c => c
            .Named("Amelia")
            .Aged(42))
        .ContainingAnArticle(a => a
            .WithPrice(32.50m)
            .InCategory("books"))
        .Build();
{% endhighlight %}

We show how we designed our C#, domain-focused, fluent builders that support nested entities by using lambdas.

<!--more-->

* [Builders with Fluent Interface](#builders-with-fluent-interface)
* [Nested Entities](#nested-entities)
* [A single, flat builder](#a-single-flat-builder)
* [Two separate builders](#two-separate-builders)
* [Lambdas](#lambdas)

## Builders with Fluent Interface
In our integration tests we make heavy use of builders designed with [Fluent Interface](https://www.martinfowler.com/bliki/FluentInterface.html).

Say we have a class `Order` with some properties:

{% highlight csharp %}
public class Order
{
    public int Number { get; set; }
{'fraction_non_alphanumeric': 0.15176151761517614, 'fraction_numerical': 0.008130081300813009, 'mean_word_length': 3.9887387387387387, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, 'https://': 8, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1767326', 'n_tokens_mistral': 737, 'n_tokens_neox': 680, 'n_words': 202}