Prev | Current Page 601 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"

The interesting part isn??™t the results??”we
won??™t be doing anything useful with the queries when we execute them??”but the
series of calls made up to the point of execution. We??™ll write types FakeQueryProvider
and FakeQuery. The implementation of each interface method writes out the current
expression involved, using a simple logging method (not shown here). Let??™s look first
at FakeQuery, as shown in listing 12.6.
class FakeQuery : IQueryable
{
public Expression Expression { get; private set; }
public IQueryProvider Provider { get; private set; }
public Type ElementType { get; private set; }
internal FakeQuery(IQueryProvider provider,
Expression expression)
{
Expression = expression;
Provider = provider;
ElementType = typeof(T);
}
internal FakeQuery()
: this(new FakeQueryProvider(), null)
{
Expression = Expression.Constant(this);
}
public IEnumerator GetEnumerator()
{
Logger.Log(this, Expression);
Listing 12.6 A simple implementation of IQueryable that logs method calls
Declares simple
automatic
properties
B
Uses this query as
initial expression
C
329 Translations using IQueryable and IQueryProvider
return Enumerable.Empty().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
Logger.Log(this, Expression);
return Enumerable.Empty().GetEnumerator();
}
public override string ToString()
{
return "FakeQuery";
}
}
The property members of IQueryable are implemented in FakeQuery with automatic
properties B, which are set by the constructors.


Pages:
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613