LINQ in Action - LINQ Book & News

Linq to Amazon source code

A while ago, I announced Linq to Amazon and started to describe how it's implemented.  Actually producing some content for the book and summer holidays kept me busy for a while, but here is finally the last part of this series of posts.

In the previous post, I said that what had to be done to create the Linq extension is to implement the IQueryable<T> interface, and write the code that converts an expression tree into an Amazon query.
What I have done is just a quick and dirty implementation. The goal is not to provide a complete solution to query Amazon using Linq, but instead to create an example to give you an idea of how the Linq extensibility stuff works. As you will see by looking at the source code, a lot of work would be required for a complete implementation.

Let's describe what you'll find in the source code.

The BookSearch class implements IQueryable<Book>. This interface speaks for itself. An object that implements it can be queried for Book objects! A BookSearch instance is what we'll query. Our queries start with code like the following:
var query =
  from book in new Amazon.BookSearch()
  ...

The important method to look for in the BookSearch class is CreateQuery<S>. This method receives an argument of type Expression. This is our expression tree. All that the CreateQuery<S> method does is creating and returning a new instance of the BookQuery class, passing the expression tree to its constructor.

The BookQuery class also implements IQueryable<Book>, but doesn't handle the same methods. First, an important thing to understand is that a BookQuery object keeps the expression tree it receives in its constructor as a private field. This will allow the BookQuery object to analyze the expression later on, when needed. The key point here is that a BookQuery instance represents a given query, which is defined by the expression tree. A different query produces a different expression tree of course.
The method to look at in the BookQuery class is IEnumerable.GetEnumerator. This is where everything happens: we parse the expression tree, we convert the Linq query into a web query, perform the web query, create a collection of the resulting books and return the book enumeration.

The other pieces of code you'll want to look at are the methods that visit the expression tree to extract the query criteria (the ProcessXXX methods), and the method that performs the web query using Linq to XML (the PerformWebQuery method).

I won't provide you with more details at this point. The source code (see below) is free for you to look at. Of course feel free to ask any question, and I'll do my best to help you.

Again, this is a straightforward implementation. This implementation supports only simple queries and is likely to fail if you try to use it with different queries... This code has been written without help from Microsoft. There is no documentation on expression trees or on how to implement IQueryable.

By the time the Linq in Action book is out, we should have more information from Microsoft and I'll adapt this sample as needed.

In the meantime, feel free to play with Linq's extensibility and to give a try to creating your own implementations! I'd be really happy if you let me know what you are up to :-)

Linq to Amazon source code => The source code has been updated for .NET 3.5 and VS 2008. It is available for download as part of the complete source code in C# and VB for the LINQ in Action book.

Note: In order to use the Amazon.com web services and test this example fully, you need to register with the Amazon Web Services program. After registering with Amazon you will be assigned an access key.  Edit the BookQuery.cs file and replace INSERT YOUR AWS ACCESS KEY HERE by your access key.

Published Thursday, September 7, 2006 8:44 PM by Fabrice Marguerie

Comments

 

Linq in Action News said:

As an example that will be included in the Linq in Action book, I've created an example that shows how...
September 7, 2006 1:26 PM
 

Paul Kinlan said:

Brilliant, I am just looking at the source now :)

Thanks
September 8, 2006 12:18 AM
 

Ferracchiati's italian blog said:

October 27, 2006 5:46 AM
 

Mike Street said:

I downloaded the source code to take a look at with the Orcas CTP Beta 1. Both projects in the source refer to an "Import Project" :

"<Import Project="$(ProgramFiles)\LINQ Preview\Misc\Linq.targets" />"

Can this import project be ignored, or is it needed in order to get the code to run ?

April 23, 2007 1:07 AM
 

Fabrice Marguerie said:

Mike, the import can be ignored because the new C# compiler supports LINQ out-of-the-box. It should be replace by the default import: <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

April 23, 2007 3:43 AM
 

Mike Street said:

I found the answer to my question above

April 23, 2007 4:15 AM
 

Mike Street said:

I'm now having problems figuring out the revised syntax in BookQuery.cs for MemberExpression and ExpressionType. I'm trying to understand how everything works so that I could attempt a LINQ to MultiValue implementation

April 23, 2007 4:37 AM
 

Fabrice Marguerie said:

I'll let you know when I have updated the code.

I'd be interested in seeing your implementation. Let me know when it's available.

April 23, 2007 4:44 AM
 

Paul Schofield said:

Are there plans to update Linq To Amazon for VS 2008 beta 2 or the final release?  I just upgraded to beta 2 and was looking forward to developing with Linq to Amazon.

Thanks!

October 28, 2007 7:33 PM
 

Fabrice Marguerie said:

We are just a few days/weeks away before we release the complete source code for the LINQ in Action book. This will include the updated source code of LINQ to Amazon, which will be valid for Beta 2 and the final VS and .NET releases. I'll keep everybody updated on this blog.

Be aware though that LINQ to Amazon is just a sample though and cannot be considered a complete tool you can use in your applications. Of course, anybody may build on the sample to improve it. If it happens, I expect the person to share the updated source code...

October 29, 2007 9:24 AM
 

Fabrice Marguerie said:

The complete source code for all the samples included in the LINQ in Action book is now available. This includes updated source code for LINQ to Amazon.

See http://linqinaction.net/blogs/main/archive/2007/12/12/linq-in-action-samples-source-code.aspx

December 12, 2007 5:29 AM
New Comments to this post are disabled