Data Binding in MVP and MVC


Published: 2017-03-25
Updated: 2017-03-25
Web: https://fritzthecat-blog.blogspot.com/2017/03/data-binding-in-mvp-and-mvc.html


Data Binding has not been modelled as class-rectangle when MVC was invented ( UML did not yet exist at that time). It has been rendered as arrows between model and view (view as model listener), and controller and model (controller as model-updater). But, instead of using just controllers, we should implement bindings and controllers. Bindings being responsible for mapping model-properties to view-fields, and controllers being action-listeners to views.

What's Wrong?

Presentation-layer responsibilities are:

  1. Provide the possibility to set different model instances
  2. Map model properties to view fields
  3. Dispatch user events and perform according actions, mostly updating model-properties from view-fields
  4. User guidance by manipulating the view-state, e.g. enabling and disabling action-buttons

For MVP, all of that is done by the Presenter.
For MVC, (2.) is done partially by the view, everything else by the Controller.

I'd like to call (2.) a read-only data binding, because it is sufficient for display-only user-interfaces. The other way round, from view to model, always goes through some presentation-logic, but also ends up in some kind of mapping between view fields and model properties.

So, let's repeat:

We should try to write OO classes like we wrote structured-language functions:

one function has just one purpose
one class has just one responsibility

This is called the Single Responsibility Principle. I would propose to separate data binding as a separate responsibility. So neither controller nor presenter nor model nor view should know which model-property maps to which view-field.

Data Binding Specification

Binding mediates between model and view, it knows both of them. Thus either the view provides abstractions of its components (field, table, row, tree, node, ...), or the binding will be windowing-system dependent (like the view is).

Main responsibilities:

A data binding must cope with:

All of these can be moved to the field-side, i.e. fields can have converters, validators, there can be factories that create fields on the fly, fields can have a setReadOnly() method, can support setBuffered() and commit(). Finally, when using generic bean bindings (that bind all properties of a bean), an @Ignore annotation can hide a property.

Although these secondary responsibilities can be assigned to the view-side, data binding must work together closely with them. And mind that they will be windowing-system specific when being on the view-side!

Keep the View Anemic

Quite a lot of things that make up such a presentation layer. Where will we put all these?

Views suck. They suck up the application. The windowing system wants to be everywhere. That is what I observe all the time. Important and wise application logic ends up in some view class. Change the windowing system, and you will have to rewrite your application completely. Nobody wants to miss these nice presentation layer classes in Swing, JavaFX, Vaadin, ... (ssslurp, you have been sucked up :-)

Most people don't care about user interface implementations. They hire cheap students or beginners and rewrite the UI any time a new technology comes up. But they forget how expensive the bugfixing and maintenance of all that spaghetti code is, and they do not notice how the UI sucks up the application.

In my next Blog I will present an example where data binding has been implemented as separate class, keeping away all mapping logic from presenter and view. Measuring the lines of code needed on view side will show us how expensive rewriting views is.





ɔ⃝ Fritz Ritzberger, 2017-03-25