Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
How to bind data with an external service provider?
I have just completed a toy application in Vaadin (and it's a great framework, thank you). My biggest hurdle was comprehending the Property/Item/Container paradigm. I had started the application by devising the data model using the sorts of practices I am used to. In this case the toy application was a Quiz. My model had a Quiz data object containing Question objects. Ignoring the purest practices of keeping a DTO just containing data, I went ahead and put some helper methods inside the DTO to return derived data like how many questions had been answered and how many were correct. These were derived from values in the Question.answer field. Furthermore, I had a variety of time taken/time left statistics inside that Quiz object all computing their answer from the system clock.
I learned along the way that this jars (sorry about the pun) with the Vaadin data binding architecture. Or least.. this is my question to you.
I ended up wrapping my Quiz & Question objects with BeanItem, and in quite a few cases I needed to remove the functionality from the data object into some kind of controller (ahem, maybe that is where it should have been in the first place?) which update the model via the Property interface rather than changing my data model directly. In the case of the timer statistics, when I wanted to mark all the timing related fields as being changed I had to iterate over the BeanItem<Question> properties (getItemPropertyIds()) and then cast each Property to be a MethodProperty so that I had access to MethodProperty.fireValueChange(). In this case I knew it was safe to cast to MethodProperty (because I was using BeanItem), but in other Property implementations there is no fireValueChange() exposed method and I notice that MethodProperty.fireValueChange() carries a warning about its exposure likely to be reduced in future versions.
So this all feels awkward - as if I am missing a different programming style.
My next more serious application would acquire data from a remote Service. This would be programmed using REST or RPC where my Vaadin app will need to ask the service provider to give me a DTO. How do you recommend I proceed?
Imagine then an application that looks up details of a product. Application would have this object lifecycle:
- create dummy DTO1
- create BeanItem on the DTO1
- create UI components, bind to the BeanItem
- take user request to look up product 100
- call the external service to look up product 100, receive a DTO2
- Copy, field by field, data from DTO2 into DTO1 via the Vaadin Property wrapper, this triggers...
- Data update to the frontend
- repeat from (4)
It is step (6) that feels wrong - how can I provide to the Property or Item or Container hierarchy a fresh object (graph) in one simple go; possible invalidating the whole tree?