Skip to content

What is Data Binding

Michael edited this page May 4, 2016 · 6 revisions

With data binding, you connect elements of your user interface to your data model using a declarative approach.

Once such a connection is established, changes on either end are propagated to the other end.

The ultimate goal of data binding is to reduce boilerplate code that you would have to write to setup the user interface based on your data and to update your data in response to changes.

However, what happens naturally if you use data binding, is that you think differently about views, model data and view controllers and who should do what. It takes some getting used to, but what you get (in an iOS context) is this:

  • The data model is optimized to represent your data in a way that emphasizes on correctness, query performance and other issues related to persistent storage or what your backend needs are.
  • View models aggregate, transform or otherwise process model data to provide a view on that data that is suitable for user interaction or presentation.
  • Views (User Interfaces) determine the layout, design and UI functionality of a user interaction (-process).
  • Views and view models share a formal contract defined by all binding expressions in views (one side) and all KVC compatible properties of the view model (the other side).
  • View Controllers manage the life cycle of their views, select and connect view model and views and manage other aspects of how a user interaction relates to the application and other parts of the user interface.

The contract between view models and views, even though not actionable at this time (no validation support yet), has an important aspect: It decouples views (the concrete user interface with colors, fonts, formats, geometry) from the abstract process (what data is presented, which actions are offered to the user, what is the state of the process). The good thing about that is that, whenever to modules are decoupled, you can unplug them and replace a part with another, as long as the plug is compatible. Even if you don't plan to and effectively won't use this feature, data binding promotes creating such architectures and after some time even thinking in such terms.