oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Creating an Application from Scratch, Part 2
Pages: 1, 2, 3, 4

Checking the Database

All of this information captured from Amazon is stored in the database table Reviews, as shown in excerpt in Figure 8.

Figure 8
Figure 8. Captured data stored in the Reviews table

As you can see, the ASIN, ReviewerID, and Rating are captured, along with the Summary and Full Review. The final two columns may not be needed any time soon, but it is painless to include them so I do. It would appear that the ReviewerID is not normalized, but in fact, once we run this for many different reviewers, we'll need the ReviewerID column to associate the rating given for any one book by the various reviewers and thus to match those other reviewers to the current user's ratings.

Since the user may write new reviews or edit existing reviews, rather than fussing with updating, I just delete all the records and re-insert them each time the program is run!

 // in btnFindUser_Click event handler...
 bool recordsCleared = 
    ClearRecords( customerLookupRequest.CustomerId ); 
private bool ClearRecords( string reviewerID )
   DataSourceSelectArguments sa = new DataSourceSelectArguments();
   ReviewsDataSource.DataSourceMode = SqlDataSourceMode.DataReader;
   ReviewsDataSource.SelectParameters.Add( "ReviewerID", reviewerID );
   SqlDataReader rdr = ReviewsDataSource.Select( sa ) as SqlDataReader;

   if ( rdr.HasRows == false )
      return true;


   ReviewsDataSource.DeleteParameters.Add( "ReviewerID", reviewerID );
   int numDeleted = 0;
   string msg = string.Empty;
      numDeleted = ReviewsDataSource.Delete();
   catch ( Exception ex )
      msg = ex.Message + ". ";
   msg += numDeleted.ToString() + " deleted.";
   lblMessage.Text = msg;
   if ( numDeleted == 0 )
      return false;
      return true;

Like many projects, the hardest part of this one is getting started. With this much done, the next few steps are almost automatic (get all the reviews for each ASIN listed, for each review obtained get the user ID, get all the reviews by that user, put it all in the DB.). The interesting part will come when it is time to score the users (do we do that dynamically or do we put the score in the DB, and how can we optimize all these calls to Amazon and to the DB and do we care?).

Another interesting set of questions will be to what degree we allow the user to control the scoring and/or the filtering when finding books highly rated by people who "match" the user's ratings of books they've both reviewed. As a quick example, suppose I have the following three users, A, B, and C. It turns out that A and B have shared reviews on only two books, but both awarded the same number of stars to those two books. A and C, however, have rated ten books in common, but their ratings tend to differ by one star in each case. Who is the better match for A: B or C?

All of these questions will be answered in future columns, but feel free to respond with suggestions, criticisms, complaints, and long diatribes about how if you had written this in PERL or (pick your choice) you'd have been done ages ago and already be reading your newly recommended books.


[1] There are some who argue that "data" is a plural noun and one should write "data are," but no American would ever say that if there weren't an editor or a teacher nearby.

[2] Amazon has changed from using the older-style keys that I use to a newer style. Please adjust your code accordingly if you have a new account.

Jesse Liberty is a senior program manager for Microsoft Silverlight where he is responsible for the creation of tutorials, videos and other content to facilitate the learning and use of Silverlight. Jesse is well known in the industry in part because of his many bestselling books, including O'Reilly Media's Programming .NET 3.5, Programming C# 3.0, Learning ASP.NET with AJAX and the soon to be published Programming Silverlight.

Read more Liberty on Beta 2 columns.

Return to