-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extension methods outside builder for optional properties #28
Comments
Hi @Sam13, Thank you for this! What you want to achieve is already possible like so: [FluentApi]
public class Student
{
[FluentMember(0)]
public string FirstName { get; private set; }
[FluentMember(1)]
public string LastName { get; private set; }
[FluentMember(2)]
[FluentContinueWith(2)]
public string? FavoriteFood { get; private set; }
[FluentMember(2)]
[FluentContinueWith(2)]
public string? Hobby { get; private set; }
[FluentMethod(2)]
public void Create()
{
}
} A final method, called Student alice1 = CreateStudent
.WithFirstName("Alice")
.WithLastName("TestName")
.WithHobby("Reading")
.WithFavoriteFood("Pizza")
.Create(); Admittedly, the attributes that have to be specified for this group of optional properties are not very intuitive. Maybe we can come up with a better syntax for this. Happy coding! |
Hi @m31coding Those methods are still part of the builder, aren't they? As example the method of my first post public static Student WithHobby(this Student student, string hobby)
{
student.Hobby = hobby; // Probably set via reflection
return student;
} What do you think? |
Ah, now I understand your approach. As you pointed out, no dummy final method would be needed. However, the downside is that the extension method remains available after the instance is constructed, which undermines the immutability of the I lean towards bundling the methods for building the instance within the builder class. I agree that the final dummy method is not ideal. Another way to avoid it could be using an implicit conversion operator. For this to work, the fluent API would need to expose a class instance (rather than an interface) in the final step, allowing for the setting of optional properties and the implicit conversion to a |
That's exactly what I want - the extension methods should be available after construction. If you want immutable data structures you may use C# records? Assuming public static Student WithHobby(this Student student, string hobby)
{
return student with { Hobby = hobby };
} |
What about generating additional extension methods for optional properties which can be populated in any order after fluent builder was fully executed?
Example:
Usage:
Generated code (pseudo code):
The text was updated successfully, but these errors were encountered: