13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Translations us<strong>in</strong>g IQueryable and IQueryProvider329}}return Enumerable.Empty().GetEnumerator();IEnumerator IEnumerable.GetEnumerator(){Logger.Log(this, Expression);return Enumerable.Empty().GetEnumerator();}public override str<strong>in</strong>g ToStr<strong>in</strong>g(){return "FakeQuery";Overrides for}sake of logg<strong>in</strong>gEThe property members of IQueryable are implemented <strong>in</strong> FakeQuery with automaticproperties B, which are set by the constructors. There are two constructors: a parameterlessone that is used by our ma<strong>in</strong> program to create a pla<strong>in</strong> “source” for the query,and one that is called by FakeQueryProvider with the current query expression.The use of Expression.Constant(this) as the <strong>in</strong>itial source expression C is just away of show<strong>in</strong>g that the query <strong>in</strong>itially represents the orig<strong>in</strong>al object. (Imag<strong>in</strong>e animplementation represent<strong>in</strong>g a table, for example—until you apply any query operators,the query would just return the whole table.) When the constant expression islogged, it uses the overridden ToStr<strong>in</strong>g method, which is why we’ve given a short,constant description E. This makes the f<strong>in</strong>al expression much cleaner than it wouldhave been without the override. When we are asked to iterate over the results of thequery, we always just return an empty sequence D to make life easy. Production implementationswould parse the expression here, or (more likely) call Execute on theirquery provider and just return the result.As you can see, there’s not a lot go<strong>in</strong>g on <strong>in</strong> FakeQuery, and list<strong>in</strong>g 12.7 shows thatFakeQueryProvider is equally simple.DReturns emptyresult sequenceList<strong>in</strong>g 12.7An implementation of IQueryProvider that uses FakeQueryclass FakeQueryProvider : IQueryProvider{public IQueryable CreateQuery(Expression expression){Logger.Log(this, expression);return new FakeQuery(this, expression);}public IQueryable CreateQuery(Expression expression){Logger.Log(this, expression);return new FakeQuery(this, expression);}public T Execute(Expression expression){Logger.Log(this, expression);return default(T);Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!