This project uses Source Generation to create a FluentBuilder for a specified model-class or DTO.
$ dotnet add package FluentBuilderAnnotate a class with [AutoGenerateBuilder] to indicate that a FluentBuilder should be generated for this class:
[AutoGenerateBuilder]
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime? Date { get; set; }
}
This scenario is very usefull when you cannot modify the class to annotate it.
And annotate this class with [AutoGenerateBuilder(typeof(XXX))] where XXX is the type for which you want to generate a FluentBuilder.
[AutoGenerateBuilder(typeof(UserDto))]
public partial class MyUserDtoBuilder
{
}
using System;
namespace Test;
class Program
{
static void Main(string[] args)
{
var user = new UserBuilder()
.WithFirstName("Test")
.WithLastName("User")
.Build();
Console.WriteLine($"{user.FirstName} {user.LastName}");
}
}
For more information, see the StefH/FluentBuilder.
Entity Framework Extensions and Dapper Plus are major sponsors and proud to contribute to the development of FluentBuilder.