Declarative data binding
Posted on | February 9, 2012 | 1 Comment
Component model has two basic interfaces that are broadly used in data binding: INotifyPropertyChanged and IBindingList. The first interface notifies subscribers of bound object modifications while the second one notifies of object collection modifications. Object collections usually don’t contain hierarchy information and they are not intended for this purpose. Different grid developers use different ways to overcome this limitation. Some of them represent IBindingList as a collection containing objects linked by “id – parent id” relations, while the others expand IBindingList syntax adding hierarchy methods to it. We think that both solutions increase code overhead and reduce performance (which is critical for real time applications).
In our opinion, IBindingList should not contain hierarchy information. It is possible to use multiple binding lists to build it. For example, let’s take Strategy class that may contain multiple orders that can be dynamically added to this class or removed from it.
class Order { ... } public class Strategy { private readonly BindingList<Order> _orders = new BindingList<Order>(); public IList<Order> Orders { get { return _orders; } } } //Add a new order to a strategy: Order order = ...; strategy.Orders.Add(order); //Add a strategy to a grid Strategy strategy = ... grid.Rows.Add(strategy);
Let’s note that when a strategy is added to the grid, it cannot display orders as it doesn’t have hierarchy information. It is possible to use declarative binding to tell the grid that Strategy.Orders field should be viewed as hierarchy foundation.
public class Strategy { [HierarchicalField] public IList<Order> Orders { ... } }
When a strategy is added to the grid, it automatically subscribes to binding list returned by this strategy. It enables to use full-featured hierarchical binding with dynamic hierarchy modification without direct access to data presentation layer. To ensure complete separation of presentation layer from data layer we shall place strategy objects to IBindingList:
BindingList<Strategy> strategies = new BindingList<Strategy>(); //Populate collection with strategies ... //Bind collection to the grid grid.DataSource = strategies; ----- Strategies and orders will be presented in .Net Grid as following --------------- + Strategy |- Order |- Order | ... + Strategy |- Order |- Order | ...
Conclusion:
In our example we have achieved full separation of data from presentation. Data manipulation doesn’t require access to control, and this is true for hierarchical declarative data binding as well. Let’s also note that displayed data can be grouped, sorted and filtered without additional effort of the programmer.
In our opinion, the above hierarchy building method is one of the most efficient methods provided by the grid. Besides declarative binding, it is possible to build a hierarchy by simply adding data objects to any row. It is also possible to use conditional binding and binding on any hierarchy level.
Programmers working with actual applications know how much code is required to complete a seemingly simple task of representing hierarchy data in the grid and synchronize it with grid presentation (adding and removing rows, etc). They will definitely appreciate our efforts in this field.
For more information of declarative data binding click here.
Tags: .Net Grid features > databinding > hierarchy > IBindingList > INotifyPropertyChanged
Comments
One Response to “Declarative data binding”
Leave a Reply
March 22nd, 2012 @ 08:51
[...] for is the author of this article on hierarchy net grid. (function($) { $(function() { $("#getArticleCode").css({opacity: 0}).hide(); [...]