Find Your Books in Amazon.com with Silverlight

Introduction

Silverlight applications make it easy to perform a lot of actions. One of its greatest capabilities is to work with Web Services. In this article I will demonstrate how you can search books in Amazon.com with Silverlight. I have created a simple application which allows you to navigate through the great catalog of books very easily.

[ Online Demo | Source Code ]

Preparation

Amazon.com provides very functional web service. It allows you to perform lots of actions such as searching items, managing cart operations, etc. In this application I use Amazon Associates Web Service. It is a RESTful one. Access to it is free. You only need to register (for FREE) as a developer and you will get your own access key. You need to provide this key when you make requests to the service.

Application Structure

This image illustrates the simplest structure of this application.

As we use RESTful web service, we do not need to add any web service references to our project. There are lots of libraries specially created for consuming Amazon Web Service (including C# and VB.NET). However, we won’t use such a library in this project because its size is big and we do not need the whole functionality, which it provides.

Making Request

When making requests, we just need to send a HTTP request to the web service URL. Thus, I use a helper classes, which help me achieve this.

When calling the ToString() method of the UrlBuilder class, it returns a string liek this:

http://some_web_address.com/some_page.aspx?q1=v1&q2=v2&….

Parsing Response

Every response from the Amazon Web Service is in XML format, so we can “read” it in many ways. As I am a LINQ fan, we will use LINQ to XML to obtain the response data. XML Response contains a common part (which is common for every request) and an uncommon one. We are going to create an abstract base class, which will parse the common part. After that, we can create other child classes, which derive from our base class, and each of them parse a specific part of the response.

As we want to search only books, we create only a class BookParser, which derives from the ResultParser one. Here is a part from the Parse() method of BookParser.

User Interface

The main part of the user interface is the search control. It consists of search text box and various search options. When the user presses the Search button, the request is sent to the Amazon Web Service. When the web service sends a response, it is parsed by the corresponding parser class. There is a listbox (which we will modify) which shows the results. We are going to bind a list of Book objects, so here is our listbox item template:

TODO

  • Search on every search option change (for example when you choose to search in a specific category)
  • Add a cart support – you will be able to add books to a cart and then just go to Amazon’s website to choose a payment model

Summary

Due to its greatest web service support, Silverlight can communicate with every web site, so you can increase your business very efficiently. Using Silverlight to connect to remote stores makes it easy to support product data.

[ Online Demo | Source Code ]