All Projects → dasMulli → Data Builder Generator

dasMulli / Data Builder Generator

Licence: mit
Code generator to easily create data builder patterns for your model classes

Data Builder Generator

Allows to generate data builder patterns for your model classes.

Reference using Visual Studio 2019 16.6 or .NET CLI 3.1.500 / 5.0.100 or higher and opt into preview language features in your project file:

<PropertyGroup>
  <LangVersion>Preview</LangVersion>
</PropertyGroup>

<ItemGroup>
  <PackageReference Include="DasMulli.DataBuilderGenerator" Version="*" PrivateAssets="All" />
</ItemGroup>

Usage

Use the GenerateDataBuilder attribute to annotate the classes you wish to generate builder patterns for. If your class has constructors, be sure that the parameter names can be resolved to the name of a property:

    using DasMulli.DataBuilderGenerator;

    [GenerateDataBuilder]
    public class Person
    {
        public string FirstName { get; set; }
        public string? MiddleNames { get; set; }
        public string LastName { get; set; }

        public Person(string firstName, string? middleNames, string lastName)
        {
            FirstName = firstName;
            MiddleNames = middleNames;
            LastName = lastName;
        }
    }

Then you can use the generated builder class:

var martinBuilder = new PersonBuilder()
    .WithFirstName("Martin")
    .WithMiddleName("Andreas")
    .WithLastName("Ullrich");

var martin = martinBuilder.Build();

var otherMartin = martinBuilder.WithoutMiddleName().WithLastName("Foo").Build();

Notes

Special thanks to Mayr-Melnhof Karton AG for supporting the development of this project.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].