CSLA 4.x ReadOnlyListBase sorting after populating from two different sources in list object

CSLA 4.x ReadOnlyListBase sorting after populating from two different sources in list object

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


JCardina posted on Thursday, April 12, 2012

I have a scenario where I need to populate a read only list object from two different data sources.  That's not a problem, I'm using encapsulated implementation and just issue two queries sequentially and populate the list first from the first query then secondly from the second query.

The heart of the issue is that the results returned need to be integrated alphabetically by name.  Right now all of query 1's items come first in the list and query 2's items come second.

I can think of all sorts of Rube Goldbergian ways of doing it but I'm wondering if there isn't a simpler way using LINQ or some other aspect of ROLB I'm unaware of to order the resulting list in place at the business object level before returning it to the UI level.

JonnyBee replied on Thursday, April 12, 2012

I often view this kind of sorting as a UI technology requirement - not necessarily the Business Object.

IE: For WindowsForms use SortedBindingList wrapper class.

For other UI technologies use LINQ and ToSyncList() extension method to get a sorted list that maps to the underlying list.

Example:

      var source = TestList.GetList(criteria);
      var synced = source.ToSyncList(from r in source
                                     orderby r.Name
                                     select r);

and then use synced as the source list in the UI.

 

 

JCardina replied on Thursday, April 12, 2012

Interesting and I'll certainly find that useful for any UI work I do but this object is part of a business object API used by 3rd party people who would expect the list to be in alpha order. 

Perhaps the ToSyncList method might be the answer if there is no way to re-order the existing ReadOnlyBase list.  However I'm not sure where ToSyncList comes from.  For example if I try your sample code in a unit test ToSyncList is unrecognized so it's not a method of ReadOnlyBase, where do I find that?

JonnyBee replied on Thursday, April 12, 2012

ToSyncList() is an extension method in LinqObservableCollection.cs (Csla 4.x).

RockfordLhotka replied on Thursday, April 12, 2012

To use extension methods you must have a using or Imports statement to bring the namespace into scope, then the extension methods should be available on any instance of a type that has been extended.

JCardina replied on Friday, April 13, 2012

RockfordLhotka

To use extension methods you must have a using or Imports statement to bring the namespace into scope, then the extension methods should be available on any instance of a type that has been extended.

Thanks Rocky, just for future reference the extension methods are in the CSLA namespace so a using reference to CSLA needs to be included.  I didn't pick it up automatically because I'm doing this in a unit test which doesn't normally reference CSLA namespace.

 

tiago replied on Thursday, April 12, 2012

/// <summary>
/// Sorts a FolderList ready for TreeView.
/// </summary>
/// <returns></returns>
public IOrderedEnumerable<FolderInfoSorted()
{
    return this.OrderBy(n => n.FolderName).OrderBy(p => p.ParentFolderID).OrderBy(l => l.FolderLevel);
}

JCardina replied on Friday, April 13, 2012

Tiago, I tried that but could not get it to work, the other method worked though.  Thank you though for posting.

JCardina replied on Friday, April 13, 2012

Ok, this works very well and just to be explicit for future readers here is how I implemented this in my quick test:

In my ReadOnlyListBase list object I've added a property:

 public LinqObservableCollection<PartListInfo> IntegratedSortedList

        { get {  return this.ToSyncList(from r in this orderby r.DisplayName select r);  } }

And confirmed it works (visually)  at the unit test level:

var v= c.IntegratedSortedList;

 StringBuilder sb = new StringBuilder();

foreach(PartListInfo i in v.QueryResult)               

  sb.AppendLine(i.DisplayName);

I realize the UI would normally be responsible for how the list is sorted in many cases but this is being called by third parties at the business object level who expect it to be sorted in advance so in this circumstance this is a useful technique.  Thanks everyone!

Copyright (c) Marimer LLC