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.

330 CHAPTER 12 LINQ beyond collections}}public object Execute(Expression expression){Logger.Log(this, expression);return null;}There’s even less to talk about <strong>in</strong> terms of the implementation of FakeQueryProviderthan there was for FakeQuery. The CreateQuery methods do no real process<strong>in</strong>g but actas factory methods for FakeQuery. The Execute method overloads just return emptyresults after logg<strong>in</strong>g the call. This is where a lot of analysis would normally be done, alongwith the actual call to the web service, database, or whatever the target platform is.Even though we’ve done no real work, when we start to use FakeQuery as thesource <strong>in</strong> a query expression <strong>in</strong>terest<strong>in</strong>g th<strong>in</strong>gs start to happen. I’ve already let sliphow we are able to write query expressions without explicitly writ<strong>in</strong>g methods to handlethe standard query operators: it’s all about extension methods, this time the ones<strong>in</strong> the Queryable class.12.2.3 Glu<strong>in</strong>g expressions together: the Queryable extension methodsJust as the Enumerable type conta<strong>in</strong>s extension methods on IEnumerable to implementthe LINQ standard query operators, the Queryable type conta<strong>in</strong>s extensionmethods on IQueryable. There are two big differences between the implementations<strong>in</strong> Enumerable and those <strong>in</strong> Queryable.First, the Enumerable methods all use delegates as their parameters—the Selectmethod takes a Func, for example. That’s f<strong>in</strong>e for <strong>in</strong>-memorymanipulation, but for LINQ providers that execute the query elsewhere, we need a formatwe can exam<strong>in</strong>e more closely—expression trees. For example, the correspond<strong>in</strong>goverload of Select <strong>in</strong> Queryable takes a parameter of type Expression. The compiler doesn’t m<strong>in</strong>d at all—after query translation, it hasa lambda expression that it needs to pass as a parameter to the method, and lambdaexpressions can be converted to either delegate <strong>in</strong>stances or expression trees.This is the reason that LINQ to SQL is able to work so seamlessly. The four key elements<strong>in</strong>volved are all new features of <strong>C#</strong> 3: lambda expressions, the translation ofquery expressions <strong>in</strong>to “normal” expressions that use lambda expressions, extensionmethods, and expression trees. Without all four, there would be problems. If queryexpressions were always translated <strong>in</strong>to delegates, for <strong>in</strong>stance, they couldn’t be usedwith a provider such as LINQ to SQL, which requires expression trees. Figure 12.3shows the two paths taken by query expressions; they differ only <strong>in</strong> what <strong>in</strong>terfacestheir data source implements.Notice how <strong>in</strong> figure 12.3 the early parts of the compilation process are <strong>in</strong>dependentof the data source. The same query expression is used, and it’s translated <strong>in</strong> exactly thesame way. It’s only when the compiler looks at the translated query to f<strong>in</strong>d the appropriateSelect and Where methods to use that the data source is truly important. At thatLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!