Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dasMulli/data-builder-generator

Code generator to easily create data builder patterns for your model classes
https://github.com/dasMulli/data-builder-generator

builder-patterns csharp-sourcegenerator

Last synced: about 1 month ago
JSON representation

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

Awesome Lists containing this project

README

        

# 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:

```xml

Preview

```

## 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:

```c#
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:

```c#
var martinBuilder = new PersonBuilder()
.WithFirstName("Martin")
.WithMiddleNames("Andreas")
.WithLastName("Ullrich");

var martin = martinBuilder.Build();

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

## Notes

Special thanks to [Mayr-Melnhof Karton AG](https://www.mayr-melnhof.com/) for supporting the development of this project.