Silverlighttm 4 Problem – Design – solution Nick Lecrenski 534045ffirs.indd 3 3/16/10 9:35:31 AM Silverlighttm 4: Problem – Design – Solution Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2010 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-0-470-53404-5 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions. Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or pro- motional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the pub- lisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read. For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. Library of Congress Control Number: 2009940875 Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trade- marks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. Silverlight is a trademark of Microsoft Corporation in the United States and/or other countries. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. 534045ffirs.indd 4 3/16/10 9:35:31 AM Contents IntroductIon xv ChaPter 1: FitnesstraCkerPlus 1 Problem 2 Design 3 Determining Requirements for the Site 3 Silverlight 4 Features 6 Solution 10 Chapter 2 10 Chapter 3 11 Chapter 4 11 Chapter 5 11 Chapter 6 11 Chapter 7 12 Chapter 8 12 Chapter 9 12 Chapter 10 12 Chapter 11 13 Chapter 12 13 Getting Started 13 Summary 17 ChaPter 2: PrePare to Be PoPular 19 Problem 19 Design 20 Physical N-Tier Design 21 Load Balancing 22 Cloud Computing 22 Logical N-Tier Design 22 FitnessTrackerPlus Application Design 78 Solution 83 Physical Tier 84 Logical Tier 84 Summary 86 534045ftoc.indd 9 3/13/10 4:48:56 PM Contents ChaPter 3: sign Me uP 87 Problem 87 Design 89 Home Page 90 User Registration 92 Login Control 97 Solution 98 Main Landing Page 98 Home View 107 User Registration 109 Login Control 134 Dashboard 138 Supplemental Pages 139 URI Mapping 140 Summary 140 ChaPter 4: WelCoMe hoMe 141 Problem 141 Adding Navigation 142 Providing Site Announcements 142 User-Selectable Themes 142 Account Settings 143 Adding a Dashboard 143 Design 143 User Home Page 144 Navigation Menu 154 Site Announcements 155 Account Settings 156 Theme Selection 157 Fitness Summaries 158 Solution 158 Global Variables 159 Navigation Menu 159 User Home Page 165 Dashboard 169 Account Settings 172 Theme Selection 180 Site Announcements 186 Fitness Summaries 191 Summary 191 x 534045ftoc.indd 10 3/13/10 4:48:56 PM CONTENTS ChaPter 5: one More sliCe Can’t hurt 193 Problem 193 Design 194 User Stories 195 Requirements 196 Food Log 196 Solution 200 User Interface 201 Database 214 Data Access 214 Business Logic 214 User Interface Code Behind 217 Summary 232 ChaPter 6: tiMe to hit the gyM 233 Problem 234 Design 234 User Stories 235 Requirements 235 Exercise Log 236 Solution 241 User Interface 242 Database 255 Data Access 256 Business Logic 257 User Interface Code Behind 262 Summary 278 ChaPter 7: aM i Working hard enough? 279 Problem 279 Design 280 User Stories 280 Requirements 281 Measurement Log 282 Calculator Controls 287 Solution 289 Measurement Log Page 289 Calculator Controls 307 Summary 311 xi 534045ftoc.indd 11 3/13/10 4:48:56 PM Contents ChaPter 8: unFinished Business 313 Problem 314 Design 314 Food Summary 315 Exercise Summary 317 Measurement Summary 318 Printing Support 319 Solution 319 Food Summary 320 Exercise Summary 333 Measurement Summary 338 Summary 347 ChaPter 9: sharing your suCCess 349 Problem 350 Design 351 Public Journal Settings 352 Public Journal 353 Solution 357 Public Journal Settings 358 Public Journal 365 Summary 386 ChaPter 10: soCial netWorking 387 Problem 387 Design 388 MySpace 388 MySpace Silverlight SDK 389 FitnessTrackerPlus — MySpace Application 391 Requirements 394 Solution 397 Getting Started 397 FitnessTrackerPlus MySpace Application 400 Summary 421 ChaPter 11: this site doesn’t run itselF 423 Generating Revenue for FitnessTrackerPlus 423 Problem 423 xii 534045ftoc.indd 12 3/13/10 4:48:56 PM CONTENTS Design 424 Advertising-Based Solutions 424 Google AdSense 424 Recurring Monthly Fees 426 Which Revenue Solution is the Right One? 428 Solution 429 Google AdSense 429 PayPal 436 Summary 461 ChaPter 12: let’s go live 463 Problem 463 Design 464 Supplemental Pages 464 Shared Hosting Providers 468 Solution 468 Summary 487 Index 489 xiii 534045ftoc.indd 13 3/13/10 4:48:56 PM 1 FitnessTrackerPlus An Overview of the FitnessTrackerPlus Application In the fall of 2008, Microsoft released an update to its new media-centric browser plug-in Silverlight. This update probably wasn’t very exciting to end users who may have had Silverlight installed to watch the 2008 summer Olympics, but to developers who make use of Microsoft web technologies, a monumental change had occurred. Silverlight 2.0 finally included the long- awaited support for C#/VB.NET programming directly in the Silverlight world. No longer were developers forced to utilize the plug-in with a JavaScript-based API only. With the addition of C#/VB.NET support, it seemed like Silverlight may have finally made the necessary jump required in order to support line-of-business-style applications. Although a dramatic improve- ment from its predecessor, Silverlight 2 still left a lot to be desired in terms of line-of-business controls. Luckily, Microsoft has heard developers loud and clear and made several important enhancements in version 3 that specifically addressed line-of-business needs. Although the Silverlight 3 release meant developers had a powerful toolkit at their disposal for creating rich internet applications, some features were still omitted that were absolutely crucial in transition- ing line-of-business applications from the desktop to the web. However, with the latest release of Silverlight version 4, these features have finally made it into the runtime, including a new Printing API, RichTextArea, Clipboard API, mouse wheel support, Implicit theming, and right- click event handling to name a few. Features like printing, right-click menus, and clipboard support offer some of the final pieces of the line-of-business missing from the Silverlight puzzle and may, in fact, make this latest release of Silverlight the most exciting yet for developers. It is the primary intent of this book to introduce you to some of these great new features as well as show you some advanced techniques that you can apply to the development of your own Silverlight-based line-of-business solutions. In this book I will be following the Wrox Problem- Design-Solution style so that each chapter contains a detailed problem statement followed by a design analysis and solution implementation. I will not be discussing detailed API information for Silverlight and the various controls; however, all of this information is available by down- loading the official Silverlight 4 documentation from http://www.silverlight.net. This book will be taking you through the creation of a fully functional Silverlight 4 line-of-business 534045c01.indd 1 3/13/10 4:53:23 PM 2 ❘ Chapter 1 FitnesstrackerPlus application while touching on several important topics such as scalable design, social networking, SEO, revenue generation, rich user interfaces, and others. In this chapter, you learn the following: ➤➤ How to take a great idea and build a design to make it into an even better web application. ➤➤ What pages you will need for the application. ➤➤ What the new Silverlight 4 features are that you’ll use to build your application. ➤➤ How you’ll actually build the application through membership, personalization, journaling, and social networking. You also learn how to monetize the site, as well as test and deploy it. ➤➤ What software you’ll need to download and install and what the basic setup instructions are for the site. It is an exciting time for Silverlight development, and with the plug-in installed on millions of com- puters and newly announced support in MySpace, now is a great time to learn how to incorporate the technology into your own solutions. problem After a few years of full-time software development, I realized that between sitting in a chair for hours on end, and the not-so-great foods that are consumed during those crunch-time marathon coding sessions, I would probably need to consider some healthy habits to offset what can otherwise be a very sedentary work environment and lifestyle. The first step for me was to join a local gym, and watch what I eat when not on a marathon coding session. After a few weeks of working out I started to wonder, “Am I getting the results I was looking for? Are these foods really good for me? In fact, how can I even measure if I am making any progress while spending all of these hours in the gym?” Perhaps most importantly, the programmer in me decided to ask, “How can I write some software to help me keep track of what I do here?” Most people would have just gone home, hopped on the Internet, and found some software or a web site that does this very thing. As programmers, however, we can just fire up the IDE and write our own instead. I decided that I wanted to create a new Silverlight-based web application that enables people to keep track of diet, exercise, and measurements. It would also be nice if the site could fos- ter a community of users who share success with others through public facing journals hosted on the site, and integration with social networking sites such as Facebook and MySpace. Because this seemed like it could become a pretty popular site that could generate enough revenue to sustain itself, I also wanted to integrate some revenue streams through some form of pay-per-click advertis- ing and monthly premium memberships for access to enhanced features of the site. At this point, the only thing missing was a name for the site and, of course, the implementation. For the name, I decided on FitnessTrackerPlus. I figured the “Plus” suffix will let users know that they are signing up for a more feature-rich site than traditional ASP.NET sites they may have used in the past. Or at least I will attempt to convince myself of that since I couldn’t think of a nice catchy Web 2.0 name like Google or Twitter that works well for a fitness-tracking web site. 534045c01.indd 2 3/13/10 4:53:23 PM Design ❘ 3 Design In this book, you will be creating a Silverlight 4 web application and will be utilizing the new fea- tures included in Silverlight 4 to provide the user with a rich user interface that is more responsive than a traditional ASP.NET web site. While the focus will be on creating an online fitness-tracking system, many of the features and techniques discussed can be applied to other line-of-business appli- cations as well. Like most line-of-business applications, the primary purpose of the site is to allow users to enter data and view reports on that data. You will also be adding a social networking aspect to the site as well. In the end, you should be able to leverage the data entry, charting, site structure, SEO, and social networking components when building your own line-of-business applications in Silverlight. Determining requirements for the site The first step in the design phase of FitnessTrackerPlus is to get a detailed list of requirements for the site. I briefly touched upon some of the most basic high-level features that will be required by the site, but now it’s time to flesh out everything. As with any application, it’s best to collect as much detail about requirements as possible before writing a single line of code. Changes will always be easier to make at this stage of the game, as opposed to once you have already started coding a solution. The following sections list the major features that FitnessTrackerPlus will include. Scalable The more popular a site becomes, the more important it is that the new traffic can be handled with- out causing problems for existing users. In order to facilitate this, a multi-tiered architecture should be used. This allows the site to grow as the site’s popularity grows, as well as keeps a clean separa- tion between user interface, business logic, and data access code. Rich User Interface Users will log on to the site to quickly enter daily exercise, foods, and measurements. A rich user interface that utilizes Silverlight helps to ensure that the amount of time performing data entry tasks is minimized. The less users have to spend entering data, the more time they will have to view reports, measure progress, and to interact with others. The key to making this process simple and painless is to ensure that the pages are not cluttered with large data entry forms. The data entry for foods, exercises, and measurements should be simple enough so that users can quickly figure out what they need to do in order to enter information. It is also common in web sites now to provide multiple themes for users so that they can select a favorite color scheme or site layout. Your site should provide a similar theme selection feature that also remembers theme preference so it can be restored on the next login. Dashboard Once users successfully log into the site, they should be presented with a dashboard that shows basic fitness information for the current day. The dashboard should provide a simple non-cluttered look and give the user quick access to current measurements, nutrition, and exercise summaries, and any site announcements that you want users to be aware of. Site announcements can be a good tool to 534045c01.indd 3 3/13/10 4:53:23 PM