Saturday, October 4, 2008

Databinding in WPF

Data binding in the user interface layer is nothing new. It has been around for quite some time, in various UI platforms, both for desktop and Web applications. The basic idea is that you "bind" the visual elements (controls) in a user interface to the data objects they are meant to display. The binding infrastructure then manages the data interactions from then on, so that modifications to the UI controls are reflected in the data objects, and vice versa. The major benefit of using data binding is that it reduces the amount of code the application developer needs to write.

The architects of some earlier UI platforms have done a good job integrating data binding with the rest of their framework. WPF's architects have done an amazing job integrating data binding with all aspects of their framework. Data binding in WPF is ubiquitous and seamless. It is so powerful and flexible that it literally forces you to change the way you think about designing and developing user interfaces.

With one simple API you can bind to domain/business objects, XML data, visual elements, ADO.NET data containers, collections, and basically anything else you can think of. You can use value converters to execute arbitrary data manipulation operations when bound values are passed back and forth. You can perform data validation by creating custom validation rules and applying them to a binding. The list goes on. Data binding in WPF is really a huge step forward.

A comparison

A developer typically creates a class to represent an entity (table) in the database, whereby CRUD operations are performed by calling methods on the objects, e.g; for class Employee, objEmployee.Save(), objEmployee.Load(), objEmployee.Update(), objEmployee.Delete() etc. Also, everytime the values in the object change, the developer has to manually write code to update each of the UI elements that display the values stored in the object's members. The same holds true when the values on the UI change. With WPF, you can lessen your code greatly by binding the object directly with the UI (each of object's members data-bound to individual UI elements) using XAML syntax. Adnan Farooq Hashmi does a very good comparison between the older approaches and the new approach in his blog.

The world of data binding

Josh Smith explores the world of data binding, and how it is put to use in the WPF Horse Race demo application in his article. His article will tickle your taste buds and you will desire more and for this you can turn on to the msdn article by Shawn Wildermuth where he gives a detailed explantion of the same. I guess that the wpf is incomplete with out the mention of binding converters and Amit has posted the following artcle WPF Binding Converter Best Practices.

Binding with LINQ to SQL

LINQ to SQL is an object-relational mapper (ORM) tool that generates business objects that match your database structure. When using LINQ to bind in a WPF interface, LINQ object already define the INotifyPropertyChanged interface, which means change notification is possible for any values edited from another process. Brian Mains has touche well on this topic in his good article 'WPF Data Binding, With LINQ'.

No comments: