Windows 8 introduces a number of new features that developers can use to create compelling applications and form rich UX. Unfortunately, these features are not always easy to unit test. Features such as sharing and auxiliary tiles can increase the interactivity and interest of your application, but they can also become less easily tested.
In this article, I'll show you a variety of different ways to let your application use features such as sharing, setting, secondary tiles, application settings, and application storage. By using the model-view-view model (MVVM) pattern, dependency injection, and some abstractions, I'll show you how to take advantage of these features while maintaining the presentation layer for easy unit testing.
About the sample application
To illustrate the concepts that will be discussed in this article, I have used MVVM to write a sample Windows application store application that users can use to view blog posts through their favorite blogs ' RSS feeds. The application explains how to:
Share information about a blog post with other applications through the "Share" super button
Change the blog that users want to read with the "Settings" Super button
Use a secondary tile to secure your favorite blog posts to the "Start" screen for later reading
Save your favorite blog for viewing on all devices with roaming settings
In addition to the sample application, I used the specific Windows 8 features that will be discussed in this article and abstracted them into an open source library called Charmed. Charmed can be used as a helper library or as a reference only. The goal of Charmed is to become a cross-platform MVVM support library for Windows 8 and Windows Phone 8. I'll talk more about the library's Windows Phone 81-Face in a future article. The progress of the Charmed library can be understood in BIT.LY/17AZFXW.
My goal for this article and for the sample code is to demonstrate that I'm using some of the new features provided by Windows 8 to develop a testable application that uses MVVM mode.
MVVM Overview
Before delving into the code and specific Windows 8 features, I'll briefly introduce MVVM. MVVM is a popular design pattern in recent years in terms of XAML based technology, including Windows Presentation Foundation (WPF), Silverlight, Windows Phone 7, Windows Phon E 8 and Windows 8 (Windows Runtime, abbreviation WinRT). MVVM divides the application architecture into three logical tiers: models, view models, and views, as shown in Figure 1.
Figure 1: Three logical tiers of the model-view-view model
The model layer involves the business logic of the application, that is, business objects, data validation, data access, and so on. In fact, the model layer is usually divided into more layers and possibly even multiple levels. As shown in Figure 1, the model layer is the bottom of the logical meaning of the application, or is called the Foundation.
The view model layer accommodates the application's presentation logic, including the data to be displayed, the properties that help enable UI elements or make them visible, and the methods that will interact with both the model and view layers. Basically, the view model layer is a view-independent representation of the current state of the UI. I say "irrelevant to view" because it provides data and methods only for the view that you want to interact with, without indicating how the view will represent the data or allow the user to interact with the methods. As shown in Figure 1, the view model layer is logically located between the model layer and the view layer and can interact with the latter. The view model layer contains code in hidden code that would have previously been in the view layer.
The view layer contains the actual representation of the application. For XAML-based applications, such as Windows Runtime applications, the view layer is primarily, if not all, composed of XAML. The view layer uses a powerful XAML data binding engine to bind to properties on the view model, while applying a skin to data that has no visual representation in other cases. As shown in Figure 1, the view layer is the top of the logical meaning of the application. The view layer interacts directly with the view model layer, but knows nothing about the model layer.
The primary purpose of the MVVM pattern is to separate the representation of an application from its functionality. This makes the application more useful for unit testing because the functionality is now in the normal old CLR object (POCO) rather than in the view of the life cycle itself.
Contract
Windows 8 introduces the concept of a contract, which is the agreement between two or more applications for the user system. These contracts align all applications and enable developers to take advantage of these features from any supporting functional application. The application can declare its supported contracts in the Package.appxmanifest file, as shown in Figure 2.
Figure 2:package.appxmanifest The contract in the document