In this part, I will start the implementation of the presentation tier for Windows Mobile, and if you want some background, please see the previous parts:
Part 1 was a general introduction Part 2 outlined the architecture (tiers, etc) Part 3 showed the benefit of loosely coupled tiers (distribution, cloud, etc) Part 4 started the implementation by creating the entity data model (using ADO.NET Entity Framework) Part 5 published the entity data model as a data service (using ADO.NET Data Services) Part 6 implemented the business domain (using the data service) Part 7 created the service (using WCF) The implemented architecture is published on CodePlex in a project called KISS Architecture, and this means that you can access the full source code as well as discuss it, come with suggested improvements, etc. As I walk you through the creation of the architecture, I suggest you keep the source code handy to check out more details.
I start the implementation of a Windows Mobile client by creating a new project of type "Smart Device Project", name it Kiss.Mobile, select "Windows Mobile 5.0 Smartphone SDK", ".NET Compact Framework Version 3.5", and "Device Application". This will allow it to run on many of the devices on the market, and still use the latest framework with new functionality. That new functionality include WCF, so let's move on by creating a reference to the WCF service I created in the previous part (7). You do that by using the tool NetCFSvcUtil that is included in the Power Toys for .NET Compact Framework 3.5. For more details on using this tool, please see A New Mobile N-tier Architecture (part 8).
Let's implement a simple user interface by renaming the automatically generated form (Form1) to MainForm, and add three controls (a Label named cityLabel, a TextBox named cityTextBox, and a DataGrid named simply dataGrid) to it. Also, add a MainMenu named mainMenu and add menu options to it named doneMenuItem, getMenuItem, and updateMenuItem. The result should look similar to the figure on the left.
With the service reference and the user interface in place, it's time to implement some presentation logic. Here's the code for the "Get" menu item:
private void getMenuItem_Click(object sender, EventArgs e)
{
OrderServiceClient.EndpointAddress = new EndpointAddress("http://192.168.0.100:2222/OrderService.svc");
OrderServiceClient service = new OrderServiceClient();
Customer[] customers = service.GetCustomersByCity(cityTextBox.Text);
dataGrid.DataSource = customers;
}
Note how easy it would be to redirect the client to use the service in another location (other server, cloud, etc) by just changing the service URI. Also note how the definition of the entity (Customer) have come all the way from the data service without any manual coding anywhere, and when it is changed, it's simply a matter of updating the service references in all tiers. The observant will note that the List<Customer> return value of the service is transformed into a plain array when serialized by WCF, but if a list is preferred, this code could be used:
List<Customer> customers = service.GetCustomersByCity(cityTextBox.Text).ToList();
The code for the "Update" menu item looks like this:
private void updateMenuItem_Click(object sender, EventArgs e)
{
Customer customer = ((Customer[])dataGrid.DataSource)[0];
customer.City = customer.City + "X";
OrderServiceClient service = new OrderServiceClient();
service.UpdateCustomer(customer);
}
It first takes the first row in the grid, and adds an "X" to the City attribute. Then the service is called with the updated entity instance (customer). It can't be much simpler than this!
In the next part, I will continue the implementation of the Windows Mobile client by adding offline support.