RSGC-Builder Design Pattern – part 8

 

 

name data-builder-generator
nuget

https://www.nuget.org/packages/DasMulli.DataBuilderGenerator/

link https://github.com/dasMulli/data-builder-generator
author Martin Andreas Ulrich

Implements the Builder Design pattern for any class. Useful , at least, for test projects
 

The code that you start with is


    [GenerateDataBuilder]                               

    public class Person

    {

         public string FirstName { get; set; }

         public string? MiddleNames { get; set; }

         public string LastName { get; set; }

         

    }


The code that you will use is



    var pOld = new Person();                                                                              

    pOld.FirstName = "Andrei";

    pOld.LastName = "Ignat";

    pOld.MiddleNames = "G";

    var build = new PersonBuilder(pOld).WithoutMiddleNames().WithFirstName("Florin");

    var pNew = build.Build();

    Console.WriteLine(pNew.FirstName);

 

The code that is generated is


    public partial class PersonBuilder                                         

         {

              private string? _firstName;

              private string? _middleNames;

              private string? _lastName;

              public PersonBuilder()

              {

              }

    

              public PersonBuilder(PersonBuilder otherBuilder)

              {

                   _firstName = otherBuilder._firstName;

                   _middleNames = otherBuilder._middleNames;

                   _lastName = otherBuilder._lastName;

              }

    

              public PersonBuilder(Person existingInstance)

              {

                   _firstName = existingInstance.FirstName;

                   _middleNames = existingInstance.MiddleNames;

                   _lastName = existingInstance.LastName;

              }

    

              public PersonBuilder WithFirstName(string firstName)

              {

                   var mutatedBuilder = new PersonBuilder(this);

                   mutatedBuilder._firstName = firstName;

                   return mutatedBuilder;

              }

    

              public PersonBuilder WithMiddleNames(string? middleNames)

              {

                   var mutatedBuilder = new PersonBuilder(this);

                   mutatedBuilder._middleNames = middleNames;

                   return mutatedBuilder;

              }

    

              public PersonBuilder WithoutMiddleNames()

              {

                   var mutatedBuilder = new PersonBuilder(this);

                   mutatedBuilder._middleNames = null;

                   return mutatedBuilder;

              }

    

              public PersonBuilder WithLastName(string lastName)

              {

                   var mutatedBuilder = new PersonBuilder(this);

                   mutatedBuilder._lastName = lastName;

                   return mutatedBuilder;

              }

    

              public Person Build()

              {

                   var instance = new Person();

                   if (!(_firstName is null))

                        instance.FirstName = _firstName;

                   if (!(_middleNames is null))

                        instance.MiddleNames = _middleNames;

                   if (!(_lastName is null))

                        instance.LastName = _lastName;

                   return instance;

              }

         }

Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/DP_Builder

All RSCG

NrBlog Post
1RSCG–part 1
2RSCG- AppVersion–part 2
3http://msprogrammer.serviciipeweb.ro/2021/02/17/rsgc-enum-part-3/
4RSGC-JSON to Class- part 4
5RSGC-Constructor – Deconstructor – part 5
6RSGC – DTO Mapper – part 6
7RSGC – Skinny Controllers- part 7
8RSGC-Builder Design Pattern – part 8
9RSGC- MetadataFromObject – part 9
10RSGC- Dynamic Mock – part 10
11RSCG- Method Decorator – part 11
12RSCG – Curry – Partial function – part 12
13RSCG- part 13 – IFormattable
14RSCG- part 14 – DP_Decorator
15RSCG- part 15 – Expression Generator
16RSCG- part 16 – Many Others
17RSCG- the book
18RSCG–Template Rendering- part 17
19CI Version
20HttpClientGenerator
21Query from database
22AutoRegister
23TinyTypes
24Static2Interface
25AppSettings
26Properties
27
Roslyn Source Code Generators