SubSonic Forums
All Your Database Are Belong To Us

New AbstractList that inherits BindingList instead of List

Latest post 03-08-2008 2:27 PM by sengo. 7 replies.
  • 11-05-2007 6:21 PM

    New AbstractList that inherits BindingList instead of List

    I've changed the AbstractList class to inherit from BindingList<T> instead of List<T> to better support Windows Forms databinding. There are other advantages as well (including better event support) but that was the main reason I did it. Until now collections could not properly handle deletes. Now its possible. There isn't a downside that I can find. Implementing sorting took a little figuring out, but I think I've got that working - still testing that. Deletes are immediately propagated to the datasource, but there isn't any reason we can't implement deferred deletes (i.e. deletes that are handled when SaveAll() is called).

    Anyway, if there's any interest, I'll put together a patch and submit it. I'll be happy to share the code in the meantime.

  • 11-21-2007 3:52 AM In reply to

    Reply: New AbstractList that inherits BindingList instead of List

    Hi would you please share the code, I looked at subsonic for asp.net. But would like to try for a Windows forms applications I am working on. Would it be cheeky to ask what the sorting complication was :)
  • 11-24-2007 5:34 AM In reply to

    Reply: New AbstractList that inherits BindingList instead of List

    Thats cool,

    I tried to implement the IBindingList - Interface, because the BindingList - Class does not support the AddRange - Method and others.

    It works, but I think it is not very elegant.

    The List support now
    - Sorting
    - Finding
    - and an own simple FilterMethod (I 've not implemented IBindingListView)

    In the AbstractRecord class I have implemented IDataErrorInfo, INotifyPropertyChanged, IEditableObject.
  • 11-27-2007 11:40 AM In reply to

    Reply: New AbstractList that inherits BindingList instead of List

    Here's the relevant code. Its just the declaration for AbstractList and the Sort method. Be warned that I haven't tested the sort method but it should work.


    [Serializable]
    public abstract class AbstractList&lt;ItemType, ListType&gt; : BindingList&lt;ItemType&gt;, ITypedList
    where ItemType : AbstractRecord&lt;ItemType&gt;, new()
    where ListType : AbstractList&lt;ItemType, ListType&gt;, new()
    {
    public void Sort(string columnName, bool ascending)
    {
    if (!String.IsNullOrEmpty(columnName))
    {
    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(new ItemType());
    PropertyDescriptor myProperty = properties.Find(columnName, false);

    ListSortDirection sort = ListSortDirection.Descending;
    if (ascending)
    sort = ListSortDirection.Ascending;

    ApplySortCore(myProperty, sort);

    //ListComparer&lt;ItemType&gt; compare = new ListComparer&lt;ItemType&gt;();
    //ItemType item = new ItemType();
    //DbType dbType = item.GetDBType(columnName);
    //compare.Ascending = ascending;
    //compare.ColumnName = columnName;
    //compare.DBType = dbType;

    //Sort(compare);
    }
    }
  • 11-27-2007 11:45 AM In reply to

    Reply: New AbstractList that inherits BindingList instead of List

    Oh yeah - you'll also need to modify the CS_ClassTemplate.aspx template also. I simply added a RemoveItem implementation like so:


    [Serializable]
    public partial class &lt;%=className%&gt;Collection : ActiveList[&lt;]&lt;%= className%&gt;, &lt;%=className%&gt;Collection[&gt;]
    {
    public &lt;%=className%&gt;Collection() {}

    /// &lt;summary&gt;
    /// Provides out-of-the box support for immediate delete under windows forms databinding.
    /// &lt;/summary&gt;
    protected override void RemoveItem(int index)
    {
    &lt;%=className%&gt; &lt;%=className%&gt;Item = this[index];
    base.RemoveItem(index);
    &lt;%=className%&gt;.Delete(&lt;%=className%&gt;Item.&lt;%=thisPK%&gt;);
    &lt;%=className%&gt;Item = null;
    }
    }
  • 12-05-2007 5:00 PM In reply to

    Reply: New AbstractList that inherits BindingList instead of List

    Thank you,

    I have changed my AbstractList class to inherit from BindingList<T> instead of the IBindingList<T> - Interface. And now it works well.
  • 03-08-2008 2:27 PM In reply to

    • sengo
    • Not Ranked
    • Joined on 03-08-2008
    • Posts 4

    Re: Reply: New AbstractList that inherits BindingList<T> instead of List<T>

    Would you be able to share your implementation of IDataErrorInfo? 

Page 1 of 1 (7 items) | RSS