M31.FluentAPI icon indicating copy to clipboard operation
M31.FluentAPI copied to clipboard

Extension methods outside builder for optional properties

Open Sam13 opened this issue 7 months ago • 4 comments

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;
}

Sam13 avatar Jul 08 '24 13:07 Sam13