Domanda

I am working on a PDF Invoice generator in Vaadin 7. The code as it stands at the time of this writing can be found here. The repo includes a crude class diagram.

My question concerns the best practice of collecting the user input from the TextField and other Vaadin components to create an instance of Invoice.

Currently it works like this:

  1. When the user clicks the button to generate the pdf, the class VaadinInvoiceGui (a Panel) calls the method createPdf(VaadinInvoiceGui gui) in the class VaadinInvoiceController.
  2. VaadinInvoiceController calls method getInvoiceFromForm(VaadinInvoiceGui gui) in class InvoiceMapperImpl.
  3. InvoiceMapperIml creates and returns an Invoice (POJO) by calling get methods in the VaadinInvoiceGui that gets passed to it. These getters return the values of the components in the view.
  4. VaadinInvoiceController takes the Invoice returned by InvoiceMapperImpl and goes on to create pdf from it etc..

The getters in VaadinInvoiceGui look like this.

    public String getCustomerName() {
        return infoPanel.getCustomerNameTextField().getValue().toString();
    }

    public String getCustomerStreet() {
        return infoPanel.getCustomerStreetTextField().getValue().toString();
    }

    public String getCustomerCity() {
        return infoPanel.getCustomerCityTextField().getValue().toString();
    }

    ...

I really don't feel like it's a good idea to pass the whole gui class to the controller and especially to the mapper, but I'm not sure what would be a better way of doing it. I could change the method createPdf(VaadinInvoiceGui gui) to something like createPdf(String customer name, String customerStreet, ...) but the number of parameters of the method would grow huge. I could do it using setters but then it would basically be doing the object mapping in the gui which doesn't seem like a very clean idea either.

What is the proper way of doing this?

È stato utile?

Soluzione

Write a bean for the data to pass around as your model. Then use the FieldGroup to bind between model and form. Wrap the model as BeanItem<Model>. Binding is either done by name (convention) or by annotation @PropertyId.

master-detail-example: https://vaadin.com/wiki/-/wiki/Main/Creating+a+master-details+view+for+editing+persons

general infos: https://vaadin.com/book/vaadin7/-/page/datamodel.html

binding in forms: https://vaadin.com/book/vaadin7/-/page/datamodel.itembinding.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top