47,99 €
Microsoft Silverlight is a programmable web browser plugin that enables features including animation, vector graphics, and audio-video playback – features that characterize Rich Internet Applications. However, Silverlight is a great (and growing) Line-Of-Business platform and is increasingly being used to build data-driven business applications. Silverlight Data Services enable efficient access to your data, allowing you to draw on multiple sources of data and solve particular data problems. There is very little existing material that demonstrates how to build data-driven solutions with the platform. Silverlight 3 made a big step into Line-Of-Business data services and Silverlight 4 builds further upon this. This book will enable .NET developers to get their finger on the pulse of data-driven business applications in Silverlight.This book is not a general Silverlight 3/4 overview book; it is uniquely aimed at developers who want to build data-driven applications. It focuses on showing .NET developers how to interact with, and handle multiple sources of data in Silverlight business applications, and how to solve particular data problems following a practical hands-on approach, using real-world recipes. It is a practical cookbook that teaches you how to build data-rich business applications with Silverlight that draw on multiple sources of data. Most of the covered features work both in Silverlight 3 and 4. However, we cover some that are specific for Silverlight 4, which will therefore not work with Silverlight 3. Where this is the case, it is clearly indicated.Packed with reusable, real-world recipes, the book begins by introducing you to general principles when programming Silverlight. It then dives deep into the world of data services, covering all the options available to access data and communicate with services to get the most out of data in your Silverlight business applications, whilst at the same time providing a rich user experience. Chapters cover data binding, data controls, the concepts of talking to services, communicating with WCF, ASMX and REST services, and much more.By following the practical recipes in this book, which are of varying difficulty levels, you will learn the concepts needed to create data-rich business applications—from the creation of a Silverlight application, to displaying data in the Silverlight application and upgrading your existing applications to use Silverlight. Each recipe covers a data services topic, going from the description of the problem, through a conceptual solution to a solution containing sample code.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 546
Veröffentlichungsjahr: 2010
Copyright © 2010 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, Packt Publishing, nor its dealers or distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: April 2010
Production Reference: 1200410
Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK.
ISBN 978-1-847199-84-3
www.packtpub.com
Cover Image by Sandeep (<[email protected]>)
Authors
Gill Cleeren
Kevin Dockx
Reviewers
Kevin DeRudder
Mike Hanley
Tarkan Karadayi
Kris Van Der Mast
Acquisition Editor
Kerry George
Development Editor
Neha Patwari
Technical Editors
Roger D'souza
Arani Roy
Copy Editor
Sneha Kulkarni
Indexer
Monica Ajmera Mehta
Editorial Team Leader
Mithun Sehgal
Project Team Leader
Priya Mukherji
Project Coordinator
Zainab Bagasrawala
Proofreader
Chris Smith
Aaron Nash
Production Coordinator
Melwyn D'sa
Cover Work
Melwyn D'sa
Gill Cleeren is Microsoft's Regional Director, MVP ASP.NET, INETA speaker bureau member, and Silverlight Insider. He lives in Belgium, where he works as a .NET architect. Since its introduction, Gill has been a .NET enthusiast. For the first few years of his career, he developed ASP.NET applications. The very day that Silverlight was introduced to the world, back in 2007, he started working with the technology. Since then, he has given many training sessions and developed several applications, both with Silverlight and WPF.
As MVP and Regional Director, Gill is also very active in the Belgian and international community as a speaker and user group leader for Visug, the Belgian .NET user group. His blog on .NET, ASP.NET, and Silverlight can be found at www.snowball.be.
For me, writing a book about the things I like to do has always been a dream. For some time now, Silverlight has been my hobby and passion. I spend hours in front of my laptop, both during and after working hours, staring at some Silverlight project. When James Lumsden of Packt Publishing offered me the possibility to write a book on Silverlight, I was immediately carried away by the idea. Packt brought me in touch with Kevin Dockx, also from Belgium, who agreed on co-authoring this book. Days after that, we sat together and started working on the book you're holding in your hands now. During the writing period, the people of Packt have been most helpful in every way. I really want to thank Kerry George, Neha Patwari, and Zainab Bagasrawala for replying to the countless mails we have exchanged! I also want to thank the review team: Kevin DeRudder, Mike Hanley, Tarkan Karadayi, and Kris Van Der Mast. And of course, a very big thank you to both my mother and my lovely girlfriend Lindsey, for the patience during the days I spent more time writing than being with you.
Kevin Dockx lives in Belgium and works at RealDolmen, one of Belgium's biggest ICT companies, where he is a technical specialist/project leader on .NET web applications, mainly Silverlight, and a solution manager for Rich Applications (Silverlight, WPF, Surface).His main focus lies on all things Silverlight, but he still keeps an eye on the new developments concerning other products from the Microsoft .NET (Web) Stack. As a Silverlight enthusiast, he's a regular speaker on various national and international events, like Microsoft DevDays in The Netherlands or on BESUG events (the Belgian Silverlight User Group). His blog, which contains various tidbits on Silverlight, .NET, and the occasional rambling, can be found at http://blog.kevindockx.com.
I'd like to thank anyone who helped me out with this book, especially Maarten Balliauw, who pointed me in the right direction for the Windows Azure recipes. Besides that, I'd also like to thank my friends for sticking around while I was busy spending my time writing this book instead of socializing. :-)
Kevin DeRudder is a .NET trainer and consultant at U2U, mainly focusing on the WEB. In the last years, Kevin was involved in lots of Silverlight and ASP.NET projects for a variety of clients and projects.
Mike Hanley is the VP of Engineering at Vertigo Software, Inc. (http://www.vertigo.com). He has worked as a Software Engineer, specializing on the Microsoft Platform for over 10 years. Recently, Mike has spent a great deal of time working on Silverlight applications for clients ranging from Hard Rock International to the Democratic National Committee. Most recently, Mike has worked on several live video events using IIS Smooth Streaming. Mike is a Certified Scrum Master and graduated from Harvey Mudd College with a B.S. in Computer Science. You can learn more about Mike on his blog: http://www.michaelhanley.org. During his spare time, Mike enjoys spending time with his wife Tylar and his first son Max. Mike also has an identical twin brother Kevin.
Tarkan Karadayi has been writing code since the age of 14. He has a Masters in Computer Science and is currently working as a Lead Developer.
I would like to thank my wife Anna, my parents, and my three sons Taran, Kyle, and Ryan for their love and support.
Kris van der Mast, an active and dedicated moderator at the official Microsoft ASP.NET forums, has reserved himself a place among the Most Valuable Professionals since 2007 and in 2009 earned the ASP Insider title. He's also a known member of several of the Belgian user groups.
Kris works for Ordina Belgium, a consultancy company working for large corporates in Belgium and abroad, as a senior .NET developer and architect. He also provides courses to clients in his specialization and technical interests, being web technologies.
This practical cookbook teaches you how to build data-rich business applications with Silverlight that draw on multiple sources of data. Most of the features covered work both in Silverlight 3 and 4. However, we cover some that are specific to Silverlight 4, which will therefore not work with Silverlight 3. Where this is the case, it will be clearly indicated.
Packed with reusable, real-world recipes, the book begins by introducing you to general principles when programming Silverlight. It then dives deep into the world of data services, covering all the options available to access data and communicate with services to make the most out of data in your Silverlight business applications, whilst at the same time providing a rich user experience. Chapters cover data binding, data controls, concepts of talking to services, communicating with WCF, ASMX, and REST services and much more.
By following the practical recipes in this book, which are of varying difficulty levels, you will learn concepts for creating data-rich business applications—from the creation of a Silverlight application, to displaying data in the Silverlight application and upgrading your existing applications to use Silverlight. Each recipe will cover a data services topic, going from the description of the problem, over a conceptual solution and a solution containing sample code.
Chapter 1, Learning the Nuts and Bolts of Silverlight 4 will get you up and running with Silverlight. While this book is aimed at developers who already have a basic knowledge of Silverlight, this chapter can act as a refresher. We'll also look at getting your environment correctly set up so that you enjoy developing Silverlight applications.
Chapter 2, An Introduction to Data Binding will explore how data binding works. We'll start by building a small data-driven application that contains the most important data binding features, to get a grasp of the general concepts. We'll also see that data binding isn't tied to just binding single objects to an interface; binding an entire collection of objects is supported as well. We'll also be looking at the binding modes. They allow us to specify how the data will flow (from source to target, target to source, or both). We'll finish this chapter by looking at the support that Blend 4 provides to build applications that use data binding features. In the next chapter, we'll be looking at the more advanced concepts of data binding.
Chapter 3, Advanced Data Binding teaches you advanced data binding concepts that can be used for customization, validations, and applying templates to data bound controls. We also have a look at converters, that are obvious hooks in data binding and their possibilities.
Chapter 4, The Data Grid covers recipes on how to work with the DataGrid. This is an essential control for applications that rely on (collections of) data.
Chapter 5, The DataForm covers recipes on how to work with the DataForm, which is an essential control for the applications that rely on (collections of) data.
Chapter 6, Talking to Services talks about the rich set of options that Silverlight provides to communicate with services.
Chapter 7, Talking to WCF and ASMX Services discovers Silverlights' built-in support for communicating with Windows Communication Foundation (WCF) and classic ASMX web services. Perform unidirectional as well as bidirectional communication with much better performance using net.tcp binding in WCF using the recipes in this chapter.
Chapter 8, Talking to REST and ADO.NET Data Services takes advantage of REST, which can be significant in the case of Silverlight. We will also look at how we can work with ADO.NET Data Services. You will abstract away a lot of plumbing code with the use of the client-side library that is available for use with Silverlight.
Chapter 9, Talking to WCF RIA Services discovers a new, free framework developed by Microsoft-WCF RIA Services- to simplify Line of Business RIA development. This chapter will teach you to easily design and develop rich data-driven applications by simplifying the access to the validation of and the authentication of services, service methods, and entities.
Chapter 10, Converting Your Existing Applications to Use Silverlight, integrates Silverlight into existing web applications and performs a step-by-step migration where Silverlight starts to become part of a system. This chapter is all about techniques to leverage your existing knowledge, applications, and business requirements to Silverlight. This chapter contains a recipe that shows how to enable WCF RIA Services for existing applications.
Appendix talks about creating a REST service from WCF, installing a SQL Server database, and working with Fiddler and Silverlight control toolkit
To work with the recipes in this book, it's required that you have Visual Studio installed. This book targets Silverlight 4, which works only with Visual Studio 2010. Many of the recipes in the book will also work in Silverlight 3, so for these recipes, you have the choice of Visual Studio 2008 or 2010. We do recommend using Visual Studio 2010, as it features a lot of enhancements for developing with Silverlight. In both cases, you'll of course need to install the Silverlight Tools, which will update your Visual Studio instance to work with Silverlight. Some recipes also require Blend 4 to be installed on your machine (again, if working with Silverlight 3, Blend 3 will suffice here as well).
The first recipe of Chapter 1, Getting our environment ready to start developing Silverlight applications, explains in detail how to get these tools and how to install them.
If you are a .NET developer who wants to build professional data-driven applications with Silverlight, then this book is for you. Basic Silverlight experience and familiarity with accessing data using ADO.NET in regular .NET applications is required.
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: "Finally, the DataReader and connection are closed and the StoreDTO object is returned."
A block of code will be set as follows:
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in our text like this: "Do this by right-clicking, selecting Add New Item, and then selecting LINQ TO SQL Classes.".
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply drop an email to <[email protected]>, and mention the book title in the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or email to <[email protected]>.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Visit http://www.packtpub.com/files/code/9843_Code.zip to directly download the example code.
The downloadable files contain instructions on how to use them.
Although we have taken every care to ensure the accuracy of our contents, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in text or code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration, and help us to improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the let us know link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.
In this chapter, we will cover the following topics:
While we assume some basic knowledge of Silverlight for this book, we also know that developers have very little time to grasp all the new technologies that keep coming out. Therefore, this first chapter contains all that we need to know to get going with Silverlight. We'll also guide you through the required tools and installations for a perfect Silverlight development environment.
Silverlight was released in the first half of 2007, and since then, it has created a lot of buzz. While ASP.NET is a server-side development platform, with the arrival of Silverlight, the focus has shifted to the client side again. A Silverlight application runs in the browser of the client and on a specific version of the Common Language Runtime (CLR).
A big benefit for developers is that Silverlight uses .NET from version 2 onwards. It has a trimmed-down version of the Base Class Library (BCL) that is impressively extended considering the size of the Silverlight plugin (less than 5 MB). Because of the similarities, many skills achieved from developing applications in the full .NET framework can be leveraged for the creation of Silverlight applications.
Silverlight itself can be considered as a trimmed-down version of its desktop counterpart, Windows Presentation Foundation (WPF). Between Silverlight 4 and WPF 4, there are still some differences. Some features are included in WPF 4, but aren't in Silverlight 4 and vice versa. It's possible to reuse code written for one technology in the other. However, upfront planning is required to ensure a smooth transition between the two technologies. Microsoft has released a whitepaper based on this aspect that provides more information on how to write applications that target both Silverlight and WPF. This document can be found at http://wpfslguidance.codeplex.com.
With the release of Silverlight 2, Microsoft made it clear that Silverlight is aimed at both creating rich and interactive applications, and next-level enterprise applications in the browser. The latter can be easily seen with the addition of a rich control set, support for many types of services and platform features such as data binding.
Due to its client-side characteristics, Silverlight applications need to perform particular tasks to get data. It doesn't support client-side databases—not even in version 4. The way to retrieve data is through services. Silverlight 3 brought some interesting features to the platform in this area such as support for binary XML, the WCF RIA services, and simplified duplex service communication. Silverlight 4 continued in the same manner, with improvements in data binding, support for net.tcp communication, cross-domain access to services by means of Trusted Silverlight applications, and much more. All these added features are a proof of the commitment Microsoft is making to position Silverlight as a platform for building enterprise applications.
In this chapter, we'll get you up and running with Silverlight. While this book is aimed at developers who already have a basic knowledge of Silverlight, this chapter can act as a refresher. We'll also look at getting your environment correctly set up so that you enjoy developing Silverlight applications.
In this recipe, we'll look at what we need to install to start developing Silverlight applications. We'll learn about the basic tools that we need as a developer and also take a look at the designer tools that can come in handy for developers as well.
To start developing Silverlight applications, we'll need to install the necessary tools and SDKs. Carry out the following steps in order to get started:
For Silverlight development, the minimum that we need are the developer tools. These will integrate with Visual Studio 2008 (if you're using Silverlight 3) or 2010. In Visual Studio 2010, a nice, visual designer is added for editing our XAML code. In the 2008 version, this designer doesn't exist. When installing the developer tools for Silverlight 4, the following components are automatically downloaded and installed:
We can write XAML code using Visual Studio. However, if you're serious about designing, you might want to consider using Microsoft Expression Blend. This tool, primarily aimed at designers, should be seen as an application that generates XAML for us by means of a rich number of options and an easy-to-use interface. It also integrates nicely with Visual Studio and source control software integration is available as well.
After having installed all the necessary tools, it might be worth taking a look at the Creating our first service-enabled and data-driven Silverlight 4 application using Visual Studio 2010 recipe as well as the Using the workflow between Visual Studio 2010 and Blend 4 recipe. In these recipes, we create an entire application in Visual Studio 2010 and Blend 4 respectively.
When working on slightly larger projects in teams, source control is an absolute necessity. By far, the most popular source control system in the Microsoft world today is Team Foundation Server (TFS). This recipe explains all that we need to get TFS to work with Silverlight applications in Visual Studio and Blend. It doesn't explain how to work with TFS itself.
Before getting started, make sure that you have installed Blend 4 and the necessary developer tools as described in the Getting our environment ready to start developing Silverlight applications section.
To start using TFS as a versioning system with a Silverlight-enabled solution, we need to perform the following steps:
Team Foundation Server source control is the preferred way of enabling version control on our projects. Explaining in detail how to work with TFS is beyond the scope of this book, but the following are a few basic steps and references:
While working a lot with TFS, an interesting feature to download is the Team Foundation Server Power Tools. This is a set of extra features that is added to TFS and is mainly aimed at power users. It can be downloaded at http://msdn.microsoft.com/en-us/teamsystem/bb980963.aspx.
A complete glossary of TFS terms can be found at http://msdn.microsoft.com/en-us/library/ms242882.aspx. As a reference, the following are a few of the more commonly used ones along with their brief explanation:
Term
Explanation
TFS workspace
A TFS workspace is a location on the Team Foundation Server (TFS) where a record of changes between local files and corresponding repository files is stored. It can also be thought of as a copy of the client-side directory, a staging ground where local changes are persisted until they are checked into the server, or a collection of working folder mappings that can be viewed and edited.
Working folder
A working folder should be seen as a client-side representation of the TFS workspace. Binding the TFS workspace to the client-side working folder is done through a TFS workspace mapping.
Check in
Check in refers to the task of committing a pending change/pending changes to a TFS repository. When you check in pending changes, a new changeset is created on the server.
Check out
Check out refers to the task of notifying the TFS server that you are changing the status of a resource from locked to writeable. When you check out for edit, TFS appends an edit to that resource.
Get latest
Get latest refers to the task of retrieving the most recent version of a file from the TFS source control to your local working folder.
Once we have a Silverlight application ready, we will want to show it to the rest of the world. This means deploying it!
While Silverlight is a .NET technology, it doesn't require .NET to be installed on the server. Remember that it's a client-side technology. The Silverlight plugin on the client will download and run the application using the version of the Common Language Runtime (CLR) embedded in the Silverlight plugin. In this recipe, we'll look at how we can deploy a Silverlight application.
Deploying a Silverlight application is easy; the Silverlight code is compiled and wrapped into a *.xap file. Getting this file on the client side and running it from there is our only concern. The following steps are to be carried out for deploying a Silverlight application:
