This is a follow-up on my blog series (see last part) on the mobile blog Windows Mobile Development about creating a simple architecture that make use of the latest technologies in the simplest way.
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 implementation, I suggest you keep the source code handy to check out more details.
In this post I will show how a simple Windows Forms client can be implemented to use the KISS architecture.
I start by creating a "Windows Forms Application" project named Kiss.WinForm, and then add a service reference to the service (see part part 7 for details) named OrderService.
Now, let's implement a simple user interface by renaming the automatically generated form (Form1) to MainForm, and add five controls (a Label named cityLabel, a TextBox named cityTextBox, two buttons named getButton and updateButton, and a DataGridView named simply dataGrid) to it. 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" button:
private void getButton_Click(object sender, EventArgs e)
{
OrderServiceClient service = new OrderServiceClient();
service.Endpoint.Address = new EndpointAddress("http://localhost:2222/OrderService.svc");
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 (local, 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" button looks like this:
private void updateButton_Click(object sender, EventArgs e)
{
OrderServiceClient service = new OrderServiceClient();
Customer customer = ((Customer[])dataGrid.DataSource)[dataGrid.CurrentRow.Index];
customer.City = customer.City + "X";
service.UpdateCustomer(customer);
}
It takes the customer from the currently selected 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!
As the same technology is available on the (Windows) client, the full architecture (with all its tiers) can be run locally and thereby enabling offline mode (e.g. when not network connection exist). In this scenario, it would be beneficial to use the Sync Framework for data synchronization, and that framework will probably also be involved in the upcoming "offline" version of ADO.NET Data Services (see the Astoria team blog for more info).
In the next part, I will show how a Web client (ASP.NET) can make use of the KISS architecture.