The sample code we’ve looked at so far has been reasonably simple. Let’s look at a more difficult problem—building a delimited list composed of values that are calculated from object properties.
Suppose we have a simple class named Party:
PartyDemo.cs (excerpt)
public class Party
{
public Party(DateTime partyDate)
{
this.partyDate = partyDate;
}
public DateTime PartyDate
{
get
{
return partyDate;
}
}
DateTime partyDate;
}
Consider the scenario that we need to concatenate instances of this class together. The desired output is a pipe-delimited list of the number of days between now and the SomeDate value.
Solution
Our first step is to determine where to place the logic that will perform the calculation and concatenation. The best place for this logic is in a predicate method that’s called from the collection’s Join method.
Performing predicate-based operations on your generic collections can really simplify and enhance your code.
The following example, in which we concatenate our Party objects, is not only useful on its own—it should also help to demonstrate the thought process behind moving from loop-based logic to clean, simple code that leverages predicates.
Let’s start by defining a new Join method that can take in a delimiter, an enumeration, and an instance of the converter delegate. The converter delegate has the following signature:
delegate TOutput Converter
As an argument to the Join method, we specify that TOutput should be a String, leaving the input as a generic object:
PartyDemo.cs (excerpt)
public static string Join
, IEnumerable
, Converter
{
StringBuilder builder = new StringBuilder();
foreach(T item in items)
{
builder.Append(converter(item));
builder.Append(delimiter);
}
if (builder.Length > 0)
{
builder.Length = builder.Length – delimiter.Length;
}
return builder.ToString();
}
With this method defined, we can concatenate an Array or collection of Party instances, like so:
PartyDemo.cs (excerpt)
Party[] parties = new Party[]
{
new Party(DateTime.Parse(“1/23/2006″))
, new Party(DateTime.Parse(“12/25/2005″))
, new Party(DateTime.Parse(“5/25/2004″))
};
string result = Join
, delegate(Party item)
{
TimeSpan ts = DateTime.Parse(“11/24/2006″) – item.PartyDate;
return ((int)ts.TotalDays).ToString();
});
Console.WriteLine(result);
Note that we make use of an anonymous delegate that examines an instance of Party and calculates the number of days that have passed since PartyDate. This calculation returns a string that will be concatenated to the previous item in the list.
That code produces the following output: 305|334|913.
Discussion
Here’s what you’ll gain by moving from “dumb” loops to predicate-based operations:
*
Your code will be more reusable, since you can reuse generic methods with different objects. A good example of this is the ActiveRecord implementation in SubSonic, an open source Data Access Layer that we’ll be exploring in detail in 17 “Advanced Topics”.
*
Your code will leverage framework methods rather than require you to write your own repetitive code. Less custom code means fewer custom bugs.
*
Your library code will be likely to perform better, since it will be strongly typed.
Tip: Where to Sort and Filter
In this solution, and those prior to it, we’ve looked at ways to sort and filter objects in the application layer. But don’t take this to mean that you should necessarily handle tasks like this in your application code rather than in the database. Generally, databases will be more efficient when it comes to sorting and filtering, although complex operations like string manipulation are better handled by application code.