ASP.NET Hosting


Archives / 2006 / September
  • 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.