Tải bản đầy đủ (.pdf) (468 trang)

Beginning Microsoft Visual Studio LightSwitch Development doc

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (42.38 MB, 468 trang )

www.it-ebooks.info
ffirs.indd iiffirs.indd ii 15/06/11 3:40 PM15/06/11 3:40 PM
www.it-ebooks.info
BEGINNING
VISUAL STUDIO® LIGHTSWITCH DEVELOPMENT
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxv
 PART I AN INTRODUCTION TO VISUAL STUDIO
LIGHTSWITCH
CHAPTER 1 Prototyping and Rapid Application Development . . . . . . . . . . . . . . . . . . . 3
CHAPTER 2 Getting Started with Visual Studio LightSwitch . . . . . . . . . . . . . . . . . . . . .19
CHAPTER 3 Technologies behind a LightSwitch Application . . . . . . . . . . . . . . . . . . . 45
CHAPTER 4 Customizing LightSwitch Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
PART II CREATING APPLICATIONS WITH VISUAL
STUDIO LIGHTSWITCH
CHAPTER 5 Preparing to Develop a LightSwitch Application . . . . . . . . . . . . . . . . . . 105
CHAPTER 6 Working with Simple Data Screens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .127
CHAPTER 7 Working with Master-Detail Data Screens . . . . . . . . . . . . . . . . . . . . . . . . 157
CHAPTER 8 Using Existing SQL Server Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
CHAPTER 9 Building and Customizing Screens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
CHAPTER 10 Validation and Business Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
CHAPTER 11 Authentication and Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . .309
CHAPTER 12 Microsoft O ce Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
 PART III ADVANCED LIGHTSWITCH APPLICATION
DEVELOPMENT
CHAPTER 13 Deploying LightSwitch Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . .349
CHAPTER 14 Using SharePoint 2010 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
 PART IV APPENDIX
APPENDIX Answers to Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .417
ffirs.indd iffirs.indd i 15/06/11 3:40 PM15/06/11 3:40 PM
www.it-ebooks.info


ffirs.indd iiffirs.indd ii 15/06/11 3:40 PM15/06/11 3:40 PM
www.it-ebooks.info
BEGINNING
Visual Studio
®
LightSwitch
Development
ffirs.indd iiiffirs.indd iii 15/06/11 3:40 PM15/06/11 3:40 PM
www.it-ebooks.info
ffirs.indd ivffirs.indd iv 15/06/11 3:40 PM15/06/11 3:40 PM
www.it-ebooks.info
BEGINNING
Visual Studio
®
LightSwitch
Development
István Novák
ffirs.indd vffirs.indd v 15/06/11 3:40 PM15/06/11 3:40 PM
www.it-ebooks.info
Beginning Visual Studio® LightSwitch Development
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2011 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-1-118-02195-8
ISBN: 978-1-118-14851-8 (ebk.)
ISBN: 978-1-118-14850-1 (ebk.)

ISBN: 978-1-118-14854-9 (ebk.)
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 />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 specifi cally disclaim all warranties, including
without limitation warranties of fi tness for a particular purpose. No warranty may be created or extended by sales or
promotional 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 publisher 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: 2011930875
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 affi liates, in the United States and other countries,
and may not be used without written permission. Visual Studio is a registered trademark of Microsoft Corporation. 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.

ffirs.indd viffirs.indd vi 15/06/11 3:40 PM15/06/11 3:40 PM
www.it-ebooks.info
To Henriett, Eszter, and Reka. I owe you many
hours for the missed weekends.
ffirs.indd viiffirs.indd vii 15/06/11 3:40 PM15/06/11 3:40 PM
www.it-ebooks.info
ffirs.indd viiiffirs.indd viii 15/06/11 3:40 PM15/06/11 3:40 PM
www.it-ebooks.info
ABOUT THE AUTHOR
ISTVÁN NOVÁK is an associate and the chief technology consultant of SoftwArt,
a small Hungarian IT consulting company. He works as a software architect
and community evangelist. In the last 20 years, he participated in more than
50 enterprise software development projects. In 2002, he co-authored the fi rst
Hungarian book about .NET development. In 2007, he was awarded with the
Microsoft Most Valuable Professional (MVP) title, and in 2011 he became a
Microsoft Regional Director. As the main author, he contributed in writing the
Visual Studio 2010 and .NET 4 Six-In-One book (Indianapolis: Wiley, 2010). He holds master’s
degree from the Technical University of Budapest, Hungary, and also has a doctoral degree in
software technology. He lives in Dunakeszi, Hungary, with his wife and two teenage daughters. He
is a passionate scuba diver. You may have a good chance of meeting him underwater at the Red Sea
in any season of the year.
ffirs.indd ixffirs.indd ix 15/06/11 3:40 PM15/06/11 3:40 PM
www.it-ebooks.info
ffirs.indd xffirs.indd x 15/06/11 3:40 PM15/06/11 3:40 PM
www.it-ebooks.info
ABOUT THE TECHNICAL EDITORS
RITA RUBAN is a writer of educational material in computing (specifi cally, Visual Basic, Visual Basic
for Applications, HTML, Excel, and various art software applications), mathematics, and biology,
having been a teacher/lecturer for these subjects. She is also an artist and designer. Visit her page on
LinkedIn for further information and web links, or e-mail her at


DIANNE SIEBOLD is a software developer and writer specializing in Visual Basic, C#, .NET
Framework, Windows Communications Foundation, ActiveX Data Objects, and SQL Server. She
has worked for a number of Microsoft partners writing enterprise applications, with an emphasis on
services and data access. Reach her by e-mail at

ffirs.indd xiffirs.indd xi 15/06/11 3:40 PM15/06/11 3:40 PM
www.it-ebooks.info
ffirs.indd xiiffirs.indd xii 15/06/11 3:40 PM15/06/11 3:40 PM
www.it-ebooks.info
CREDITS
ACQUISITIONS EDITOR
Paul Reese
PROJECT EDITOR
Kevin Shafer
TECHNICAL EDITORS
Rita Ruban
Dianne Seybold
PRODUCTION EDITORS
Debra Banninger
Nick Moran
COPY EDITOR
Luann Rou
EDITORIAL DIRECTOR
Robyn B. Siesky
EDITORIAL MANAGER
Mary Beth Wakefi eld
FREELANCER EDITORIAL MANAGER
Rosemarie Graham
ASSOCIATE DIRECTOR OF MARKETING

David Mayhew
PRODUCTION MANAGER
Tim Tate
VICE PRESIDENT AND EXECUTIVE
GROUP PUBLISHER
Richard Swadley
VICE PRESIDENT AND EXECUTIVE
PUBLISHER
Barry Pruett
ASSOCIATE PUBLISHER
Jim Minatel
PROJECT COORDINATOR, COVER
Katie Crocker
PROOFREADER
Nancy Carrasco
INDEXER
Robert Swanson
COVER DESIGNER
LeAndra Young
COVER IMAGE
© iStock / Pamela Moore
ffirs.indd xiiiffirs.indd xiii 15/06/11 3:40 PM15/06/11 3:40 PM
www.it-ebooks.info
ffirs.indd xivffirs.indd xiv 15/06/11 3:40 PM15/06/11 3:40 PM
www.it-ebooks.info
ACKNOWLEDGMENTS
I REALLY ENJOYED WRITING THIS BOOK. The idea started at the beginning of August, 2010 when
Microsoft announced Visual Studio LightSwitch. A few weeks later, after trying the fi rst Beta,
I imagined an idea about how this great product could be evangelized. I’m happy that Acquisitions
Editor Paul Reese supported my idea and helped to form this book.

In addition to my readers, I owe a lot to other great people who helped me with the writing of this
book and ensured that my imagination became reality.
First of all, I’d like to thank Kevin Shafer for his amazing editorial work. This is my second book
working together with Kevin, and I really appreciated how he carried the whole book writing
process on his shoulders, and ironed my sentences. I hope I can meet Kevin personally in the near
future, and shake his hand.
I’d also like to thank Luann Rouff for thoroughly reading the manuscript and translating my
originally complex paragraphs to simple and tangle-free sentences. Dianne Siebold and Rita Ruban,
the Technical Editors, raked over my submitted chapters and suggested many changes that made this
writing much more accurate than it was before their reviews. I’m very grateful for their help.
The Hungarian Silverlight User Group and the Hungarian Windows Azure User Group provided me
with several opportunities to speak about LightSwitch. They also gave me valuable feedback about
positioning the product. Thanks for their support.
Finally, I owe many kisses to my wife and daughters for letting me spend so many hours away from
them — in my study, working with the book. Now, they get their evenings and weekends back.
ffirs.indd xvffirs.indd xv 15/06/11 3:40 PM15/06/11 3:40 PM
www.it-ebooks.info
ffirs.indd xviffirs.indd xvi 15/06/11 3:40 PM15/06/11 3:40 PM
www.it-ebooks.info
CONTENTS
INTRODUCTION xxv
PART I: AN INTRODUCTION TO VISUAL STUDIO LIGHTSWITCH
CHAPTER 1: PROTOTYPING AND RAPID APPLICATION DEVELOPMENT 3
Line-of-Business Software Development Challenges 4
LOB Software Development 5
Changing Project Environment 5
Creating a Requirements Specifi cation 5
Feedback Frequency 7
Application Prototyping 8
Wireframe Models 8

Proof-of-Concept Models 10
Low-Fidelity Prototypes 10
High-Fidelity Prototypes 11
Rapid Application Development 11
RAD Tools 12
Visual Studio LightSwitch and RAD 15
Summary 15
CHAPTER 2: GETTING STARTED WITH VISUAL STUDIO LIGHTSWITCH 19
Getting to Know Visual Studio 19
A Short History of Visual Studio 20
Roots 20
Other Visual Languages and Tools 21
Integrating Languages into Visual Studio 21
Moving to .NET 22
Visual Studio Editions 23
Visual Studio 2010 23
Getting to Know Visual Studio LightSwitch 23
Business Code versus Plumbing Code 24
LOB Applications and LightSwitch 24
Installing Visual Studio LightSwitch 26
Types of Installation 26
Running LightSwitch for the FirstTime 29
ftoc.indd xviiftoc.indd xvii 14/06/11 1:56 PM14/06/11 1:56 PM
www.it-ebooks.info
xviii
CONTENTS
Creating Your First LightSwitch Application 30
Creating a Project 31
Creating a Table 33
Creating a Screen to List Album Information 35

Making Runtime Customizations 40
Summary 42
CHAPTER 3: TECHNOLOGIES BEHIND A LIGHTSWITCH APPLICATION 45
The Three-Tier Application Architecture Pattern 45
The Presentation Tier 47
The Logic Tier 47
The Data Tier 48
LightSwitch and the Three-Tier Architecture Pattern 48
The .NET Framework 51
The .NET Runtime 51
.NET Languages 52
.NET Framework and LightSwitch 53
Sources for More Information about the .NET Framework 54
Silverlight 4 54
A New UI Concept 55
Layout 55
Data Binding 56
Styles and Templates 57
Sources for More Information about Silverlight 4 59
SQL Server 2008 59
SQL Server Features 59
SQL Server 2008 Express 61
Sources for More Information about SQL Server 64
SharePoint 2010 64
SharePoint 2010 Features 64
SharePoint 2010 Lists and LightSwitch 65
Microsoft O ce 66
Microsoft O ce Application Features 67
Exporting Information to Microsoft Excel 68
Windows Azure 69

Dynamic Resource Allocation 70
Application Development with Windows Azure 71
Windows Azure and LightSwitch 71
Sources for More Information about Windows Azure 72
Summary 72
ftoc.indd xviiiftoc.indd xviii 14/06/11 1:56 PM14/06/11 1:56 PM
www.it-ebooks.info
xix
CONTENTS
CHAPTER 4: CUSTOMIZING LIGHTSWITCH APPLICATIONS 77
Customizing Data 78
Creating the Aquarium Database 78
Connecting to an Existing Database 80
Changing Names and Types in the Existing Table 83
Creating the Cart Table 84
Customizing Screens 87
Creating a Creature Catalog 87
Changing the Grid Layout 88
Creating a Shopping Cart Screen 91
Writing Code 93
Setting a Default Property Value in Code 94
Setting the Startup Screen 95
Managing Price Information 96
Other Points Where Code Is Used 99
Summary 99
PART II: CREATING APPLICATIONS WITH VISUAL STUDIO LIGHTSWITCH
CHAPTER 5: PREPARING TO DEVELOP A LIGHTSWITCH
APPLICATION 105
The ProjectTrek Sample 105
The Functionality of ProjectTrek 106

The ProjectTrek Application Database 108
LightSwitch Application Development Life cycle 115
The Iterative Model 115
Construction: Design and Implementation 116
Testing 117
Deployment 118
Extending a LightSwitch Application 119
Team Foundation Server Support 120
A Short Tour of the LightSwitch IDE 120
The Workbench and the Start Page 120
Window Management 124
Summary 125
CHAPTER 6: WORKING WITH SIMPLE DATA SCREENS 127
Creating New Tables 127
Creating the ProjectTrek Project 128
Designing the TimeCategory Table 130
Table Fields and Properties 132
ftoc.indd xixftoc.indd xix 14/06/11 1:56 PM14/06/11 1:56 PM
www.it-ebooks.info
xx
CONTENTS
Creating Screens 141
The Add New Screen Dialog 142
Predefi ned Screen Types 142
The Layout Structure of a Screen 145
Inserting, Editing, and Deleting Data 146
Creating a Details Screen 147
Understanding How the Detail Screen Works 148
Creating a New Data Screen 149
Creating an Editable Grid Screen 150

Deleting a Record 152
Polishing 153
Finalizing the EditableTimeCategeory Screen 153
Implementing the ProjectRoleType Entity 154
Summary 155
CHAPTER 7: WORKING WITH MASTER-DETAIL DATA SCREENS 157
Creating Detail Tables 158
Relationships 158
The Add New Relationship Dialog 161
Creating Master-Detail Screens 170
Understanding Master-Detail Screens 170
Using the Add New Screen Dialog to Create Master-Detail Screens 171
The Structure of Master-Detail Screens 173
Queries 175
Understanding Queries 175
Editing Queries 176
Creating New Queries 178
Query Parameters 179
Binding Query Parameters with Screen Data 180
Adding Details to Screens 182
Adding Project Details to ListProjects 183
Adding ProjectTask and AdditionalCost Details 184
Aggregations 186
Polishing 189
Summary 191
CHAPTER 8: USING EXISTING SQL SERVER DATA 195
Connecting to SQL Server Databases 196
Using Server Explorer 197
Establishing a Connection to a SQL Server Database 203
ftoc.indd xxftoc.indd xx 14/06/11 1:56 PM14/06/11 1:56 PM

www.it-ebooks.info
xxi
CONTENTS
Customizing Data in Existing Databases 210
Renaming Tables and Fields 211
Mapping and Changing Field Types 213
Using Computed Properties to Extend Attached Tables 216
Server-Side Computed Fields 218
Managing Data Relationships 218
Using Existing Relationships 218
Creating New Relationships between a LightSwitch Table and
an External Table 219
Summary 223
CHAPTER 9: BUILDING AND CUSTOMIZING SCREENS 227
Understanding the Screen Layout Structure 228
Data Items 229
Layout Items 230
Data Binding 232
Using Layout Containers 232
Understanding Layout Containers 233
Nesting Layout Containers 235
Layout Container Sizing 238
Runtime Screen Customization 239
Using Customization Mode 240
Features and Restrictions in Customization Mode 243
The Data Grid and the List Controls 243
Working with the Data Grid Control 244
Data Grid Properties 247
Working with the List Control 247
Using Simple Controls and Value Pickers 249

Working with Simple Controls 251
Control Sizing 253
Picking Up Values 253
Methods, Buttons, and Command Bars 257
Adding Methods and Buttons to a Screen 257
Enabling and Disabling Methods 260
Adding Buttons to the Screen’s Command Bar 262
Adding Methods and Buttons to Controls 264
Removing Buttons and Methods 266
Tasks and Navigation 267
Understanding the Screen Navigation Structure 267
Changing the Navigation Structure of ProjectTrek 269
ftoc.indd xxiftoc.indd xxi 14/06/11 1:56 PM14/06/11 1:56 PM
www.it-ebooks.info
xxii
CONTENTS
Extending the UI with Shells, Themes, and Custom Controls 270
Applying Shells and Themes 271
Custom Controls 272
Summary 274
CHAPTER 10: VALIDATION AND BUSINESS RULES 279
Validation and Business Logic Architecture of LightSwitch 280
Validation Steps 280
Defi ning Business Rules 282
The Save Pipeline 283
Adding Validation Rules 285
Declaring Simple Field Validation Rules 285
Declaring Custom Field Validation Rules 287
Validation through Navigation Properties 289
Validating Entities 292

Validation Messages in the UI 296
Implementing Business Rules 298
Intercepting the Save Pipeline 298
Save Pipeline Event-Handling Methods 300
Query Event-Handling Methods 302
Auditing Data Modifi cations 303
Summary 307
CHAPTER 11: AUTHENTICATION AND ACCESS CONTROL 309
Authentication and Access Control Concepts 309
Authentication 311
Access Control 312
Permissions 312
Roles and Users 313
Access Control Methods 313
Using Access Control in LightSwitch Applications 314
Defi ning and Checking Permissions 314
Available Access Control Methods 316
Managing Users, Roles, and Permissions 320
Authentication in LightSwitch 320
Using Windows Authentication 321
Using Forms Authentication 323
Managing Roles and Permissions 323
Managing Users 325
Summary 325
ftoc.indd xxiiftoc.indd xxii 14/06/11 1:56 PM14/06/11 1:56 PM
www.it-ebooks.info
xxiii
CONTENTS
CHAPTER 12: MICROSOFT OFFICE INTEGRATION 329
Exporting Data to Microsoft Excel 329

Understanding and Using O ce Automation 331
Using the Microsoft O ce Automation Model from
LightSwitch Applications 332
Exporting to Excel Using the Automation Model 332
Exploring the Automation Model 340
Exporting a Project List to Word 342
Summary 344
PART III: ADVANCED LIGHTSWITCH APPLICATION DEVELOPMENT
CHAPTER 13: DEPLOYING LIGHTSWITCH APPLICATIONS 349
Deployment Options 349
Thin Client and Thick Client 350
Client and Application Server Topologies 351
Restrictions 352
Thick Client Deployment 353
Creating a Desktop Client Installation Kit 353
Using the Installation Kit 358
Removing the Desktop Client 359
Managing the Database Deployment 359
Deploying with IIS 362
Three-Tier Deployment 362
LightSwitch and Three-Tier Deployment 363
Publishing the Application Server to IIS 364
Windows Azure Deployment 365
Prerequisites for Windows Azure Deployment 366
Using the Publish Application Wizard for Windows Azure Deployment 367
Starting the Application in Windows Azure 372
Summary 374
CHAPTER 14: USING SHAREPOINT 2010 LISTS 377
Creating a Sample SharePoint Site 378
Using Your Company’s SharePoint Server 378

Installing SharePoint Foundation 2010 378
Preparing a Custom SharePoint List 380
Using SharePoint 2010 Lists 384
Attaching to SharePoint Lists 384
Table Fields Generated for SharePoint Lists 388
ftoc.indd xxiiiftoc.indd xxiii 14/06/11 1:56 PM14/06/11 1:56 PM
www.it-ebooks.info

×