WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button Liberty on Beta 2

Creating an Application from Scratch, Part 2

by Jesse Liberty
01/31/2006
Use ClickOnce to Deploy Windows Applications

Back in November, I wrote a column in which I promised to create a project from scratch, while you watched. At the time I was calling the project Concord, but for no good reason I've renamed it PeopleLikeMe.

Well, time and clients and paying work intervened, and only this weekend have I actually started developing the project. Progress has been pretty good. Here's where things stand: I've created an ASP.NET 2.0 application with a page named UpdateRecords in which you enter your own AccountID from Amazon and create (or update) a record of all of your own reviews. (Soon to be implemented, it will also find the ID of everyone else who has reviewed the books you reviewed, and then find all the books they have reviewed.

The first cut of the database is fairly simple, consisting of only three related tables as shown in Figure 1.

As you can see, each book has an entry in the Books table and all the reviews for all of the books (by all of the reviewers) are stored in the Reviews table. Every reviewer has a ReviewerID (assigned by Amazon as the CustomerID) and that ReviewerID is tied to the user's security and personalization ID through the User_To_Reviewer table. It isn't clear that we need the Books table at all, as we may end up generating all the information we need dynamically through a query to the Amazon Web Service by ASIN (ASIN is Amazon's generic identifier for all items; for books this value equals the book's ISBN) but for now it is a convenience and not difficult to populate.

Figure 1
Figure 1. Our database starts out as just three related tables.

The Amazon Web Service

All of the data about user reviews is [1] gathered from the Amazon E-Commerce Web Service, which is infinitely safer and more convenient than screen-scraping (and eliminates the copyright infringement issue). To get started, you'll need an Amazon Web Services Account.

Begin by going to Amazon and clicking on their link for Web Services (Figure 2).

Figure 2
Figure 2. Link to Amazon Web Services

Follow the steps to obtain the necessary membership keys [2] by clicking on Create Your Free Amazon Web Services Account, and then navigate to the (free!) Amazon E-Commerce Service.

You may want to download the code samples which (as of this writing) contain an out-of-date sample .NET (1.x) control. You'll certainly want the documentation,which includes a PDF file that, once you realize it was written for non-.NET programmers, can help you figure out which objects you want to use and which properties to set. For example, to find all the reviews by a given user, you'll discover there is a CustomerFullResponseGroup and the documentation shows (in part) that the result of making a request for information about a specific customer returns the XML shown in Figure 3 (excerpt).

Figure 3
Figure 3. The XML returned by a REST request

I've put in arrows that indicate the tags that are translated into properties of the CustomerContentLookupResponse object which I then turned into C# code more or less like Example 1:


Customers[] customersFound = CustomerLookupResponse.Customers;
 if ( customersFound.Length > 0 )
 {
     Customer[] customerCollection = customersFound[0].Customer;
     CustomerReviews[] customerReviews =
       customerCollection[0].CustomerReviews;
     CustomerReviews custReviews = customerReviews[0];

    if ( pageNumber == 1 )
    {
       bool recordsCleared = ClearRecords( customerLookupRequest.CustomerId );
       if ( recordsCleared == false )
          break;
       totalPages = Convert.ToInt32( custReviews.TotalReviewPages );
       totalReviews = Convert.ToInt32( custReviews.TotalReviews );
    }

    Review[] reviews = custReviews.Review;
    foreach ( Review review in reviews )
    {
       InsertReviews(
          review.ASIN,
          customerLookupRequest.CustomerId,
          review.Rating,
          review.Summary,
          review.Content );
    }
    
Example 1 (Error checking elided)

Note: InsertReviews() is a method I wrote to insert this information into my database (not shown).

It turns out that to maintain maximum flexibility, many of these properties are collections, even when their property name is singular (and I hate that). For example, as shown in Example 1, the Customer property is actually a collection of Customer objects and the Review property is actually a collection of Review objects (why weren't these named Customers and Reviews?). You can also see that I make the (safe) assumption, given my query, that I will get back only zero or one customer, and that customer will have only zero or one set of reviews (though in the actual working code I do check these assumptions).

Programming ASP.NET

Related Reading

Programming ASP.NET
Building Web Applications and Services with ASP.NET 2.0
By Jesse Liberty, Dan Hurwitz

Pages: 1, 2, 3, 4

Next Pagearrow