CSLA Filtering but over multiple Properties

CSLA Filtering but over multiple Properties

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


krtek posted on Friday, December 08, 2006

First of all, sorry about my english. Smile [:)]

I have a little problem. In ASP.NET application using CSLA I need to filter data before binding
them to the grid. For this puprose I find FilteredBindingList class.
But this class only allow me to filter using one property. Is there any way how to filter over
more properities.

Thanks Petr

JoeFallon1 replied on Friday, December 08, 2006

Brian Criswell has built a great control named ObjectListView which can do this.

I use it for sorting and filtering. He did a great job with it - I recommend it highly.

I think if you go to this page and download the latest file it is included as part of the overall project.

I split it out into a separate project on my C: drive and compiled it and then added the .dll as a reference to my main project.

http://www.codeplex.com/CSLAcontrib/SourceControl/ListDownloadableCommits.aspx

 

I added some code to my project to make calling the ObjectListView simple:

Public Shared Function GetObjectListView(ByVal list As IList, ByVal props() As String, ByVal directions() As ListSortDirection) As ObjectListView
 
If list IsNot Nothing AndAlso list.Count > 0 Then
   
Dim sort As New System.Text.StringBuilder
   
For i As Integer = 0 To props.GetUpperBound(0)
     
If sort.Length > 0 Then
       
sort.Append(", ")
     
End If
     
sort.Append(props(i))
     
If directions(i) = ListSortDirection.Descending Then
       
sort.Append(" DESC")
     
End If
   
Next
   
Return New ObjectListView(list, sort.ToString())
 
Else
   
Return Nothing
 
End If
End Function

Public Shared Function GetObjectListView(ByVal list As IList, ByVal prop As String, ByVal direction As ListSortDirection) As ObjectListView
 
If list IsNot Nothing AndAlso list.Count > 0 Then
   
Dim dir As String = ""
   
If direction = ListSortDirection.Descending Then
     
dir = " DESC"
   
End If
   
Return New ObjectListView(list, prop & dir)
 
Else
   
Return Nothing
 
End If
End Function

 

You can then call it like this from the UI:

Private mSortedList As ObjectListView

'Using the ObjectListView, single Property sorting is 1 line of code:
mSortedList = ePayUtil.GetObjectListView(mSomeList, mSort, mSortDirection)
dgAcct.DataSource = mSortedList
dgAcct.CurrentPageIndex =
CInt(ViewState("pageIndex"))
DataBind()

'multiple property sorting requires 3 lines of code - you need to build 2 arrays and call a function:
'Dim props() As String = {"fname", "lname"}
'Dim directions() As ListSortDirection = {ListSortDirection.Ascending, ListSortDirection.Descending}
'mSortedList = ePayUtil.GetObjectListView(mSomeList, props, directions)

Joe

Brian Criswell replied on Friday, December 08, 2006

I should probably add that filtering with the ObjectListView is basically the same as the System.Data.DataView in that it has a Filter property that takes SQL syntax.  For example:

MyList list = MyList.GetMyList();
ObjectListView view = new ObjectListView(list);
view.Filter = "StartDate > '20030401' or Id in (5, 6, 8) or Active = True";

brembot replied on Tuesday, December 19, 2006

I haven't  use ObjectListView before and it is highly recommended to some users in this forum if you have complex filtering in your application. Right now, i used Csla.FilteredBindingList and it works great. The changes i made to the filtered collection is also reflected to the source collection and i find it cool. Until the time i need to filter multiple properties and i find it difficult to implement it. I have some Q below.

1. Is ObjectListView works the same as DataView? What I mean is that if you have modification to the filtered collection it will be reflected also to the source collection?
2. I wonder why ObjectListView is not incorporate to the Csla Framework so that we can have one assembly? I think Rocky would appreciate you guys in helping Csla Framework.

Thanks and Merry Christmas

glenntoy replied on Tuesday, December 19, 2006

This is my point of view in #2 and its quiet OT, leave Csla Framework *as-is*, it would be much better to have another framework/library that contains all the extended features for Csla (like ObjectListView) and for sure this framework/library are efforts made by the community/people using Csla. :-)

Again it all depends on Rocky and the author of ObjectListView if they agreed to incorporate it.

Brian Criswell replied on Thursday, December 21, 2006

brembot:
I haven't  use ObjectListView before and it is highly recommended to some users in this forum if you have complex filtering in your application. Right now, i used Csla.FilteredBindingList and it works great. The changes i made to the filtered collection is also reflected to the source collection and i find it cool. Until the time i need to filter multiple properties and i find it difficult to implement it. I have some Q below.

1. Is ObjectListView works the same as DataView? What I mean is that if you have modification to the filtered collection it will be reflected also to the source collection?
2. I wonder why ObjectListView is not incorporate to the Csla Framework so that we can have one assembly? I think Rocky would appreciate you guys in helping Csla Framework.

Thanks and Merry Christmas

I have made the ObjectListView behave like the DataView as much as possible.  If you make a modification, it will pass that change down to the underlying list item.  It also has two methods of modifying sorting.  The business object in the list can implement an interface or the user of the ObjectListView can listen to an event and modifying sorting comparisons from there.  This means that you can have your business objects expose dates as strings for simple databinding, but it will sort properly as a date instead of as a string.

As for 2, this question comes up periodically.  I personally would have no problem with it being in the CSLA framework as this is my way of giving back to the community, but Rocky has stated in the past that CSLA is not an open source project with multiple contributors.  Instead, he has provided a place to share our modifications in CSLAcontrib, which may grow and evolve over time.

kayakheaven replied on Thursday, January 11, 2007

Brian when you say "have your business objects expose dates as strings for simple databinding, but it will sort properly as a date instead of as a string." Does that apply to both or one or other of the two methods of sorting (interface and event)? This is a really important point, can you summarize how this works?

Moving to object-oriented programming from data-centric programming (e.g. ADO.NET Datasets and DataTables) means we lose the behaviour associated with DB nullable values displaying as blank, which is quite useful so I'm very curious to know how this is supported.

Brian Criswell replied on Thursday, January 11, 2007

There are two ways of extending the sort to change how a property is sorted by the ObjectListView, the IExtendSort interface and the ExtendSort event.

IExtendSort is used by objects that know that one of their properties should be sorted by a different manner than the property's IComparable implementation.  An object implements the interface, and when the interface's method is called, the object's implementation of the method checks to see if the property being sorted is one that it needs to modify. An example would be for a StartDate property exposed as a string but internally a SmartDate.

object IExtendSort.GetSortValue(PropertyDescriptor property, object value)
{
    switch (property.PropertyName)
    {
       case "StartDate":
          return _startDate.Date;
    }

    return value;
}

So if the list of this object was sorting by StartDate, the method would return an actual date instead.  This would then sort properly by date.  Sorting by any other property would just return the value passed in to the method.

The ExtendSort event is for modifying the sorting of lists that you do not have control over.  So a list of objects from a third party library or an enumeration could be sorted in this manner.  In this case the event handler would modify a property on the event arg instead of returning a value.

With these methods you can return any value (including "null" values) when a specific property is sorted.  The only restriction is that for any property you modify the sort of, you should make sure that the type does not change.  In the example the StartDate sort always returns a DateTime.

xal replied on Friday, December 08, 2006

I believe you could pass an array with all the values you need to the filter method and handle that in your filter provider. You could even pass an object that has properties with values that you want to use for filtering.

Andrés

Copyright (c) Marimer LLC