mercredi 1 juillet 2015

How to prevent JSF namespace pollution

I consider JSF have a namespace pollution in which I have to expose private members to the world that I shouldn't do.

Here's an example:

Suppose I have a form with From data and To date input forms, which represented in the backing beans as:

private Date fromDate;
private Date toDate;
// then getters and setters only so JSF XHTML page can see the fields and work with 'em

Then I have this private method, that is being called on post construct:

private List<? extends StatisticsModel> _getDataModel() {
    List<? extends StatisticsModel> dataModel = null;
    if (isSingleDate()) {
        dataModel = getDataModel(getFromDate(), getFromDate(),
                getSelected());
    } else {
        dataModel = getDataModel(getFromDate(), getToDate(),
                getSelected());
    }
    return dataModel;
}

which calls this abstract method that every subclass have to implement:

protected abstract List<? extends StatisticsModel> getDataModel(Date from, Date to, String[] selected);

If you look carefully, you will find that the abstract method from date and to date parameters are too critical and differ from the private fields.

But because of JSF, I have to make the private fields of date, to be public, hence the developer -who will work on subclasses - will be confuse ed between the dates sent to the method as args and the dates inherited as public properties from the super class.

For such case, I did a workaround by appending an under score on the front the private fields names (C trick), but I think this is not good enough.

Do you think this is a real problem? And How can we solve it?

Aucun commentaire:

Enregistrer un commentaire