Christian
First, here's another thread where this subject has already been discussed:
http://forums.lhotka.net/forums/thread/899.aspx
To address your specific question about the IComparer interface, yes there is a way to do it within the CSLA construct. The way we handled this was to overload the ApplySort methods (we have several) and add "IComparer comparer" as an additional parameter. You can either store that as a private variable or pass it to the DoSort method as an argument.
Then, if you look closely at the code for DoSort, you will see that sorting is actually deferred to the _sortIndex list. The Sort method for a System.Collection.Generic.List<T> object has overloads that will do what you want. In our case we also implemented range sorting using "index" and "count" arguments as well as allowing developers to use their own IComparer objects for sorting. All of these can be passed into the _sortIndex.Sort() methods within the DoSort routine without any additional changes to the code or logic, such as:
_sortIndex.Sort(comparer);
You don't need to check for null because the underlying List control handles that for you.
Hope that helps.
Nevermind. I figured out the problem.
Thanks anyway.
Here is how I did to make it work for me. It may work for you for multiple properties sorting...
I updated CompareTo method in private class ListItem : IComparable<ListItem> class shown in red. That is the ONLY change I made to CSLA 2.0 solution.
public
int CompareTo(ListItem other){
object target = other.Key;
//made IComparable a generic type
if (Key is IComparable<T>)
return ((IComparable<T>)Key).CompareTo((T)target);
...
...}
Then implemented CompareTo method for IComparable interface in the class which was inheriting from Csla.BusinessBase.
eg. I am sorting Form object by LineOfBusinessTypeiD, FormPrefix, and then FormNumber
public
partial class Form : Csla.BusinessBase<Form>, IComparable<Form>#region
IComparable Memberspublic
int CompareTo(Form y){
//compare by LOB Type first
//coalesce to non-null values for comparison
int xLOB = (int?)this.LineOfBusinessTypeID ?? -1;
int yLOB = (int?)y.LineOfBusinessTypeID ?? -1;
if (xLOB < yLOB)
{
return -1;
}
else if (xLOB > yLOB)
{
return 1;
}
else
{
//sort by form Prefix
if ((this.FormPrefix.CompareTo(y.FormPrefix)) < 0)
{
return -1;
}
else if ((this.FormPrefix.CompareTo(y.FormPrefix)) > 0)
{
return 1;
}
else
{
//sort by form Number
if ((this.FormNumber.CompareTo(y.FormNumber)) < 0)
{
return -1;
}
else if ((this.FormNumber.CompareTo(y.FormNumber)) > 0)
{
return 1;
}
else
{
return 0;
}
}
}
}
#endregion
To use this,
SortedBindingList<Form> sortedList = new SortedBindingList<Form>(TempformsList);
//calls Form.CompareTo IComparable method which sorts by LOB, FormPrefix, then FormNumber. Keep PropertyName field blank (1st parameter in ApplySort).
sortedList.ApplySort("", System.ComponentModel.ListSortDirection.Ascending);
foreach (Form form in sortedList)//add sorted forms to formsList
formsList.Add(form);
Finally, formsList will contain a sorted list of forms by LineOfBusinessTypeID, FormPrefix and FormNumber.
The problem I have with this solution is that it makes modifications to the SortedBindingList class where you are making the framework aware of your Subclass T. This may be defined in your implementation of CSLA or by making CSLA dependent on another assembly that has T defined. Also it is kind of embedding a one-off solution in a more general framework.
Don't get me wrong, I'm a newbie to C# and CSLA. I'm not trying to tell everyone their business but this is what strikes me as a possible problem...
I would prefer using the ApplySort(IComparer comparer) method but I've not seen any code on it. If anyone could email me such code, I'd appreciate it greatly!!! Aztec_2Step ( @ ) hotmail.com
Copyright (c) Marimer LLC