Builder pattern in C# with nested objects

I’ve used the builder pattern a lot recently for writing unit test setups, filling data into in-memory database. It can be used with nice fluent syntax too.

var document = new DocumentBuilder()
  .WithName("My document")
  .WithIcon("mydoc.png")
  .Build();

But what I’ve been trying to figure out, what’s the preferred way to do nested objects (or parent-child-relationships). Let’s say that I’m trying to build a document folder tree. I have folders, inside folders I have documents and sub-folders.

Continue reading “Builder pattern in C# with nested objects”