You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The test added to #555 shows that there will be no way to implement fluent API for components "correctly" (make component parameterized so that setters return parameter type not current one).
So
There is a way to add a Fluent API, completely out of the inheritance. The same could be used to add a DSL for Kotlin later ( additionally dependency). I created the solution and it looks like the following...
private final VerticalLayout layout = new VerticalLayoutBuilder(VerticalLayout::new)
.setDefaultHorizontalComponentAlignment(Alignment.START)
.setSizeUndefined()
.add(new HorizontalLayout(username , password))
.add(rememberMe)
.add(new HorizontalLayout(btnLogin , btnCancel))
.build();
private final Button btnCancel = new ButtonBuilder(Button::new)
.setId(BTN_CANCEL_ID)
.addClickListener(e -> clearFields())
.setText(getTranslation(BTN_CANCEL_CAPTION))
.setVisible(true)
.build();
Reacted by Giovanni Lovato, paulroemer and Alex Smith
Thanks for the effort @svenruppert ! but personally, I would hate having to write the following to instantiate something
new VerticalLayoutBuilder(VerticalLayout::new)
and I would never use this. There needs to be a simpler way. (less is more)
And as someone commented on our Slack, this would be a good thing to pitch as an add-on. IMO not part of @mstahv's Viritin, but something separate so that it would be possible to see how many people opt to use it. Viritin has bunch of other stuff too that make it harder to see what features should be promoted as official framework features.
A builder API could be made in a way that typically doesn't require the new VerticalLayoutBuilder(VerticalLayout::new) step, even though it would be kept there for the really advanced cases. Instead, there could be static builder factory methods in the class itself. In that way, the examples could be changed to look like this:
The biggest challenge in any way is that we would have to ensure the builder classes are always in sync with the actual components. It might be possible to use some kind of code generation library for automating this, but I'm not too familiar with such things.
To keep them in sync, I started working on an AnnotationProcessor. It is not too difficult.. With this approach, we could add support for custom components as well. (maybe a Pro)
Is there any gauge on how requested a fluent API still is?
I initially created one as kind of an experimental "bonus" in another add-on and wouldn't want to miss it anymore. I'm currently putting the finishing touches on moving it to its own project(s) where most of it is automatically generated.
And now I'm wondering if I should bother publishing it to the Vaadin Directory. Two other comparable add-ons I found didn't seem to get much attention and are outdated to varying degrees without anyone complaining.
ohh, an old topic. :-) Well, the best way would be inside the framework. It could be generated to be in sync with every version. But it is not on the prio list.. means.. it will not happen. If you created the new project, let me know. I would have a view on it to try it out. Cheers Sven
I have no idea how much of Viritin usage is for the fluent API it provides for the core components. Personally at least I have been using those quite much, kind of as a more readable/flexible constrcutor.
Lately, my thinking about this topic has evolved a bit though. I nowadays favor separate classes whenever I see n+1 (where n is currently somewhere between 0-2) setters or fluent configurations on an object. For example this snippet from above (derived to Viritin's API):
Even as anonymous class, it gives some structure, but as a named class it can be easily moved to the end of the class (and leave the more important details to be more readably in the beginning of the class. I actaully have an article in the publishing pipeline with a title "Don't be afraid of classes" :-)
But yeah, still, I'd appreciate the feature. It would be neat in certain cases, better than polluting constrcutors) and it would for sure be (ab)used a lot.
I went with the fluent wrapper route since that allows those (short lived) wrappers to provide all the conveniences you can come up with, while keeping the component API clean and concise. And unlike fluent subclasses, it also allows you to (re)configure any existing components, like those mapped from a template with @id.
An example with some layouting could look like this:
That way you won't even need shortcuts like new HorizontalLayout(username, password), because you can create your component hierarchy top down (you can create the parent layout before you create the child components which just feels more natural to me) and there's no sudden jump in complexity or fiddlyness when you need to do more than just add the component.
Activity
denis-anisimov commentedon Apr 21, 2016
The test added to #555 shows that there will be no way to implement fluent API for components "correctly" (make component parameterized so that setters return parameter type not current one).
So
won't work.
So it's better to use Builders instead.
macjuan commentedon Apr 27, 2016
Added to the backlog
Legioth commentedon Apr 27, 2016
Affects #495 and #494
svenruppert commentedon Feb 11, 2019
There is a way to add a Fluent API, completely out of the inheritance. The same could be used to add a DSL for Kotlin later ( additionally dependency). I created the solution and it looks like the following...
pleku commentedon Feb 11, 2019
Thanks for the effort @svenruppert ! but personally, I would hate having to write the following to instantiate something
and I would never use this. There needs to be a simpler way. (less is more)
And as someone commented on our Slack, this would be a good thing to pitch as an add-on. IMO not part of @mstahv's Viritin, but something separate so that it would be possible to see how many people opt to use it. Viritin has bunch of other stuff too that make it harder to see what features should be promoted as official framework features.
svenruppert commentedon Feb 11, 2019
ok
Legioth commentedon Feb 12, 2019
A builder API could be made in a way that typically doesn't require the
new VerticalLayoutBuilder(VerticalLayout::new)step, even though it would be kept there for the really advanced cases. Instead, there could be static builder factory methods in the class itself. In that way, the examples could be changed to look like this:The biggest challenge in any way is that we would have to ensure the builder classes are always in sync with the actual components. It might be possible to use some kind of code generation library for automating this, but I'm not too familiar with such things.
svenruppert commentedon Feb 12, 2019
Sure, the solution from me was assuming that there is no change in the existing framework and to support extended Components.
svenruppert commentedon Feb 12, 2019
To keep them in sync, I started working on an AnnotationProcessor. It is not too difficult.. With this approach, we could add support for custom components as well. (maybe a Pro)
jhult commentedon Feb 22, 2020
Just leaving this here as a plug for a great existing Kotlin DSL for Vaadin:
Frettman commentedon Nov 28, 2024
Is there any gauge on how requested a fluent API still is?
I initially created one as kind of an experimental "bonus" in another add-on and wouldn't want to miss it anymore. I'm currently putting the finishing touches on moving it to its own project(s) where most of it is automatically generated.
And now I'm wondering if I should bother publishing it to the Vaadin Directory. Two other comparable add-ons I found didn't seem to get much attention and are outdated to varying degrees without anyone complaining.
svenruppert commentedon Nov 28, 2024
ohh, an old topic. :-) Well, the best way would be inside the framework. It could be generated to be in sync with every version. But it is not on the prio list.. means.. it will not happen. If you created the new project, let me know. I would have a view on it to try it out. Cheers Sven
mstahv commentedon Nov 28, 2024
I have no idea how much of Viritin usage is for the fluent API it provides for the core components. Personally at least I have been using those quite much, kind of as a more readable/flexible constrcutor.
Lately, my thinking about this topic has evolved a bit though. I nowadays favor separate classes whenever I see n+1 (where n is currently somewhere between 0-2) setters or fluent configurations on an object. For example this snippet from above (derived to Viritin's API):
would become something like
Even as anonymous class, it gives some structure, but as a named class it can be easily moved to the end of the class (and leave the more important details to be more readably in the beginning of the class. I actaully have an article in the publishing pipeline with a title "Don't be afraid of classes" :-)
But yeah, still, I'd appreciate the feature. It would be neat in certain cases, better than polluting constrcutors) and it would for sure be (ab)used a lot.
Frettman commentedon Dec 1, 2024
I went with the fluent wrapper route since that allows those (short lived) wrappers to provide all the conveniences you can come up with, while keeping the component API clean and concise. And unlike fluent subclasses, it also allows you to (re)configure any existing components, like those mapped from a template with @id.
An example with some layouting could look like this:
The most interesting part here is probably the
.addTo(...)which condenses something liketo usually a one-liner like
That way you won't even need shortcuts like
new HorizontalLayout(username, password), because you can create your component hierarchy top down (you can create the parent layout before you create the child components which just feels more natural to me) and there's no sudden jump in complexity or fiddlyness when you need to do more than just add the component.Anyway, I created the release now (via Maven Central), so feel free to take a look: https://gitlab.com/codecamp-de/vaadin-flow-ui-toolkit#fluent-api-for-vaadin-flow
Any feedback is of course welcome.