Share via


Component-based Development for Mobile Devices

 

Christian Forsberg
businessanyplace.net

September 2003

Applies to:

    Microsoft® .NET Compact Framework 1.0
    Microsoft Visual Studio® .NET 2003

Summary: Learn how component-based development (CBD) and multi-tiered design can be applied to your mobile solutions. Much of the knowledge achieved during years of PC application design can be utilized when designing Pocket PC applications, and this article will both explain why and how you can do that. (7 printed pages)

Download PPC_NET_app_design.exe

Contents

Introduction
Multi-tier Design Recap
Pocket PC Multi-tier Design
Tiers Anyplace Sample
Code Walkthrough
Conclusion

Introduction

As new technology platforms are created, developer communities often develop new ideas and ways to implement applications on the new platform. However, at the same time many well-known, traditional platform designs are forgotten and sometimes reinvented. If you are an experienced PC developer this article should confirm what you already know, and if you've just started developing for Pocket PC you can benefit from the valuable experience of PC solution design.

The word "component" in this article can in most cases be replaced by "DLL."

Multi-tier Design Recap

PC developers are familiar with the idea of a three-tier application design. The three basic tiers are:

  • User Interface Services
  • Business Logic Services
  • Data Services

The names of the tiers often differ depending on where you look, but the purpose of each tier is more or less standardized.

The User Interface Services tier implements the presentation (forms, buttons, and so on) and presentation logic (navigation, conditions for exiting forms, and so on). Business Logic Services implements most of the application logic (business rules, and so on), and Data Services mainly takes care of retrieving and storing the information in a database or other store (queries, adding, updating, and removing). In this simplified view, Data Services also includes the actual storing of information in a structured and persistent way, which is normally handled by a database engine. From this model a huge variety of designs with many more tiers can be derived.

Many Pocket PC developers do not believe this design will pay off for them. One reason could be that developers believe there is no need to separate tiers on such a small application. Many view a Pocket PC application as being simpler than a PC application; they're fooled by the small screen. It is necessary to design the user interface in a more efficient way (fewer controls, simpler input methods, and so on) compared to a corresponding PC application. Developers might think that the "simplification" of an application into a monolithic, single file, binary will save memory on a device. Remember that most early PC applications were also designed to run alone on the PC, and application integration, especially on the client, was not a big issue. Today, many Pocket PC applications, not to mention the operating system itself, share a lot of functionality in common components.

Benefits of multi-tiered design include:

  • Component-based development. When functionality is divided into separate components, implementation becomes less important than published interfaces. As a user of a component, you need only focus on the desired functionality and how to access it through the interface.
  • Reusability. Other developers can focus on using the functionality in your components rather than implementing the functionality themselves. With .NET, it doesn't even matter in what language the component was implemented.
  • Clear separation of responsibility. Developers working on different components of the same project need only worry about the interfaces published by the other developers because the components underneath those interfaces function independently.
  • Maintainability. With component-based development, bugs are easier to trace, and because other parts of the application would not be affected by bugs contained in one component, the developer maintaining the app can focus on keeping the interfaces consistent.
  • Distribution. While single-file installations are fairly simple from a developer's perspective, most enterprise installations require multiple file installations anyway—possibly the inclusion of third-party controls. Also, installing the same functionality with many applications ultimately proves problematic.

Pocket PC Multi-tier Design

There are two basic Pocket PC application designs: one that runs independently on the device—even if it may synchronize data with a server—and one that runs completely on the server, such as a Web application. The Pocket PC browser is used to present the user interface.

A high-level overview of the logical tiers that cover the full spectrum of application designs could look like this:

Figure 1. Application tiers on device and server (PC)

The various components in a Pocket PC application—forms, controls, business classes, and utilities—can be categorized by how commonly they are used. Different levels could be for everyone, for company applications, for department application, or just for this specific application. This categorization is important when deciding how to partition various classes among different components.

Tiers Anyplace Sample

This is a sample application for the Pocket PC created with Microsoft Visual Studio® .NET 2003, C#, and the Microsoft .NET Compact Framework. It shows how to implement a multi-tiered solution and consists of one form:

Figure 2. Tiers Anyplace sample

The form shows a list of customers from the sample Northwind database, and the following figure illustrates what happens when the form is loaded.

Figure 3. Sequence diagram (UML)

As shown above, the sample is implemented using a multi-tiered design in which user services, business logic service, and data services are implemented as separate components.

Code Walkthrough

The solution consists of four projects:

  1. Tiers—main (startup) project with user interface (form)
  2. Business—component that holds business logic classes
  3. Data—component that holds data access classes
  4. Common—component that holds general functionality

Here's an overview of the projects (components):

Figure 4. Project (component) overview

Each of the components has its own namespace—Tiers.UserServices, Tiers.BusinessServices, Tiers.DataServices, and Tiers.CommonServices—and only reference the needed child tiers, as shown in the Figure 4.

The Data component only contains one class, Customer, and the full implementation looks like this:

using System;
using System.Data;
using System.Data.SqlServerCe;
using Tiers.CommonServices;

namespace Tiers.DataServices
{
  public class Customer

    public DataSet GetAll()
    {
      SqlCeConnection cn = 
        new SqlCeConnection(@"Data Source=\sample.sdf");
      SqlCeDataAdapter da =
        new SqlCeDataAdapter("SELECT * FROM Customers", cn);
      cn.Open();
      DataSet ds = new DataSet();
      da.Fill(ds, "Customer");
      return ds;
    }
  }
}

The GetAll method simply creates a database connection and a DataAdapter. Then, all rows from the Customer table are retrieved and returned as a DataSet. A sample Microsoft SQL Server™ CE database with the Customer table from the Northwind sample is included with the sample code.

The full code for the Customer class in the Business component looks like this:

using System;
using System.Data;
using Tiers.CommonServices;
using Tiers.DataServices;

namespace Tiers.BusinessServices
{
  public class Customer
  {
    public DataSet GetList()
    {
      DataServices.Customer customer = new DataServices.Customer();
      return customer.GetAll();
    }
  }
}

Nothing magical happening here as the business logic class method (GetList) simply forwards the request to the data services class.

Now, let's look at the code for the Form, which starts with:

using Tiers.CommonServices;
using Tiers.BusinessServices;
The Form's Load event looks like this:
// Set font size (8) on Heading
FontHandler.SetFont(lblHeading, "Tahoma", 8, FontStyle.Bold);

// Create Customer business object
Customer customer = new Customer();

// Get Customer list
DataSet ds = customer.GetList();

// Fill ListView from DataSet
ListViewItem lit;
foreach (DataRow dr in ds.Tables[0].Rows)
{
  lit = new ListViewItem(dr["CompanyName"].ToString());
  lvwItems.Items.Add(lit);
}

The call to FontHandler.SetFont is a call to the Common component that makes it possible to set font size to 8 points, which is the preferred size for dialog form captions.

A Customer business object is created and the DataSet retrieved through the three tiers is used to fill the ListView.

Even if this sample only makes a simple call through the tiers, it can be used as a starting point to include more common functionality (insert, update, save, and so on).

Conclusion

Numerous PC applications demonstrate that component-based design is the best approach to take when developing mobile applications, and the new tools in Visual Studio .NET will help save time both in application development and maintenance.