Visual studio lightswitch 2012

659 46 0
Visual studio lightswitch 2012

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

www.it-ebooks.info For your convenience Apress has placed some of the front matter material after the index Please use the Bookmarks and Contents at a Glance links to access them www.it-ebooks.info Contents at a Glance About the Author��������������������������������������������������������������������������������������������������������������� xxi About the Technical Reviewers��������������������������������������������������������������������������������������� xxiii Acknowledgments������������������������������������������������������������������������������������������������������������ xxv Foreword������������������������������������������������������������������������������������������������������������������������ xxvii Introduction��������������������������������������������������������������������������������������������������������������������� xxix ■■Chapter 1: Introducing LightSwitch�����������������������������������������������������������������������������������1 ■■Chapter 2: Setting Up Your Data��������������������������������������������������������������������������������������11 ■■Chapter 3: Introducing Silverlight Screen Design�����������������������������������������������������������37 ■■Chapter 4: Accessing Data with Code�����������������������������������������������������������������������������63 ■■Chapter 5: Validating Data�����������������������������������������������������������������������������������������������89 ■■Chapter 6: Querying Your Data��������������������������������������������������������������������������������������113 ■■Chapter 7: Mastering Silverlight Screen Design������������������������������������������������������������139 ■■Chapter 8: Creating HTML Interfaces�����������������������������������������������������������������������������189 ■■Chapter 9: Creating and Using RIA Services������������������������������������������������������������������247 ■■Chapter 10: Sharing Data with OData����������������������������������������������������������������������������265 ■■Chapter 11: Creating and Using Custom Controls���������������������������������������������������������281 ■■Chapter 12: Creating Control Extensions�����������������������������������������������������������������������309 ■■Chapter 13: Creating Data and Presentation Extensions�����������������������������������������������365 ■■Chapter 14: Creating Reports����������������������������������������������������������������������������������������435 ■■Chapter 15: Sending E-mail�������������������������������������������������������������������������������������������481 iii www.it-ebooks.info ■ Contents at a Glance ■■Chapter 16: Authenticating Your Users�������������������������������������������������������������������������523 ■■Chapter 17: Authorizing Your Users������������������������������������������������������������������������������537 ■■Chapter 18: Deploying Your Application������������������������������������������������������������������������557 ■■Appendix A: Culture Names�������������������������������������������������������������������������������������������597 ■■Appendix B: Data Type Identifiers���������������������������������������������������������������������������������603 ■■Appendix C: Using Properties in Custom Controls���������������������������������������������������������605 ■■Appendix D: Custom Screen Template View IDs������������������������������������������������������������609 ■■Appendix E: HelpDesk Tables�����������������������������������������������������������������������������������������613 Index���������������������������������������������������������������������������������������������������������������������������������627 iv www.it-ebooks.info Introduction It’s possible to see many things in life as a journey, and writing a business application is no exception On this particular journey, your goal is to build an application that fully works and meets the expectations of your users Let’s imagine, for a moment, that the tool that builds your application represents your journey’s mode of transport Using this analogy, I like to think of LightSwitch as a high-speed train because it allows you to reach your destination a lot faster than usual Speaking of trains, journeys are much more comfortable today than they were in the past Modern carriages are Wi-Fi enabled and include tables and electrical sockets that you can plug your laptop into There are even touch-screen displays that you can use to watch television or to view a map that shows you exactly where you are, and how much further there is to go The ride itself is smooth enough for you to leave your hot coffee on the table without fear of it spilling Everything is much cleaner than in the age of steam; and you never risk getting a hot cinder in your eye if you open a window! The fascinating thing is that your railway journey might follow exactly the same route as it would have 150 years ago However, the experience itself is quicker, cleaner, and more convenient Just like the railways, LightSwitch has evolved during its short lifespan The improvements in the latest version help to keep it fresh, relevant, and purposeful When LightSwitch first came out, it created applications based on Microsoft Silverlight Today, you can support mobile and touch-screen tablet devices by extending your application to include an HTML front end A big benefit of this approach is that you can reuse all of your existing data and business logic This technical change is the railway equivalent of swapping all the carriages on a train for modern replacements Likewise, the first version of LightSwitch employed RIA services as the communication channel between the client and server Today’s LightSwitch uses the OData protocol instead Applying the railway analogy once more, this is like replacing the railway tracks with smoother rails If LightSwitch doesn’t totally meet your requirements, you can easily integrate your application with other systems and platforms—like completing the final leg of the journey by car, bus, or foot The point of this analogy is to highlight one of the beauties of LightSwitch It’s possible for LightSwitch to evolve and improve because it’s built in a modular way that allows individual pieces to be replaced This modular architecture ensures that LightSwitch will remain relevant for a long time to come You can also consider this book as a journey—a tour that shows you how to build a LightSwitch application from start to finish As with most journeys, there’ll be highlights and features along the way I’m really pleased that you’ve chosen to take the LightSwitch journey It’s sure to save you time and make your life easier; so I’m certain it’ll be a journey that you won’t regret taking! Who This Book Is For This book is designed for readers who want to build data-driven business applications quickly You don’t need to have any prior knowledge of LightSwitch, but it helps if you know a little about NET and database development in general xxix www.it-ebooks.info ■ Introduction How This Book Is Organized If learning LightSwitch seems like an enormous task, don’t worry! You might have heard of the Pareto Principle, which also goes by the name of the 80/20 rule This rule states that in most situations, 20% of something causes 80% of the results If you apply the Pareto Principle to this book, it suggests that you can accomplish 80% of what you want to accomplish by reading less than four chapters! And, in fact, the first three chapters highlight the key topics that are all you need to build an application that’s almost 80% complete By the end of Chapter 3, you’ll understand how to create data tables and attach to external data You’ll know how to build a Silverlight web or desktop application that includes screens and navigation controls You’ll see how to open child records from a parent record and how to assign parent records to child records by using controls like the autocomplete box (a picker control that’s similar to a drop-down box) In short, you’ll know how to build a working business application with enough functionality to perform most basic tasks The next part of the book shows you how to write code and queries After that, you’ll learn how to create screens that contain advanced features and support mobile devices by adding an HTML client Unless you want to create an application that’s completely isolated from other systems, it’s useful to know how you can attach to unusual data sources and how to share your LightSwitch data with other applications You’ll discover how to this through RIA services and OData You can extend your Silverlight applications through custom controls and extensions, and you’ll find an entire section of the book devoted to this topic The remaining chapters of the book show you how to build reports, integrate with e-mail systems, and deploy your application The Sample Application Most of the chapters include code samples that refer to a HelpDesk application The purpose of this application is to relate the technical content in the book to a real-life scenario that you can understand The HelpDesk application is typical of the applications that you can build with LightSwitch Its features are simple enough to understand, yet complex enough to show off the more advanced features of LightSwitch It is designed to help companies manage problems, and it’s especially suitable for departments that deal with technical support issues It allows users to record the actions they carry out to resolve a problem, while giving managers an overview of all current issues Figure shows a screen from this application and illustrates some of the features that you’ll learn about in this book xxx www.it-ebooks.info ■ Introduction Figure 1.  A typical screen from the HelpDesk manager Figure shows the data-entry screen through which users can enter help desk issues They can set the user that the issue relates to and allocate an engineer to the record The data-entry screen allows users to respond to the issue, attach documents, register feedback, and record the time that engineers spend on the task To help you re-create this application, you can find a summary of the application’s tables in Appendix E Code Samples LightSwitch supports C#, VB.NET, and JavaScript This book includes code samples in all three languages To make life simple, LightSwitch hides much of the complexity that’s associated with application design When you want to write some code, you this by clicking on a write code button that LightSwitch shows in its graphical designers The write code button shows a list of events that you can handle When you select one of the options in the list, LightSwitch opens a code editor window that you can use to author your code When you’re starting out with LightSwitch, it isn’t always obvious where your code is stored To add some clarity, each code listing includes a file location, as shown in Listing xxxi www.it-ebooks.info ■ Introduction Listing 1.  Hello World Example VB: File: HelpDeskVB\Server\UserCode\ApplicationData.vb Imports System.Text.RegularExpressions   'REM VB Code appears here Dim message = "Hello World" C#: File: HelpDeskCS\Server\UserCode\ApplicationData.cs using System.Text.RegularExpressions;   //REM C# Code appears here var message = "Hello World"; For both the VB and C# examples in Listing 1, the File heading specifies the file name and path In this example, HelpDeskVB and HelpDeskCS refer to the name of your LightSwitch application The name Server refers to the Server project This piece of information is useful because it informs you that the code runs on the server rather than the client (You’ll learn all about this in Chapter 1.) The next part of the file heading, UserCode, indicates a subfolder in the Server project Finally, ApplicationData.vb and ApplicationData.cs refer to the name of the VB or C# file that the listing shows If a piece of code requires you to reference a NET namespace, the code listing will show the necessary Imports (VB.NET) or using (C#) statement Be sure to add these statements to the top of your code file Many of the code samples include numeric symbols to help you locate specific lines in a code listing For example, line  defines a comment, whereas line  declares a variable There’s obviously no need to enter these numeric symbols when you re-create this code for real! As in all books, the length of a code line can exceed the width of this book In most cases, I’ve put line breaks in places that still allow the code to compile But in some circumstances, this isn’t always possible Notable examples include namespace declarations in XAML files and VB.NET keywords like inherits and implements If your application doesn’t compile, it’s worth checking your code for extraneous line breaks Tips/Notes/Cautions This book includes callouts with tips, notes, and cautions Tips are helpful hints or pieces of information that may be particularly interesting Notes provide you with nonessential, yet interesting additional information about a particular topic Cautions alert you to anything that might be detrimental to your work or could cause damage Exercises Some readers prefer to learn kinesthetically (that is, in a touchy-feely way) If you fall into this category, you can try the exercises that you’ll find throughout this book These exercises provide you with ideas to try out in LightSwitch to help you further understand the content that’s described in the book xxxii www.it-ebooks.info ■ Introduction Comments and Errata Although my editors and I have tried to be as accurate as possible, mistakes sometimes happen (particularly with a book of this length) If you have any feedback or want to report any bugs, please visit the official page for this book at the Apress website: http://www.apress.com/9781430250715 This page also shows any mistakes that we’ve found following the publication of this book xxxiii www.it-ebooks.info Chapter Introducing LightSwitch Do you ever feel that software development is too complicated? If you’re building business applications already, you feel frustrated by the amount of time that it takes to build an application? Or if you’ve never built a modernday web application, does the thought of doing so frighten you? If you’re starting from scratch, where exactly you begin? And are you really confident that you can build a robust application that meets the expectations of your users and isn’t susceptible to “falling over” or getting hacked? If any of this sounds familiar, LightSwitch will come as a breath of fresh air It’s a rapid software development tool that helps you to work faster, better, and smarter It allows you to build data-centric business applications that can run on the desktop or on the web But despite LightSwitch’s simplicity, there’s no need to compromise on functionality You can build applications with all the features that you’d expect to find in a typical business application, and this book will show you exactly how The first chapter teaches you the core LightSwitch principles that will help you throughout the rest of this book The key topics that you’ll cover in this chapter include • The three-tier architecture that LightSwitch uses • How LightSwitch uses a model-centric architecture, and applies the MVVM pattern • What exactly happens when you build and compile a LightSwitch application Let’s begin by taking a look at LightSwitch’s architecture Understanding LightSwitch’s Architecture In keeping with best practices, LightSwitch applications consist of three layers This architecture enforces the logical separation of your application and provides better maintainability and scalability The biggest advantage is that it allows Microsoft to rewrite or replace entire layers in response to changes in requirements or technology Every time you create a new application, LightSwitch creates multiple projects in your LightSwitch solution Each project is designed to run on a specific layer in this architecture This automation of the project layout itself is a major timesaver If you built an application without LightSwitch, you could spend days on getting the architecture correct Figure 1-1 illustrates the parts that make up a LightSwitch application It highlights the way that applications are separated into data, logic, and presentation layers It also illustrates how the layers communicate with each other www.it-ebooks.info ■ Contents Modifying Screen Queries���������������������������������������������������������������������������������������������������������122 Writing Queries in Code�������������������������������������������������������������������������������������������������������������123 Understanding LINQ Syntax������������������������������������������������������������������������������������������������������������������������������� 124 Understanding Method (Lambda) Syntax Code������������������������������������������������������������������������������������������������� 124 Using Standard Query Operators����������������������������������������������������������������������������������������������������������������������� 125 Filtering Data with Lambda Expressions����������������������������������������������������������������������������������������������������������� 125 Using the Any and All Operators������������������������������������������������������������������������������������������������������������������������ 126 Performing For Each Logic�������������������������������������������������������������������������������������������������������������������������������� 127 Querying Data Remotely������������������������������������������������������������������������������������������������������������������������������������ 128 Querying Data Locally���������������������������������������������������������������������������������������������������������������������������������������� 129 Performing Advanced Filtering��������������������������������������������������������������������������������������������������131 Filtering by Related Child Items������������������������������������������������������������������������������������������������������������������������� 131 Exists, In������������������������������������������������������������������������������������������������������������������������������������������������������������ 132 Not Exists, Not In����������������������������������������������������������������������������������������������������������������������������������������������� 133 Filtering by Date Elements��������������������������������������������������������������������������������������������������������������������������������� 134 Top N Records���������������������������������������������������������������������������������������������������������������������������������������������������� 135 Creating Global Values���������������������������������������������������������������������������������������������������������������135 Adding Your Global Value to Your LSML File������������������������������������������������������������������������������������������������������ 136 Creating the Global Value Class������������������������������������������������������������������������������������������������������������������������� 137 Using Global Values in Upgraded Projects��������������������������������������������������������������������������������������������������������� 138 Summary�����������������������������������������������������������������������������������������������������������������������������������138 ■■Chapter 7: Mastering Silverlight Screen Design������������������������������������������������������������139 Working with Screen Data���������������������������������������������������������������������������������������������������������139 Displaying Custom Text and Information����������������������������������������������������������������������������������������������������������� 140 Adding Data Controls to a Screen���������������������������������������������������������������������������������������������������������������������� 141 Setting Control Values and Default Values��������������������������������������������������������������������������������������������������������� 142 Setting the Screen Title in Code������������������������������������������������������������������������������������������������������������������������ 143 Creating an Advanced Search Screen��������������������������������������������������������������������������������������������������������������� 144 Managing Screens��������������������������������������������������������������������������������������������������������������������146 Opening Screens from Buttons or Links������������������������������������������������������������������������������������������������������������ 146 Refreshing All Open Screens����������������������������������������������������������������������������������������������������������������������������� 148 x www.it-ebooks.info ■ Contents Passing Arguments into Screens����������������������������������������������������������������������������������������������������������������������� 149 Creating a Continuous New Data Screen����������������������������������������������������������������������������������������������������������� 150 Working with Controls���������������������������������������������������������������������������������������������������������������151 Finding Controls Using FindControl������������������������������������������������������������������������������������������������������������������� 151 Reference the Underlying Silverlight Control����������������������������������������������������������������������������������������������������� 155 Handling Silverlight Control Events������������������������������������������������������������������������������������������������������������������� 156 Custom Examples����������������������������������������������������������������������������������������������������������������������159 Designing an Add/Edit Screen��������������������������������������������������������������������������������������������������������������������������� 159 Customizing Data Grid Dialogs�������������������������������������������������������������������������������������������������������������������������� 161 Nesting Autocomplete Boxes����������������������������������������������������������������������������������������������������������������������������� 164 Bulk Updating Records by Using a Multiselect Grid������������������������������������������������������������������������������������������ 165 Assigning and Unassigning Self-Joined Data���������������������������������������������������������������������������������������������������� 168 Creating Screens to Work with Single Row Tables�������������������������������������������������������������������������������������������� 170 Working with Threads���������������������������������������������������������������������������������������������������������������171 Finding Out Which Thread Your Code Is Executing On��������������������������������������������������������������������������������������� 173 Understanding When to Execute Code on a Different Thread���������������������������������������������������������������������������� 173 Reacting to Data Changes���������������������������������������������������������������������������������������������������������175 Using PropertyChanged on a New Data Screen Template��������������������������������������������������������������������������������� 176 Using PropertyChanged on a Details Screen����������������������������������������������������������������������������������������������������� 178 Working with Files���������������������������������������������������������������������������������������������������������������������180 Uploading Files�������������������������������������������������������������������������������������������������������������������������������������������������� 180 Downloading and Saving Files��������������������������������������������������������������������������������������������������������������������������� 183 Opening Files in Their Application��������������������������������������������������������������������������������������������������������������������� 184 Summary�����������������������������������������������������������������������������������������������������������������������������������186 ■■Chapter 8: Creating HTML Interfaces�����������������������������������������������������������������������������189 Introducing the HTML Client������������������������������������������������������������������������������������������������������189 Setting Up Visual Studio for HTML Development�����������������������������������������������������������������������190 Adding an HTML Client��������������������������������������������������������������������������������������������������������������190 Adding Screens to Your Application�������������������������������������������������������������������������������������������191 xi www.it-ebooks.info ■ Contents Designing Screens��������������������������������������������������������������������������������������������������������������������192 Understanding the Top-Level Screen Layout����������������������������������������������������������������������������������������������������� 193 Using Data List Controls������������������������������������������������������������������������������������������������������������������������������������ 193 Using Data Controls������������������������������������������������������������������������������������������������������������������������������������������� 194 Using Data-Picker Controls�������������������������������������������������������������������������������������������������������������������������������� 195 Setting Screen Properties���������������������������������������������������������������������������������������������������������������������������������� 195 Creating a Navigation Structure������������������������������������������������������������������������������������������������196 Creating a Startup Screen��������������������������������������������������������������������������������������������������������������������������������� 196 Navigating to Different Screens������������������������������������������������������������������������������������������������������������������������� 197 Adding New Records�����������������������������������������������������������������������������������������������������������������198 Using Dialogs����������������������������������������������������������������������������������������������������������������������������199 Creating Child Records in a Dialog�������������������������������������������������������������������������������������������������������������������� 199 Editing Child Records in a Dialog����������������������������������������������������������������������������������������������������������������������� 200 Using Popups����������������������������������������������������������������������������������������������������������������������������201 Using Compact Margins������������������������������������������������������������������������������������������������������������������������������������� 203 Creating a Search Screen����������������������������������������������������������������������������������������������������������204 Filtering the List Control������������������������������������������������������������������������������������������������������������204 Extending Your Application with JavaScript������������������������������������������������������������������������������207 Using the LightSwitch JavaScript API���������������������������������������������������������������������������������������������������������������� 207 Attaching JavaScript Code�������������������������������������������������������������������������������������������������������������������������������� 208 Setting Default Screen Values��������������������������������������������������������������������������������������������������������������������������� 209 Setting the Screen Title������������������������������������������������������������������������������������������������������������������������������������� 209 Hiding Controls�������������������������������������������������������������������������������������������������������������������������������������������������� 210 Accessing Data in Code������������������������������������������������������������������������������������������������������������������������������������� 211 Deleting Records����������������������������������������������������������������������������������������������������������������������������������������������� 211 Setting a Custom Command Icon���������������������������������������������������������������������������������������������������������������������� 214 Accessing Screen Data Collections������������������������������������������������������������������������������������������������������������������� 214 Validating Data�������������������������������������������������������������������������������������������������������������������������������������������������� 216 Adding Custom HTML to Screens����������������������������������������������������������������������������������������������217 xii www.it-ebooks.info ■ Contents Using Custom Controls��������������������������������������������������������������������������������������������������������������218 Showing Data with Custom Controls����������������������������������������������������������������������������������������������������������������� 219 Running Code When Data Changes������������������������������������������������������������������������������������������������������������������� 223 Replacing Default Controls with Custom Controls��������������������������������������������������������������������������������������������� 224 Executing Queries���������������������������������������������������������������������������������������������������������������������226 Working with Documents����������������������������������������������������������������������������������������������������������229 Uploading Files�������������������������������������������������������������������������������������������������������������������������������������������������� 229 Downloading Files��������������������������������������������������������������������������������������������������������������������������������������������� 237 Customizing the Appearance of Your Application����������������������������������������������������������������������242 Applying Themes����������������������������������������������������������������������������������������������������������������������������������������������� 242 Creating Your Own Themes�������������������������������������������������������������������������������������������������������������������������������� 242 Changing the Application Icon and Title������������������������������������������������������������������������������������������������������������ 243 Securing Your Application���������������������������������������������������������������������������������������������������������244 Securing Methods��������������������������������������������������������������������������������������������������������������������������������������������� 244 Summary�����������������������������������������������������������������������������������������������������������������������������������245 ■■Chapter 9: Creating and Using RIA Services������������������������������������������������������������������247 Creating an RIA Services Project�����������������������������������������������������������������������������������������������247 Creating an Entity Class������������������������������������������������������������������������������������������������������������������������������������� 248 Creating a Domain Service Class���������������������������������������������������������������������������������������������������������������������� 250 Retrieving Data��������������������������������������������������������������������������������������������������������������������������252 Retrieving a Connection String from web.config�����������������������������������������������������������������������255 Updating Data����������������������������������������������������������������������������������������������������������������������������256 Calling SQL Stored Procedures�������������������������������������������������������������������������������������������������258 Consuming Your RIA Service�����������������������������������������������������������������������������������������������������261 Summary�����������������������������������������������������������������������������������������������������������������������������������263 ■■Chapter 10: Sharing Data with OData����������������������������������������������������������������������������265 What Is OData?��������������������������������������������������������������������������������������������������������������������������265 Using External Data Sources�����������������������������������������������������������������������������������������������������265 Finding Third-Party Data Sources to Use����������������������������������������������������������������������������������������������������������� 266 Determining Connection Settings���������������������������������������������������������������������������������������������������������������������� 266 xiii www.it-ebooks.info ■ Contents Connecting to an OData Data Source���������������������������������������������������������������������������������������������������������������� 267 Understanding Default Data Source Options����������������������������������������������������������������������������������������������������� 269 Exposing Your LightSwitch Data via OData�������������������������������������������������������������������������������269 Finding Your Application’s End Point URL���������������������������������������������������������������������������������������������������������� 270 Querying OData�������������������������������������������������������������������������������������������������������������������������������������������������� 271 Securing OData Feeds��������������������������������������������������������������������������������������������������������������������������������������� 271 Consuming LightSwitch Data����������������������������������������������������������������������������������������������������272 Connecting to LightSwitch Data from Excel������������������������������������������������������������������������������������������������������ 272 Connecting to LightSwitch Data from NET������������������������������������������������������������������������������������������������������� 272 Connecting to Use LightSwitch from Other Platforms��������������������������������������������������������������������������������������� 278 Summary�����������������������������������������������������������������������������������������������������������������������������������279 ■■Chapter 11: Creating and Using Custom Controls���������������������������������������������������������281 Using Custom Controls��������������������������������������������������������������������������������������������������������������281 Specifying a Custom Control����������������������������������������������������������������������������������������������������������������������������� 282 Binding Data to Custom Controls����������������������������������������������������������������������������������������������������������������������� 284 Binding Data Collections to Custom Controls���������������������������������������������������������������������������������������������������� 286 Converting Values When Data Binding��������������������������������������������������������������������������������������������������������������� 288 Creating a Custom Silverlight Control���������������������������������������������������������������������������������������291 Understanding Dependency Properties������������������������������������������������������������������������������������������������������������� 292 Creating a New Control and Dependency Property������������������������������������������������������������������������������������������� 293 Binding Dependency Properties to the Data Context����������������������������������������������������������������������������������������� 298 Applying the Duration Control on a Screen�������������������������������������������������������������������������������������������������������� 299 Calling Custom Control Methods via Dependency Properties���������������������������������������������������������������������������� 300 Calling Screen Code from a Custom Control�����������������������������������������������������������������������������303 Summary�����������������������������������������������������������������������������������������������������������������������������������308 ■■Chapter 12: Creating Control Extensions�����������������������������������������������������������������������309 Using LightSwitch Extensions���������������������������������������������������������������������������������������������������309 Installing LightSwitch Extensions���������������������������������������������������������������������������������������������������������������������� 309 Using the Many-to-Many Data Control�������������������������������������������������������������������������������������������������������������� 310 Preparing Your Computer to Develop Extensions����������������������������������������������������������������������312 xiv www.it-ebooks.info ■ Contents Understanding Custom Control Types���������������������������������������������������������������������������������������313 Creating Value Controls�������������������������������������������������������������������������������������������������������������314 Specifying Which Data Types to Support����������������������������������������������������������������������������������������������������������� 316 Supporting the FindControl Method������������������������������������������������������������������������������������������������������������������� 316 Setting the Control Icon������������������������������������������������������������������������������������������������������������������������������������� 318 Running and Deploying Your Extension�������������������������������������������������������������������������������������323 Setting Product/Installation Attributes��������������������������������������������������������������������������������������������������������������� 324 Creating a Detail Control (ComboBox)���������������������������������������������������������������������������������������325 Setting the Control’s Metadata�������������������������������������������������������������������������������������������������������������������������� 326 Finding the Summary Property for an Entity����������������������������������������������������������������������������������������������������� 334 Creating a Helper Class������������������������������������������������������������������������������������������������������������������������������������� 334 Creating Custom Property Editors���������������������������������������������������������������������������������������������341 Customizing Visual Studio’s Property Editor������������������������������������������������������������������������������������������������������ 341 Customizing the Runtime Designer������������������������������������������������������������������������������������������������������������������� 352 Creating a Group Control Extension������������������������������������������������������������������������������������������357 Setting the Visibility of Labels��������������������������������������������������������������������������������������������������������������������������� 359 Creating a Command Control Extension������������������������������������������������������������������������������������360 Summary�����������������������������������������������������������������������������������������������������������������������������������363 ■■Chapter 13: Creating Data and Presentation Extensions�����������������������������������������������365 Creating a Business Type Extension������������������������������������������������������������������������������������������365 Associating Custom Controls with Business Types������������������������������������������������������������������������������������������� 366 Enforcing Validation������������������������������������������������������������������������������������������������������������������������������������������� 367 Creating Custom Property Editor Windows�������������������������������������������������������������������������������������������������������� 374 Using Your Business Type���������������������������������������������������������������������������������������������������������������������������������� 382 Creating a Custom Shell Extension�������������������������������������������������������������������������������������������383 Preparing Your Project��������������������������������������������������������������������������������������������������������������������������������������� 384 Defining the Look of Your Shell�������������������������������������������������������������������������������������������������������������������������� 384 Binding Data to Your Shell��������������������������������������������������������������������������������������������������������������������������������� 388 Displaying Your Application’s Logo�������������������������������������������������������������������������������������������������������������������� 389 Adding Code That Supports our Custom Shell��������������������������������������������������������������������������������������������������� 390 xv www.it-ebooks.info ■ Contents Managing Screens��������������������������������������������������������������������������������������������������������������������������������������������� 408 Persisting User Settings������������������������������������������������������������������������������������������������������������������������������������ 410 Setting the Name and Description��������������������������������������������������������������������������������������������������������������������� 412 Using Your Custom Shell������������������������������������������������������������������������������������������������������������������������������������ 412 Creating a Custom Theme Extension�����������������������������������������������������������������������������������������413 Applying a Different Font����������������������������������������������������������������������������������������������������������������������������������� 413 Setting Different Colors������������������������������������������������������������������������������������������������������������������������������������� 414 Creating a Screen Template Extension��������������������������������������������������������������������������������������416 Setting Template Properties������������������������������������������������������������������������������������������������������������������������������ 416 Defining the Data Source Type�������������������������������������������������������������������������������������������������������������������������� 418 Generating Screen Controls������������������������������������������������������������������������������������������������������������������������������� 418 Generating Screen Code������������������������������������������������������������������������������������������������������������������������������������ 423 Creating More Complex Screen Templates�������������������������������������������������������������������������������������������������������� 425 Creating a Data Source Extension���������������������������������������������������������������������������������������������425 Creating an Entity Class������������������������������������������������������������������������������������������������������������������������������������� 425 Creating the Data Service Class������������������������������������������������������������������������������������������������������������������������ 428 Using the Data Source Extension���������������������������������������������������������������������������������������������������������������������� 433 Summary�����������������������������������������������������������������������������������������������������������������������������������433 ■■Chapter 14: Creating Reports����������������������������������������������������������������������������������������435 Choosing a Reporting Option�����������������������������������������������������������������������������������������������������435 Using ASP.NET to Create Reports����������������������������������������������������������������������������������������������436 Creating an ASP.NET Project������������������������������������������������������������������������������������������������������������������������������ 436 Adding an ASP.NET Page������������������������������������������������������������������������������������������������������������������������������������ 437 Creating Charts�������������������������������������������������������������������������������������������������������������������������������������������������� 441 Securing Reports����������������������������������������������������������������������������������������������������������������������������������������������� 442 Deploying Your ASP.NET Application������������������������������������������������������������������������������������������������������������������ 444 Using Microsoft SQL Server Reporting Services�����������������������������������������������������������������������445 Installing Reporting Services����������������������������������������������������������������������������������������������������������������������������� 445 Creating Reports������������������������������������������������������������������������������������������������������������������������������������������������ 445 xvi www.it-ebooks.info ■ Contents Linking Reports to LightSwitch�������������������������������������������������������������������������������������������������452 Opening Reports in a New Browser Window����������������������������������������������������������������������������������������������������� 452 Displaying Reports Inside a LightSwitch Screen����������������������������������������������������������������������������������������������� 453 Printing Reports������������������������������������������������������������������������������������������������������������������������������������������������� 458 Opening Reports in an HTML Client Application������������������������������������������������������������������������������������������������ 459 Creating Reports with Microsoft Word��������������������������������������������������������������������������������������459 Performing Simple Automation�������������������������������������������������������������������������������������������������������������������������� 459 Performing Mail Merges with Word������������������������������������������������������������������������������������������������������������������� 466 Creating Adobe PDF Documents������������������������������������������������������������������������������������������������475 Using Other Third-Party Solutions���������������������������������������������������������������������������������������������479 Summary�����������������������������������������������������������������������������������������������������������������������������������479 ■■Chapter 15: Sending E-mail�������������������������������������������������������������������������������������������481 Choosing How to Send E-mail���������������������������������������������������������������������������������������������������481 Sending E-mail by Using Server-Side Code������������������������������������������������������������������������������482 Sending E-mail When Data Changes����������������������������������������������������������������������������������������������������������������� 483 Making Your E-mail Code Reusable������������������������������������������������������������������������������������������������������������������� 485 Using Configuration Settings to Save Credentials��������������������������������������������������������������������������������������������� 488 Triggering Server E-mail from an Onscreen Button on the Client��������������������������������������������������������������������� 489 Sending E-mails with File Attachments������������������������������������������������������������������������������������������������������������� 496 Creating Mail in a Pickup Folder�����������������������������������������������������������������������������������������������499 Sending Mail via Outlook by Using COM Automation����������������������������������������������������������������500 Opening the Default Mail Program��������������������������������������������������������������������������������������������504 Using mailto in a Silverlight Client��������������������������������������������������������������������������������������������������������������������� 505 Using mailto in the HTML Client������������������������������������������������������������������������������������������������������������������������ 507 Calling External Resources from LightSwitch Clients���������������������������������������������������������������508 Using AJAX from the HTML Client���������������������������������������������������������������������������������������������������������������������� 509 Calling Web Services from the Silverlight Client����������������������������������������������������������������������������������������������� 512 Summary�����������������������������������������������������������������������������������������������������������������������������������521 xvii www.it-ebooks.info ■ Contents ■■Chapter 16: Authenticating Your Users�������������������������������������������������������������������������523 Choosing an Authentication Method������������������������������������������������������������������������������������������523 Disabling Authentication������������������������������������������������������������������������������������������������������������������������������������ 524 Enabling Windows Authentication��������������������������������������������������������������������������������������������������������������������� 525 Enabling Forms Authentication�������������������������������������������������������������������������������������������������������������������������� 526 Understanding Where User Details Are Stored��������������������������������������������������������������������������527 Changing the Password Complexity Rules�������������������������������������������������������������������������������������������������������� 529 Changing Password-Encryption Settings���������������������������������������������������������������������������������������������������������� 530 Sharing Forms Authentication Data with ASP.NET��������������������������������������������������������������������������������������������� 530 Allowing Users to Log Out of Web Applications������������������������������������������������������������������������������������������������� 532 Summary�����������������������������������������������������������������������������������������������������������������������������������535 ■■Chapter 17: Authorizing Your Users������������������������������������������������������������������������������537 Applying Permissions����������������������������������������������������������������������������������������������������������������537 Defining Permissions����������������������������������������������������������������������������������������������������������������539 Setting Up Administrators Who Can Manage Users������������������������������������������������������������������������������������������� 539 Debugging Permissions������������������������������������������������������������������������������������������������������������������������������������� 540 Applying Table-Level Access Control����������������������������������������������������������������������������������������������������������������� 540 Applying Screen-Level Access Control�������������������������������������������������������������������������������������������������������������� 544 Applying Query-Level Permissions�������������������������������������������������������������������������������������������������������������������� 547 Specifying the Application Administrator����������������������������������������������������������������������������������547 Creating and Managing Roles���������������������������������������������������������������������������������������������������548 Custom Authorization Examples������������������������������������������������������������������������������������������������548 Associating Logins with Your Data�������������������������������������������������������������������������������������������������������������������� 548 Opening Screens Conditionally at Login������������������������������������������������������������������������������������������������������������ 552 Restricting Row-Level Access to Data��������������������������������������������������������������������������������������������������������������� 553 Setting Screen Control Properties by Permission���������������������������������������������������������������������������������������������� 554 Allowing Users to Bypass Validation������������������������������������������������������������������������������������������������������������������ 555 Summary�����������������������������������������������������������������������������������������������������������������������������������556 xviii www.it-ebooks.info ■ Contents ■■Chapter 18: Deploying Your Application������������������������������������������������������������������������557 Getting Started with Deployment ���������������������������������������������������������������������������������������������557 Choosing an Application Topology��������������������������������������������������������������������������������������������������������������������� 559 Choosing Whether to Publish or Package���������������������������������������������������������������������������������������������������������� 559 Setting Up Your Workstations���������������������������������������������������������������������������������������������������������������������������� 560 Setting Up SQL Server��������������������������������������������������������������������������������������������������������������������������������������� 560 Using the Publish Wizard�����������������������������������������������������������������������������������������������������������560 Data Connections Page�������������������������������������������������������������������������������������������������������������������������������������� 562 Prerequisites Page��������������������������������������������������������������������������������������������������������������������������������������������� 564 Security Page���������������������������������������������������������������������������������������������������������������������������������������������������� 565 Digital Signature/Certificate Page��������������������������������������������������������������������������������������������������������������������� 566 Setting Up Your Web Server������������������������������������������������������������������������������������������������������568 Setting Up IIS on Windows Server 2008/2012��������������������������������������������������������������������������������������������������� 569 Setting Up IIS on Windows 8, 7, or Vista������������������������������������������������������������������������������������������������������������ 570 Setting Up IIS on Windows 2003��������������������������������������������������������������������������������������������������������������������� 571 Setting Up IIS Application Pools ������������������������������������������������������������������������������������������������������������������������ 573 Configuring SQL Server Windows Authentication���������������������������������������������������������������������������������������������� 575 Configuring Secure Sockets Layer (SSL) ���������������������������������������������������������������������������������������������������������� 576 ASP.NET Configuration �������������������������������������������������������������������������������������������������������������������������������������� 578 Installing the LightSwitch Server Components������������������������������������������������������������������������������������������������� 578 Installing the Packages�������������������������������������������������������������������������������������������������������������579 Installing a Two-Tier Desktop Application���������������������������������������������������������������������������������������������������������� 579 Installing a Three-Tier Browser and Desktop Applications�������������������������������������������������������������������������������� 580 Three-Tier Manual Deployment ������������������������������������������������������������������������������������������������583 Deploying Data��������������������������������������������������������������������������������������������������������������������������583 Updating an Application������������������������������������������������������������������������������������������������������������584 Troubleshooting Deployment Errors������������������������������������������������������������������������������������������584 Tracing Your Application ����������������������������������������������������������������������������������������������������������������������������������� 585 xix www.it-ebooks.info ■ Contents Deploying to Azure��������������������������������������������������������������������������������������������������������������������587 Connecting Visual Studio to Azure��������������������������������������������������������������������������������������������������������������������� 587 Choosing an Azure Service Type������������������������������������������������������������������������������������������������������������������������ 589 Creating a Database������������������������������������������������������������������������������������������������������������������������������������������ 591 Completing the Publishing Process������������������������������������������������������������������������������������������������������������������� 592 Publishing to SharePoint�����������������������������������������������������������������������������������������������������������593 Summary�����������������������������������������������������������������������������������������������������������������������������������595 ■■Appendix A: Culture Names�������������������������������������������������������������������������������������������597 ■■Appendix B: Data Type Identifiers���������������������������������������������������������������������������������603 ■■Appendix C: Using Properties in Custom Controls���������������������������������������������������������605 ■■Appendix D: Custom Screen Template View IDs������������������������������������������������������������609 ■■Appendix E: HelpDesk Tables�����������������������������������������������������������������������������������������613 HelpDesk Tables������������������������������������������������������������������������������������������������������������������������613 External Tables��������������������������������������������������������������������������������������������������������������������������624 Database Diagram���������������������������������������������������������������������������������������������������������������������625 Index���������������������������������������������������������������������������������������������������������������������������������627 xx www.it-ebooks.info About the Author Tim Leung is a professional software developer based in England For the past 15 years, he has specialized in enterprise application development using products from the Microsoft technology stack In particular, he possesses deep knowledge of Microsoft LightSwitch, the Microsoft NET Framework and SQL Server He is an active member of the U.K developer community and often delivers presentations on technical topics He is also a chartered member of the British Computer Society He is passionate about the concept of rapid application development and was awarded the Microsoft Community Contributor Award in 2011 for contributions to the LightSwitch community xxi www.it-ebooks.info About the Technical Reviewers Stephen Provine is a principal software design engineer on the Microsoft Visual Studio LightSwitch team He has 12 years of experience working across many aspects of the Visual Studio rapid-application-development (RAD) experiences, including the Server Explorer, SQL database tooling and overall data designer extensibility in the Visual Studio IDE Most recently, he has focused on the architecture and design for the LightSwitch HTML client introduced in Visual Studio 2012 Update Prior to working at Microsoft, Stephen received a bachelor’s degree in Computer Science from Harvard University Fabio Claudio Ferracchiati is a senior consultant and a senior analyst/developer using Microsoft technologies He works for Brain Force (www.brainforce.com) in its Italian branch (www.brainforce.it) He is a Microsoft Certified Solution Developer for NET, a Microsoft Certified Application Developer for NET, a Microsoft Certified Professional, and a prolific author and technical reviewer Over the past ten years, he’s written articles for Italian and international magazines and coauthored more than ten books on a variety of computer topics xxiii www.it-ebooks.info Acknowledgments I’d like to thank everyone who supported me during the writing of this book In particular, I’d like to thank my development editor Tom Welsh for all the help he’s given me throughout this project Stephen Provine spent a lot of time reviewing this book and went out of his way to answer my technical questions I highly appreciate all the help that he gave me Beth Massi at Microsoft has helped me countless times in the past, and I’m very grateful for her support Last but not least, I’d like to thank Katie Sullivan, Ewan Buckingham, and everybody at Apress for making this book possible xxv www.it-ebooks.info Foreword Every business uses software to automate their business functions At their heart, such applications are about gathering, storing, and processing data, so such applications could involve what we typically think of under the rubric of “business applications”—for example, tracking finances or assets (as in ERP software) But it also includes the process of software development, systems management, or anything else involving data There is a lot of this business software out there, a lot more being written, and even more that people wish they had the time, budget, and skill to write Building such software involves two concerns: the business problem to be solved (the domain) and the technology into which a solution is rendered First you have to have one person who understands both the domain and the technology necessary to render it into software, or you have to have a team with a mix of skills That’s enough to kill many small projects Assuming you can get the right people together, the technologist then spends a great deal of time on issues that have nothing to with the business problem being solved, including UI, protocols, business logic mechanism, security, integration with Microsoft Office, and much, much more One needs a good deal of skill, time, inclination, and budget to get a project accomplished To help people write business applications faster, we created Microsoft Visual Studio LightSwitch, the simplest way for developers of all skill levels to develop business applications for the desktop and cloud Using LightSwitch, the core technical problems are solved and a lot of projects—that without LightSwitch would have never seen the light of day—are now in production Visual Studio LightSwitch 2012 provides a conceptual and practical introduction to many core LightSwitch building blocks, including importing and defining data schema, designing queries and screens, validating data, authenticating and authorizing application users, and deploying the final product However, the challenge with rapid application development environments is that they’re great at solving the problems they anticipated, but what if you need to more? Happily, LightSwitch was designed without the glass ceiling that constrains the tools of the 4GL generation, so the full power of Visual Studio is available to you if you want it—the limit is your imagination Tim has a lot of imagination and has explored many ways to supplement LightSwitch in this book He offers solutions for a number of problems that LightSwitch does not address but that you may encounter as you write your applications His is a cookbook Some recipes won’t have ingredients you like, some you’ll season to fit your taste, some will open possibilities you hadn’t even considered, and some you’ll use as is A sampling includes sending e-mail, creating reports, and implementing auditing He shares a passion with the LightSwitch team and with their readers: to build great business applications, fast Together we can make businesses more successful through software Steve Anonsen, Distinguished Engineer, Microsoft Visual Studio xxvii www.it-ebooks.info ... and files From Visual Studio s Build menu, select the Build Solution option After Visual Studio builds your project, use Windows Explorer to view your project files Notice how LightSwitch generates... Chapter ■ Introducing LightSwitch Figure 1-1.  LightSwitch s architecture The Data Layer LightSwitch s architecture defines a separate logical data layer This means that LightSwitch stores your... build and compile a LightSwitch application Let’s begin by taking a look at LightSwitch s architecture Understanding LightSwitch s Architecture In keeping with best practices, LightSwitch applications

Ngày đăng: 11/03/2019, 17:09

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan