chris carter's web log

Home |  Contact |  Admin
 

Enum Versus Boolean

Posted on August 20, 2008

I've been in discussions in the past about this one.  Let's say you have a method signature, and one of the parameters to the method can either be true or false.  What Type do you use? Boolean? seems simple enough.  But it's becoming increasingly annoying to have to discover boolean values in method signatures.

Recently I created a method with a signature like this:

IEnumerable<T> OrderBy(string propertyName, bool sortAscending)
1

Using the above signature, my client code looked like this:

myRepository.OrderBy("FirstName", true)
1

Reading that code kinda makes sense, but that true as the second arg is a mystery.  What's true?? Intellisense tells me, but only after I put effort into this; effort I don't want to have to put in when I'm reading code.  So I went back and changed it to this:

IEnumerable<T> OrderBy(string propertyName, Sort sortDirection)
1

Where Sort is an enum that looks like this:

public enum Sort {
  Ascending,
  Descending
}
1
2
3
4

What's the difference? When I'm skimming code, this:

myRepository.OrderBy("FirstName", Sort.Ascending)
1

is more readable than this

myRepository.OrderBy("FirstName", true)
1

I guess it depends on what you're building, but I like the more readable look rather than the other, at least if I'm publishing the interface.

Dynamic.cs: Yummy Linq Goodness

Posted on August 20, 2008

While testing out some more asp.net mvc stuff, I came up with a need to order a list of objects by property name(a string value).  Assuming I have a list of customers named customerList, i want to do something like: customerList.OrderBy("FirstName"), and get back a list of customers sorted by the FirstName property of each customer instance in the list.

Originally, I used something based on this post.  It looked like this:

public IEnumerable<TModel> OrderBy(string propertyName, bool sortAscending){

  if (String.IsNullOrEmpty(propertyName))
    throw new ArgumentNullException("propertyName");

  PropertyInfo property = typeof(TModel).GetProperty(propertyName);
  if (property == null)
    throw new NullReferenceException(
      String.Format("property '{0}' was not found on type '{1}'",
      propertyName,
      typeof(TModel).Name));

  if (sortAscending)
    return _models.OfType<TModel>().OrderBy(mo => property.GetValue(mo, null));
  else
    return _models.OfType<TModel>().OrderByDescending(mo => property.GetValue(mo, null));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

But then I found dynamic.cs located in the DynamicQuery project in the CSharpSamples.zip file.  Wicked cool shizzle. Now that method above looks like this:

public IEnumerable<TModel> OrderBy(string propertyName, Sort sortDirection){

  if (String.IsNullOrEmpty(propertyName))
    throw new ArgumentNullException("propertyName");

  return _models.AsQueryable<TModel>()
    .OrderBy(propertyName, sortDirection.ToString());
}
1
2
3
4
5
6
7
8

Now using my updated code looks like this:

IEnumerable<Customer> customers = customerRepository.OrderBy("FirstName", Sort.Descending);
1

I had to dig a little for this, but the signature on OrderBy, one of them anyway, looks like this:

IQueryable<T> OrderBy<T>(string ordering, params object[] values)
1

To use this, pass in the property name in the first spot, and "Ascending" or "Descending" in the second spot, like this:

customerList.AsQueryable<Customer>().OrderBy("FirstName", "Descending")
1

I felt like sticking the OrderBy method on my IRepository interface which, using your favorite subversion tool, can be checked out here:
http://svn.chrisjcarter.com/public/EPS

There's some really cool geeky code in dynamic.cs, check it out.