Saturday 16 June 2012

Hierarchical binding: Bind various data very easily

For every application data binding is an important aspect without it no application is complete. There are various sources from where the application can get data such as array, data set etc. All these data needs to be implemented in the application to get the benefit out of it. One of the ways of binding data is by hierarchical binding. There is always a possibility that a data objects may contain hierarchical data which is to be implemented in the application by using the hierarchical binding technique. This type of hierarchical binding is not easy to achieve but with Dapfor’s features it can be done in a simple and easy manner. For example look at the hierarchical binding code below:
// data objects which implements the hierarchical binding capability
class Author
{
    private readonly IList<Book> _books = new List<Book>();
    private readonly string _name;

    public Author(string name)
    {
        _name = name;
    }

    public IList<Book> Books
    {
        get { return _books; }
    }
    public string Name 
    {
        get { return _name; }
    }
}

class Book
{
    private readonly Author _author;
    private readonly string _name;

    public Book(Author author, string name)
    {
        _author = author;
        _name = name;
    }

    public Author Author
    {
        get { return _author; }
    }
    public string Title 
    {
        get { return _name; }
    }
}
 The IBindingList interface contains all the authors and can be bounded on the grid for the purpose of implementing hierarchical binding by the following code:
//Populate binding list with some authors
BindingList<Author> authors = new BindingList<Author>();
Author author = new Author("Agata Kristi");
author.Books.Add(new Book(author, "Second front"));
author.Books.Add(new Book(author, "Shameful Star"));
authors.Add(author);

author = new Author("Conan Doyle");
author.Books.Add(new Book(author, "The Blanched Soldier"));
author.Books.Add(new Book(author, "The Mazarin Stone"));
authors.Add(author);

//Bind grid to author collection
grid.DataSource = authors;
But if you look carefully hierarchical binding is not formed because the grid doesn’t have the knowledge about the relation between the Author and Book class. This is where the usefulness of Dapfor hierarchical binding feature comes into the picture; it provides an attribute with which it notifies the grid about the hierarchical binding by marking the Author.Book relationship.
class Author
{
    ...

    [HierarchicalField]
    public IList<Book> Books
    {
        get { return _books; }
    }

    ...
}
If any author writes a new book which is to be added to the collection for the purpose of hierarchical binding, you can do use the following code:
/Add a book to a collection
author.Books.Add(new Book(author, "His Last Bow"));
Users won’t find any change because the Book collection implements the IList<Book>. To send notification about the changes users need to implement the BindingList<Book> instead of the IList. This will make the grid receive notification about the changes and displays them automatically. If some kind of sorting, filtering or grouping is required on the application which has hierarchical binding feature, the grid does the checking for the users.
class Author
{
    private readonly IList<Book> _books = new BindingList<Book>();

    ...
}
In this way, Dapfor grid provides the functionality of the hierarchical binding. Implementing the hierarchical binding is achieved very easily and less modification is required on the different classes.  

1 comment:

  1. The only constraints for data binding are that the server-side control must support a property called Data Source and a method called Data Bind(), and that the data source to which the control is bound implement the I Enumerable interface.
    http://www.dapfor.com/en/net-suite/net-grid/features/performance

    ReplyDelete