I have a the following domain classes:
public class Pony
{
public string Name { get; set; }
public DateTime FoundDate { get; set; }
}
public class Person
{
public ICollection<Pony> Ponies { get; private set; }
public Pony NewestPony
{
get
{
return Ponies
.OrderBy(pony => pony.FoundDate)
.FirstOrDefault();
}
}
}
The NewestPony
property encapsulates a domain rule that determines what the newest pony that lucky person has found.
I can use this property using the Enumerable
extension methods:
IEnumerable<Person> people = GetPeople();
people.Where(p => p.NewestPony != null);
This is great.
However, people
is an IQueryable
facade to a SQL Server (NHibernate in this case), the query has to be processed by a query provider and translated into SQL. Because there is no physical "NewestPony" column in the database, the query provider will choke on the query.
To solve this, I can expand the property in the query, replacing it with its get
implementation:
IEnumerable<Person> people = GetPeople();
person.Where(p
=> p.Ponies.OrderBy(pony => pony.FoundDate).FirstOrDefault() != null);
Because the query provider understands the relationships in SQL Server, it can evaluate this query now.
This solution, though workable, does duplicate that rule that was previously encapsulated. I'm looking for a way to avoid this duplication of behaviour, perhaps even a way to expand the property in the query provider to allow it to generate the correct SQL statements.