M31.FluentAPI
M31.FluentAPI copied to clipboard
Extension methods outside builder for optional properties
What about generating additional extension methods for optional properties which can be populated in any order after fluent builder was fully executed?
Example:
[FluentApi]
public class Student
{
[FluentMember(0)]
public string FirstName { get; private set; }
[FluentMember(1)]
public string LastName { get; private set; }
...
public string? FavoriteFood { get; private set; }
public string? Hobby { get; private set; }
}
Usage:
Student alice1 = CreateStudent
.WithFirstName("Alice").
.WithLastName("TestName")
.WithHobby("Reading")
.WithFavoriteFood("Pizza");
Student alice2 = CreateStudent
.WithFirstName("Alice").
.WithLastName("TestName")
.WithFavoriteFood("Pizza")
.WithHobby("Reading");
Generated code (pseudo code):
public static Student WithHobby(this Student student, string hobby)
{
student.Hobby = hobby; // Probably set via reflection
return student;
}