1

In client-server technology, what is the best way to bind element between 'binding all attributes separately' and 'the entire component' (or another way) ?

For example in JSF :

-<h:inputText value="#{bean.value}" rendered="#{bean.rendered}" disabled="#{bean.disabled}" readonly="#{bean.readonly}" />

  • Bind only needed attributes
  • Declare a lot of attributes on bean, less readable, modify the view when a new attribute is binding
  • Other arguments...

-<h:inputText binding="#{bean.inputTextBinding}" />

  • Only one attribute declared on the bean, more readable
  • Require more space on the server to store the component
  • Other arguments...

Can you give more arguments, and tell what is recommended ? Personally, I prefer bind only needed attributes.

Jean-Charles
  • 1,690
  • 17
  • 28

2 Answers2

1

This is a good question, but I am personally in favor of binding only needed attributes as well.

•Only one attribute declared on the bean, more readable

I challenge this argument as I feel the it is actually LESS readable. I believe that your code and markup are the BEST documentation that can be provided for the behavior of a system. Code is written for the benefit of people, not machines, otherwise we would all be writing assembly.

Declaring your explicit attributes and individual bindings declares what the behavior of that component is, I cannot tell that looking at binding. At that point I have to dig deeper into the code to understand the nature of the component.

So really the only real arguments that you have for the second approach are that the page markup is smaller and it takes shorter amount of time to type. These are both specious arguments as file storage space is trivially inexpensive and also that typing is not what a developer spends the majority of his time on.

maple_shaft
  • 10,435
  • 6
  • 46
  • 74
  • @Everyone: What is your feeling about the storage of the bean on the server side (in session for example) : 'Store 3 or 4 boolean and a String' versus 'Store an UIComponent'. With multiple sessions, in the second way, the size of the memory used on the server will probably be bigger much faster? Or is it unsignificant? – Jean-Charles Sep 08 '11 at 08:05
  • @Jean-Charles, In most cases it is insignificant unless you are storing extremely large byte arrays in memory. I am also in favor of session scoped beans with managed properties as this helps promote seperation of model and view. I am actually also a big user of `@ViewScoped` beans as they maintain state for the life of a single page or view, but not persisting unused data in memory unnecessarily. Further they have an advantage over request scoped beans in that they limit database calls for each request and you don't need to worry about cleaning up resources, that is taken care of on disposal. – maple_shaft Sep 08 '11 at 11:21
1

I prefer value binding, tha main reason is value binding give me greater separation of the presentation layer from the model layer. Plus, if your work environment you have separate responsibilities for UI designers and Java programmers then a good separation becomes crucial.

Anyway each of these techniques has its advantages and disadvantages, have a look at Java EE tutorial, Binding Component Values and Instances to External Data Sources and analyze which one is best for your needs.

Victor Martinez
  • 1,102
  • 2
  • 8
  • 22
  • Interesting answer, I think you're right when you say each case is different. But for my needs, and I think in the majority of cases, I prefer bind only needed attributes for the reasons told by maple_shaft. Perhaps there isn't only one good answer for this question. – Jean-Charles Sep 08 '11 at 07:52
  • 1
    @Jean-Charles Have a look at [CDI-Context](http://download.oracle.com/javaee/6/tutorial/doc/giwhl.html) .IMO is the best option to hold state over the duration of the user’s interaction with the application using JSF – Victor Martinez Sep 08 '11 at 09:01