Skip to content
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

Fluent API enhancements #21

Open
jvmlet opened this issue Mar 16, 2023 · 4 comments · May be fixed by #39
Open

Fluent API enhancements #21

jvmlet opened this issue Mar 16, 2023 · 4 comments · May be fixed by #39
Assignees
Labels
enhancement New feature or request

Comments

@jvmlet
Copy link

jvmlet commented Mar 16, 2023

First of all, BIG thanks for the great library, having java background and getting used to Lombok, this functionality was really missing. Should be part of C# compiler ;-)

Suggestion :

  1. Having public abstract class Builder<T> where T : Builder<T> instead of public abstract class Builder<T> where T : class allows you to have fluent API by returning :
public T  WithObject(T value)  {
      Object = new System.Lazy<T>(() => value);
      return (T)this; //safe cast to T
  }
  1. I would expect that WithObject(obj).WithSomethingElse(somethingElse).Build() will alter SomethingElse property of original object obj, but looking at generated BuilderGenerator.Builder , this doesn't behave like this. Would you please provide support for such use-case ? Maybe generating c'tor that gets obj :
    new ConcreteBuilder(obj).WithSomethingElse(somethingElse).Build() to be backward compatible and not interfere with .1 ?

Thanks again for your work,

@MelGrubb
Copy link
Owner

These builders work a little different than most. There's not an instance of an object behind the scenes that's being mutated. There are a series of lazy properties that then get applied when you finally tell it to build the final product. My builders are more like blueprints. However, I can see that by rearranging things slightly, they could move the "Object" to the head of the line and then apply the other properties after that, which might achieve what you're after. I'm in the middle of a performance and caching push these days, but I'll see what I can do.

@MelGrubb MelGrubb self-assigned this Apr 17, 2023
@MelGrubb MelGrubb added the enhancement New feature or request label Apr 17, 2023
@jvmlet
Copy link
Author

jvmlet commented Apr 17, 2023

Btw, protobuf generates obj.toBuilder() method ... Very similar to what Lombok does

@MelGrubb MelGrubb linked a pull request May 28, 2023 that will close this issue
@MelGrubb
Copy link
Owner

I know it's been a while, but I have a newer release for testing. v2.4.0-alpha introduces a constructor that takes in an example object. It copies the current properties in to the backing fields as well as setting the behind-the-scenes object to the one passed in. I think this covers the scenario you described. I'm still trying to decide on the final shape of things and write some more tests, but I thought you might be interested in checking out the alpha.

@jvmlet
Copy link
Author

jvmlet commented Jan 16, 2024

Great, thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants