16
Mar
09

Composing Specifications

In my previous post, I suggested that composing specifications using my implementation may prove difficult. It turns out not to be so hard.

  public readonly Func<long, Predicate> IdEqualWithARunningSheet;
  public HasSpecifications()
  {
      IdEqualWithARunningSheet = (id => IdEqual(id).And(ARunningSheet()));
  }

Because the specifications are defined as instance members, we need to use the constructor to reference them, which is a shame. We could make the specification members static, but then the LINQ queries start to look odd.

The And method looks like this:

public static Expression<Func<T, bool>> And<T>(
        this Expression<Func<T, bool>> source,
        Expression<Func<T, bool>> other)
{
    return (Expression<Func<T, bool>>) Expression.Lambda(
                                               Expression.AndAlso(source.Body, other.Body),
                                               source.Parameters.ToArray());
}
Advertisements


%d bloggers like this: