Does the framework have an implementation to Sort ReadOnly Lists?

Does the framework have an implementation to Sort ReadOnly Lists?

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


jspurlin posted on Saturday, November 18, 2006

Does the framework have an implementation to Sort ReadOnly Lists?

Thank you,
jspurlin

albruan replied on Saturday, November 18, 2006

There isn't an implementation, per se, in the CSLA framework to sort read-only lists; however, this link http://http://forums.lhotka.net/forums/post/5638.aspx to another post here in this site should be of some help.

jspurlin replied on Sunday, November 19, 2006

Thank you, that helped. This is pretty much a CSLA textbook example, but since I am sure others will search this topic in the future, here is what I did.

I have a basic class:  UserBasic
I have a collection class: UsersBasic

My DataSource binding routine (for selecting is as follows, the red highlights are explained below)

protected void BasicUsersDataSource_SelectObject(
      object sender, Csla.Web.SelectObjectArgs e)
    {
        try
        {
            object users = Session["currentObject"];
            if (users == null)
            {
                users = GetBasicUsers();
                Session["currentObject"] = users;
            }
            SortedBindingList<UserBasic> sortedList =
                new SortedBindingList<UserBasic>((ResourceManager.Library.UsersBasic)users);
            sortedList.ApplySort(GridSortExpression, ListDirection);
            e.BusinessObject = sortedList;
        }
        catch (System.Security.SecurityException ex)
        {
            this.ErrorLabel.Text = ex.Message;
        }
        catch (Csla.DataPortalException ex)
        {
            this.ErrorLabel.Text = ex.BusinessException.Message;
        }
        catch (Exception ex)
        {
            this.ErrorLabel.Text = ex.Message;
        }
    }


I add the method "OnSorting=GridView1_Sorting" to the GridView and the following to the page code behind:

#region Sorting

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        GridSortDirection = e.SortDirection;
        GridSortExpression = e.SortExpression;
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        SortPageIndex = e.NewPageIndex;
    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        if (SortPageIndex != -1)
            GridView1.PageIndex = SortPageIndex;
    }


    protected SortDirection GridSortDirection
    {
        get
        {
            object _sortDirection = ViewState["SortDirection"];
            if (_sortDirection == null)
                return SortDirection.Ascending;
            else
                return (SortDirection)_sortDirection;
        }
        set
        {
            ViewState["SortDirection"] = value;
        }
    }
    protected string GridSortExpression
    {
        get
        {
            object _sortExpression = ViewState["SortExpression"];
            if (_sortExpression == null)
                return String.Empty;
            else
                return _sortExpression.ToString();
        }
        set
        {
            ViewState["SortExpression"] = value;
        }
    }
    protected int SortPageIndex
    {
        get
        {
            object _index = ViewState["SortPageIndex"];
            if (_index == null)
                return -1;
            else
                return Convert.ToInt32(_index);
        }
        set
        {
            ViewState["SortPageIndex"] = value;
        }
    }

    protected System.ComponentModel.ListSortDirection ListDirection
    {
        get
        {
            if (GridSortDirection == SortDirection.Ascending)
                return ListSortDirection.Ascending;
            else
                return ListSortDirection.Descending;
        }
    }
    #endregion

The CSLA framework is a thing of beauty. I will find away to repay my gratitude to this community.
The CSLA framework exhibits a sublime "kiss" (keep it simple, (I personally do not use the word 'stupid') approach. My code is benefiting from not deviating from the proven implementations.

Plowking replied on Wednesday, January 24, 2007

Thanks for that, very nice. Here is a vb translation... (not an exact translation as my collection object wasnt sessioned...)

#Region " BasicUsersDataSource "

    Protected Sub BasicUsersDataSource_SelectObject(ByVal sender As Object, ByVal e As Csla.Web.SelectObjectArgs) Handles BasicUsersDataSource.SelectObject
      
        Try
            Dim Users As UsersBasic= UsersBasic.GetBasicUsers

            Dim sortedList As SortedBindingList(Of UserBasic) = New SortedBindingList(Of UserBasic)(Users)
            sortedList.ApplySort(GridSortExpression, ListDirection)
            e.BusinessObject = sortedList

        Catch ex As System.Security.SecurityException

            Me.ErrorLabel.Text = ex.Message
        Catch ex As Csla.DataPortalException
            Me.errorlabel.text = ex.BusinessException.Message
        Catch ex As Exception
            Me.ErrorLabel.text = ex.Message
        Finally

        End Try

    End Sub

#End Region

#Region " Sorting "

    Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
        gridsortdirection = e.SortDirection
        gridsortexpression = e.SortExpression
    End Sub

    Protected Sub GridView1_PageIndexChanging1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        sortpageindex = e.NewPageIndex
    End Sub

    Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound
        If Not SortPageIndex = -1 Then GridView1.PageIndex = SortPageIndex
    End Sub

    Protected Property GridSortDirection() As SortDirection
        Get
            Dim _sortDirection As Object = ViewState("SortDirection")
            If (_sortDirection Is Nothing) Then
                Return SortDirection.Ascending
            Else
                Return CType(_sortDirection, SortDirection)
            End If
        End Get
        set
            ViewState("SortDirection") = Value
        End Set
    End Property

    Protected Property GridSortBLOCKED EXPRESSION As String
        Get
            Dim _sortExpression As Object = ViewState("SortBLOCKED EXPRESSION
            If (_sortExpression Is Nothing) Then
                Return String.Empty
            Else
                Return _sortExpression.ToString
            End If
        End Get
        Set(ByVal value As String)
            ViewState("SortBLOCKED EXPRESSION = value
        End Set
    End Property

    Protected Property SortPageIndex() As Int32
        Get
            Dim _index As Object = ViewState("SortPageIndex")
            If (_index Is Nothing) Then
                Return -1
            Else
                Return Convert.ToInt32(_index)
            End If
        End Get
        Set(ByVal value As Int32)
            ViewState("SortPageIndex") = value
        End Set
    End Property

    Protected ReadOnly Property ListDirection() As ListSortDirection
        Get
            If (GridSortDirection.Equals(SortDirection.Ascending)) Then
                Return listsortdirection.ascending
            Else
                Return listsortdirection.Descending
            End If
        End Get

    End Property

#End Region

I completely agree, Kiss. Long live the mort! ;)


RockfordLhotka replied on Wednesday, January 24, 2007

I should also point out that in version 2.1.2, which is the exact version covered by my upcoming CSLA .NET Version 2.1 Handbook ebook, this is much smoother. Version 2.1.2 provides better and more direct integration with the ASP.NET data binding for the purpose of sorting.

I expect version 2.1.2 to go online this weekend. The VB ebook should be available then as well, with the C# ebook available in about a week.

Copyright (c) Marimer LLC