Why is SortedBindingList<T> not serializable?

Why is SortedBindingList<T> not serializable?

Old forum URL: forums.lhotka.net/forums/t/197.aspx


Shazam posted on Wednesday, May 24, 2006

Hi there,
 
first post here.  I've been using CSLA to convert a project I'm working on, and have run into an annoying issue.
 
SortedBindingList<T> should be serializable. It sure would help in regards to Webforms.  When enabling both sorting and paging on something like a GridView, since SortedBindingList<T> can't be serialized, I have to always create the SortedBindingList<T> and call ApplySort when I'm sorting AND paging, which can take some time to run.  I would prefer to just sort once (when the user requests sorting), and during paging, simply rebind to the persisted SortedBindingList<T>.
 
I tried to add the [Serializable] attribute to SortedBindingList<T> and the inner class ListItem, but it didn't seem to work.  Any ideas?

Shazam replied on Thursday, May 25, 2006

I did miss marking the class SortedEnumerator, but it seems that System.ComponentModel.PropertyDescriptor is the problem, as it can't be serialized. Argg.

Shazam replied on Thursday, May 25, 2006

Ok, I found a workaround.
 
Add the [NotSerialized] attribute to the _sortBy variable in SortedBindingList<T>, and mark all classes in that file as [Serializable].  That will allow you to persist SortedBindingList<T> in SessionState, etc.  It should render the SortExpression getter unuseable between postbacks, however.

pfeds replied on Thursday, May 25, 2006

Why do you need to serialize it?  The SortedBindingList is a view of an IList, similar to a DataView of a DataTable in ADO.NET.  Your client will retrieve the IList (BusinessCollection) from the DataPortal, which will be serialized.  The SortedBindingList will display a sorted view on the client.  Think of the sorted list being a collection of pointers to the actual objects in the business collection.

Does that help?

 

Shazam replied on Thursday, May 25, 2006

I already mentioned why it needs to be persisted above.  Sorting is not free, and with something like a GridView, you have to resort every time a user changes pages on a sorted collection.

RockfordLhotka replied on Thursday, May 25, 2006

You should be very careful when doing something like this. As someone noted, the sorted list is merely a view over another list. If you serialize the sorted list, it will serialize the original list and all its child objects (and their children, etc.).

But if you also serialize the actual business object, then you'll end up with TWO byte streams containing the same data. When you deserialize you'll get TWO object graphs. The sorted list will NOT point to the actual business objects any more, but rather to a copy of the business objects.

This is why I didn't make SortedBindingList serializable - because it isn't real obvious that this would happen, and I can easily envision all sorts of unintended consequences coming from this - hard to find bugs, etc. Things where the user edits an object and then you call Save() and the changes aren't saved - because the user changed the copy rather than the real object - that sort of thing...

Shazam replied on Thursday, May 25, 2006

Yes, I do realize the implications of serializing the sortview.
 
Unfortunately this means that SortedBindingList is rather limited in usage for webforms.
 
My needs only require that ReadOnlyList be sorted.  I guess I'll have to roll my own solution to this then.

Copyright (c) Marimer LLC