<?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; INotifyPropertyChanged</title>
	<atom:link href="http://www.blog.dapfor.com/tag/inotifypropertychanged/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>.NetGrid v2.9.2 has been released.</title>
		<link>http://www.blog.dapfor.com/netgrid-v2-9-2-has-been-released</link>
		<comments>http://www.blog.dapfor.com/netgrid-v2-9-2-has-been-released#comments</comments>
		<pubDate>Mon, 05 Aug 2013 17:24:42 +0000</pubDate>
		<dc:creator>dapadm</dc:creator>
				<category><![CDATA[bug fixing]]></category>
		<category><![CDATA[new release]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[.Net Grid features]]></category>
		<category><![CDATA[INotifyPropertyChanged]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[release]]></category>

		<guid isPermaLink="false">http://www.blog.dapfor.com/?p=227</guid>
		<description><![CDATA[A significant increase in productivity when working with objects that implement the INotifyPropertyChanged interface The following bug has been fixed: [BUG] Fixed a bug where the grid redraw the content of the entire row upon receiving notification from INotifyPropertyChanged interface for missing or hidden column. [BUG] Fixed minor bugs in the designer.]]></description>
			<content:encoded><![CDATA[<p>A significant increase in productivity when working with objects that implement the <a href="http://msdn2.microsoft.com/es-es/library/ms133020" target="_blank">INotifyPropertyChanged</a> interface</p>
<p>The following bug has been fixed:</p>
<ul>
<li>[BUG] Fixed a bug where the grid redraw the content of the entire row upon receiving notification from <a href="http://msdn2.microsoft.com/es-es/library/ms133020" target="_blank">INotifyPropertyChanged</a> interface for missing or hidden column.</li>
<li>[BUG] Fixed minor bugs in the designer.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.dapfor.com/netgrid-v2-9-2-has-been-released/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why BindingList is slow with objects implementing INotifyPropertyChanged interface</title>
		<link>http://www.blog.dapfor.com/why-bindinglist-is-slow-with-objects-implementing-inotifypropertychanged-interface</link>
		<comments>http://www.blog.dapfor.com/why-bindinglist-is-slow-with-objects-implementing-inotifypropertychanged-interface#comments</comments>
		<pubDate>Thu, 29 Mar 2012 18:59:31 +0000</pubDate>
		<dc:creator>dapadm</dc:creator>
				<category><![CDATA[Data binding]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[BindingList]]></category>
		<category><![CDATA[INotifyPropertyChanged]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://www.blog.dapfor.com/?p=139</guid>
		<description><![CDATA[A BindingList&#60;T&#62; is the main tool for binding objects to grids. It is a container that may notify subscribers when data is added or removed. For this purpose it provides public ListChanged event that specifies collection changes with ListChangedType. Besides changing the collection itself (adding, removing, etc) the binding list notifies of data object changes. [...]]]></description>
			<content:encoded><![CDATA[<p>A <strong>BindingList&lt;T&gt;</strong> is the main tool for binding objects to grids. It is a container that may notify subscribers when data is added or removed. For this purpose it provides public ListChanged event that specifies collection changes with ListChangedType. Besides changing the collection itself (adding, removing, etc) the binding list notifies of data object changes. In other words, when data is added to collection <strong>BindingList&lt;T&gt;</strong> checks whether it implements <strong>INotifyPropertyChanged</strong> interface. If it is implemented, the collection subscribes to each object changes and forwards notifications as <strong>IBindingList.ListChanged</strong> events with <strong>ListChangedType.ItemChanged</strong> type and with specified PropertyDescriptor that is searched by reflection. </p>
<p>It’s worth mentioning that handler of <strong>INotifyPropertyChanged</strong> notification is very poorly implemented. Low performance reflection is used for searching PropertyDescriptor and internal cache is very poorly organized thus significantly reducing performance. Specifically when the <strong>BindingList&lt;T&gt;</strong> receives notifications of new object, it compares this object with the previous object that was used for notification. If they are not identical, the binding list does a terrible thing – it runs through all objects to get the index of the notifying object. This seriously impacts performance when there are a lot of elements. When the number of elements is about 100 000, handling of a single notification takes about 1 ms, which is unacceptable for most applications. </p>
<p>In actual applications <strong>ListChangedType.ItemChanged</strong> event with <strong>ListChangedType.ItemChanged</strong> flag and set PropertyDescriptor is handled quite rarely. However, it is hard to disable <strong>INotifyPropertyChanged</strong> handling in a <strong>BindingList&lt;T&gt;</strong> via simple means. The only acceptable method of disabling handling is to use reflection. The thing is that the binding list in its constructor checks whether the data type implements <strong>INotifyPropertyChanged</strong> interface and sets <strong>raiseItemChangedEvents</strong> private variable to true if it does. Otherwise it is set to false. This variable influences only subscriptions to <strong>INotifyPropertyChanged</strong> interface. There is no direct access to this variable and it can be modified only via reflection. An example of code for this is provided below. </p>
<pre>
class CustomBindingList&lt;T&gt; : BindingList&lt;T&gt;
{
    public CustomBindingList()
    {
        FieldInfo fi = typeof (BindingList&lt;T&gt;).GetField("raiseItemChangedEvents",
                               BindingFlags.Instance | BindingFlags.NonPublic);
        if (fi != null)
        {
            fi.SetValue(this, false);
        }
    }
}
</pre>
<p><br/></p>
<p>The above implements fully functional binding list with disabled handling of <strong>INotifуPropertyChanged</strong> interface events. At the same time the binding list notifies subscribers of adding or removing data just like in standard implementation of this container. </p>
<p>Some figures: In standard binding list implementation handling of 1000 notifications in 100 000 container took more than 600 msec. In optimized version this time – 0 since the container no longer subscribes to data objects. Besides that, no subscription enables memory saving. </p>
<p>This article concerns only binding list performance aspects. Another important aspect of working with binding lists <strong>INotifyPropertyChanged</strong> is their use in <a href="http://www.dapfor.com/en/net-suite/net-grid/tutorial/data-binding">advanced binding</a>.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.dapfor.com/why-bindinglist-is-slow-with-objects-implementing-inotifypropertychanged-interface/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
