Beginning Windows 8 Data Development
Total Page:16
File Type:pdf, Size:1020Kb
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. Contents at a Glance About the Author ...............................................................................................................xiii About the Technical Reviewer ............................................................................................ xv Acknowledgments ............................................................................................................ xvii ■ Chapter 1: Introduction to Windows 8 Development .........................................................1 ■ Chapter 2: HTML5 and JavaScript Apps with MVVM and Knockout ................................13 ■ Chapter 3: Windows 8 Modern App Data Access Options ...............................................29 ■ Chapter 4: Local Data Access: I: IndexedDB ....................................................................35 ■ Chapter 5: Local Data Access I: JET API and Application Data .......................................61 ■ Chapter 6: Local Data Access III: SQLite ..........................................................................89 ■ Chapter 7: ASP.NET Web API ..........................................................................................123 ■ Chapter 8: WCF Services ...............................................................................................147 ■ Chapter 9: Windows Azure Mobile Services ..................................................................179 ■ Chapter 10: Windows Phone 8 Data Access ..................................................................209 Index .................................................................................................................................229 v CHAPTER 1 Introduction to Windows 8 Development With Windows 8, Microsoft introduced significant changes to the underlying platform and user interface. These new features include a new start screen experience, Windows stores to purchase apps from a single repository, and a new platform known as Windows Runtime (WinRT). WinRT provides a new set of APIs and tools to create a new style of touch-first apps that are fast and fluid. These apps are generally called Windows Store Apps. For the purposes of this book, some of the key things to know about WinRT and Windows Store apps include • Windows 8 Apps runs in Windows X86, x64, and ARM processors. • Windows 8 Apps can either run in full-screen mode or be docked to the side of the screen. • WinRT supports programming languages such ac C, C++, VB.NET, and C#, along with HTML5 and JavaScript. • WinRT APIs are designed to be asynchronous. APIs that take more than 50 ms to run are made asynchronous. • The WPF/Silverlight XAML UI model is exposed to developers. • To ensure stability and security, the Windows Store Apps run within a sandboxed environment. • Finally, the most important thing to know is that there is no direct way to connect to the database servers using data providers in Windows RT. As this book is more about data access in Windows 8, this chapter provides an overview of the Windows 8 app framework and briefly looks into the development choices, UI data controls, MVVM patterns, and other necessary concepts that will be used in various examples throughout this book. In the later part of this chapter we’ll write our first data-driven Windows 8 App that displays the New York Times Best Sellers list. Windows App Framework In Figure 1-1, we see the Windows 8 modern-style app framework compared to that of desktop applications, where both share the same Windows core OS services. If we look at the desktop application section, JavaScript and HTML are used to target Internet Explorer, C and C++ are used for Win32 apps, and C# and Visual Basic for .NET and Silverlight. Each of these will have a separate set of APIs. But with Windows 8 Apps, we have one set of APIs that for WinRT whether we use XAML, C#, C/C++, Visual Basic, HTML/CSS, or JavaScript. 1 CHAPTER 1 ■ INTRODUCTION TO WINDOWS 8 DEVELOPMENT Figure 1-1. Windows App framework Development Choices For developing Windows 8 Apps, we can choose either of the two development paths shown in Figure 1-2. Figure 1-2. Development choices In the HTML path we will be able to use the traditional Web technologies like HTML5, CSS, and JavaScript. For presentation, you use HTML tags such as div, table, spans, and input, and CSS for styling. For coding, JavaScript can be used. Apart from the HTML controls, Windows Library for JavaScript provides a set of new controls designed for Windows Store Apps. This WinJS library is our path for the WinRT. If you are a WPF, Silverlight, or Windows Phone developer, then designing the UI and presentation layer using XAML is an ideal fit. Here we will be using C#, Visual Basic, or C++ for coding. 2 CHAPTER 1 ■ INTRODUCTION TO WINDOWS 8 DEVELOPMENT Creating the New York Times Best Sellers App The New York Times Best Sellers app is a simple Windows 8 App that uses the MVVM pattern to display the New York Times Best Sellers list. Building this app is a starting point to learn to use Visual Studio 2012, the MVVM framework, data binding, data controls, and other necessary concepts to create a data-driven Windows 8 Modern UI app. Introducing the MVVM Application Framework Model-View-ViewModel (MVVM) is the most widely used framework in WPF/Silverlight/Windows Phone XAML-based development. Considering MVVM as the central concept of Windows 8, it supports XAML-based development and is ideologically similar to the technologies that use MVVM as the application framework, so it is an ideal choice. This chapter introduces you to the MVVM framework. In later chapters you will learn about some of the most commonly used MVVM frameworks like MVVM Light and Prism. What Is MVVM? The MVVM pattern splits the user interface code into three conceptual parts: Model, View, and ViewModel (see Figure 1-3). The concept of the ViewModel is the new, and it controls the View’s interactions with the rest of the app. Figure 1-3. The basic relationships of the MVVM framework • Model represents actual data or information and holds only the data and not the behavior or service that manipulates the data. • View visually represents the data in the ViewModel by holding a reference to the ViewModel. • ViewModel serves as the glue between the View and the Model by exposing commands, notifiable properties, and observable collections to the View. 3 CHAPTER 1 ■ INTRODUCTION TO WINDOWS 8 DEVELOPMENT Advantages in Using MVVM These are the some of the advantages of using MVVM over other patterns: • The MVVM pattern is designed specifically for the data binding capabilities that are available in XAML applications, allowing views to be simple presentations that are abstracted from the business logic process, which should not happen at the user interface layer. • Another primary benefit of the MVVM pattern is the unit testability of codebase. The lack of connection between the View and ViewModel helps in writing the unit test against the ViewModel. • MVVM allows developers and UI designers to more easily collaborate when developing their respective parts of the application. • The MVVM pattern is widely used and there are several mature MVVM frameworks like Caliburn Micro and MVVMLight that provide all the base template code out of the way, of course, but they also can add advanced binding, behaviors, actions, and composition features. Setting Up the Development Environment Download the developer tools from http://dev.windows.com. The developer tool includes the Windows 8 Software Development Kit, a blend of Visual Studio and project templates. Microsoft Visual Studio for Windows 8 is our integrated development environment (IDE) to build Windows 8 Apps and this version runs only on Windows 8. Optionally, Microsoft Visual Studio 2012 can also be used. The full version has advanced debugging tool support, multi-unit testing framework and refactoring support, code analysis, profiling, and support for connecting to Team Foundation Server. ■ Note Windows 8 Apps cannot be developed with Windows 7, Windows Vista, or Windows XP. Visual Studio project templates give a great jump-start to building HTML and XAML applications. We create a new Visual C# Windows Store Blank App (XAML) project and name it NYTimesBestSeller (see Figure 1-4). 4 CHAPTER 1 ■ INTRODUCTION TO WINDOWS 8 DEVELOPMENT Figure 1-4. Visual Studio templates for XAML The New York Times Best Sellers app displays the details of the New York Times fiction best sellers in a grid view. Before we go further let’s see the project structure in Figure 1-5. 5 CHAPTER 1 ■ INTRODUCTION TO WINDOWS 8 DEVELOPMENT Figure 1-5. NYTimesBestSeller project structure In the default project structure, we have created three new folders via Models, Views, and ViewModel. These folders are used for the Models, Views, and ViewModel. Also we moved the MainPage.xaml to the Views folder. Creating the Model Now, we create the application's data model. This class are created in the Model folders in the C# file BookSellersModel.cs. The BookSellersModel.cs file implements two classes: • Book • BestSellersModel The Book class shown in Listing 1-1 represents details of one of the books on the best sellers list. The details include book title, description, author, and price. 6 CHAPTER 1 ■ INTRODUCTION TO WINDOWS 8 DEVELOPMENT Listing 1-1. Adding Book Class to the Project public class Book { public string Title { get; set; } public