<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dapfor.Net Grid Blog &#187; databinding</title>
	<atom:link href="http://www.blog.dapfor.com/tag/databinding/feed" rel="self" type="application/rss+xml" />
	<link>http://www.blog.dapfor.com</link>
	<description>.Net Grid and MFC Grid</description>
	<lastBuildDate>Fri, 09 Sep 2016 05:40:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Binding list and thread safety</title>
		<link>http://www.blog.dapfor.com/binding-list-and-thread-safety</link>
		<comments>http://www.blog.dapfor.com/binding-list-and-thread-safety#comments</comments>
		<pubDate>Mon, 13 Feb 2012 21:05:28 +0000</pubDate>
		<dc:creator>dapadm</dc:creator>
				<category><![CDATA[Data binding]]></category>
		<category><![CDATA[thread safety]]></category>
		<category><![CDATA[databinding]]></category>
		<category><![CDATA[INotifyPropertyChanged]]></category>
		<category><![CDATA[threading]]></category>
		<category><![CDATA[threadsafety]]></category>

		<guid isPermaLink="false">http://www.blog.dapfor.com/?p=64</guid>
		<description><![CDATA[Data binding is the basis of modern applications based on separation of data layer from presentation layer. The main purpose of such separation is to make application logic independent of its representation. Otherwise, logic code should not directly call presentation layer class methods (i.e. Control class methods). When internal state changes, business logic layer sends [...]]]></description>
			<content:encoded><![CDATA[<p>Data binding is the basis of modern applications based on separation of data layer from presentation layer. The main purpose of such separation is to make application logic independent of its representation. Otherwise, logic code should not directly call presentation layer class methods (i.e. Control class methods). When internal state changes, business logic layer sends a GUI notification via <strong>IBindingList</strong> / <strong>INotifyPropertyChanged</strong> interfaces and the presentation layer displays modified data on screen. </p>
<p>It is necessary to note that there is a serious obstacle to implementing such event-driven model – thread safety. Business logic shouldn’t know anything of presentation, not only with regard to direct links to GUI classes but also with regard to thread used for subscriber notification. However, in actual applications things are more complicated. Application usually has a single GUI thread where the controls work. All control methods should be called only from this thread. However, data binding doesn’t consider this aspect, i.e. when a data source is connected, most controls imply that all notifications come from the main thread and don’t synchronize. This results in application crashes when notifications arrive from non-GUI thread. </p>
<p>While working with numerous real-world applications we have noted that developers often don’t follow the principle of separating data layer from the presentation layer because it is necessary to synchronize notifications with GUI thread. This means that synchronization is performed by business logic and not by the presentation layer. Thus, the required call of <strong>Control.Invoke</strong>/<strong>Control.BeginInvoke</strong> method is placed to business logic that should contain a reference to control that will process notifications. </p>
<pre>
class FeedCollection : BindingList&lt;Feed&gt;
{
    <strong>private Control _control;</strong>

    //The method is called when data is updated (for ex. data comes from TCP/IP)
    void OnUpdateReceiced(...)
    {
        _control.Invoke(new MethodInvoker(delegate
        {
            //Raise notification in GUI thread
            OnListChanged(new ListChangedEventArgs(...));
        }));
    }
}

DataGrid someGrid = ...;
FeedCollection feed = ...;
someGrid.DataSource = feed;
</pre>
<p><br/><br />
It is only one of the examples where rules of separating logic from presentation are violated. When our developers were working on .Net Grid, they have initially designed its architecture to receive notifications from any thread and to perform thread synchronization after that. Thus, business logic can be fully separated from the presentation layer and safely notify subscribers without pre-synchronization of threads and therefore without unnecessary references to GUI controls. </p>
<pre>
class FeedCollection : BindingList&lt;Feed&gt;
{
    //The method is called when data is updated (for ex. data comes from TCP/IP)
    void OnUpdateReceiced(...)
    {
        //Raise notification directly from non-GUI thread
        //Dapfor .Net Grid will synchronize threads itself.
        OnListChanged(new ListChangedEventArgs(...));
    }
}

Dapfor.Net.Ui.Grid grid = ...;
FeedCollection feed = ...;
grid.DataSource = feed;
</pre>
<p><br/></p>
<p>As we have said above, ensuring thread safety is not a trivial task for application developers. Complex modern applications may contain a  lot of assemblies. Some of them may contain codes with graphical controls, others may contain business logic, various math libraries, code  for TCP/IP interaction, etc. However, limitations related to GUI operation only in one thread and thread synchronization method require unneeded and dangerous dependencies of business logic from graphical components (Control.Invoke/Control.BeginInvoke).  This may seriously violate the principle of business logic independence from its presentation. Dapfor .Net Grid doesn’t just enable thread synchronization, but also makes it possible to completely avoid such dependencies using an event-driven model. It means that if the application is well architected,  business logic assemblies will not (and should not!) depend on Dapfor assemblies and System.Windows.Forms libraries.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.dapfor.com/binding-list-and-thread-safety/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Declarative data binding</title>
		<link>http://www.blog.dapfor.com/declarative-data-binding</link>
		<comments>http://www.blog.dapfor.com/declarative-data-binding#comments</comments>
		<pubDate>Thu, 09 Feb 2012 22:29:42 +0000</pubDate>
		<dc:creator>dapadm</dc:creator>
				<category><![CDATA[Data binding]]></category>
		<category><![CDATA[.Net Grid features]]></category>
		<category><![CDATA[databinding]]></category>
		<category><![CDATA[hierarchy]]></category>
		<category><![CDATA[IBindingList]]></category>
		<category><![CDATA[INotifyPropertyChanged]]></category>

		<guid isPermaLink="false">http://www.blog.dapfor.com/?p=39</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>Component model has two basic interfaces that are broadly used in data binding: <strong>INotifyPropertyChanged</strong> and <strong>IBindingList</strong>. 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). </p>
<p>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.</p>
<pre>
class Order
{
   ...
}

public class Strategy
{
    private readonly BindingList&lt;Order&gt; _orders = new BindingList&lt;Order&#038;gt();

    public IList&lt;Order&gt; 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);
</pre>
<p><br/>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 <strong>Strategy.Orders</strong> field should be viewed as hierarchy foundation. </p>
<pre>
public class Strategy
{
    <strong>[HierarchicalField]</strong>
    public IList&lt;Order&gt; Orders
    {
        ...
    }
}
</pre>
<p><br/>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 <strong>IBindingList</strong>:</p>
<pre>
BindingList&lt;Strategy&gt; strategies = new BindingList&lt;Strategy&gt;();
//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
 | ...
</pre>
<p><br/>Conclusion:<br />
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. </p>
<p>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.  </p>
<p>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. </p>
<p>For more information of declarative data binding click <a href="http://www.dapfor.com/en/net-suite/net-grid/features/hierarchical-data-binding">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.dapfor.com/declarative-data-binding/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
