Skip to content

Extension methods outside builder for optional properties #28

@Sam13

Description

@Sam13

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions