CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

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

Answered (Verified) This post has 1 verified answer | 8 Replies | 3 Followers

Top 50 Contributor
184 Posts
JCardina posted on Thu, Apr 12 2012 3:14 PM

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.

Answered (Verified) Verified Answer

Top 10 Contributor
1,765 Posts
Verified by JCardina

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.

 

 

Jonny Bekkum, Norway CslaContrib Coordinator

All Replies

Top 10 Contributor
1,765 Posts
Verified by JCardina

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.

 

 

Jonny Bekkum, Norway CslaContrib Coordinator

Top 50 Contributor
184 Posts

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?

Top 25 Contributor
450 Posts
/// <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);
}

Tiago Freitas Leal, CslaGenFork (Open Source CSLA code generator)

Top 10 Contributor
1,765 Posts
JonnyBee replied on Thu, Apr 12 2012 10:09 PM

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

Jonny Bekkum, Norway CslaContrib Coordinator

Top 10 Contributor
9,270 Posts

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.

Rocky

Top 50 Contributor
184 Posts

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.

 

Top 50 Contributor
184 Posts

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!

Top 50 Contributor
184 Posts

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

Page 1 of 1 (9 items) | RSS

Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems