CodeFluent Documentation Send comments on this topic.
A Custom Windows Forms Smart Client
CodeFluent Entities > Tutorials > Using The Core Edition > The Persistence, the Business, and the Service Layers > A Custom Windows Forms Smart Client

Glossary Item Box

Creating the foundations of your application

Before taking care of the smart client properly speaking, we must set up a data tier. The data tier is the place where the application will persist all of its data and is therefore commonly referred to as the persistence layer. On top of that layer, CodeFluent allows us to generate a middle tier which will contain a set of objects enabling developers to:

  1. manipulate the data objects stored in the persistence layer,

  2. define the business logic of our application.

Therefore, this middle tier is known as the Business Object Model (BOM). In the end, whatever the user interface or the type of architecture is, the persistence layer (containing the data) and the business layer (containing the business logic) are always the fundamental pillars of an application.

On top of those fundamental principles, in order to create a smart client we need to create a set of services that will expose them and allow our client to consume them remotely. This set of services corresponds to the services layer. Since those three layers are common to all types of smart clients, their creation won't be detailed in this article; instead this article extends the The Persistence, the Business, and the Service Layers one by using them in a Windows Forms smart client.

Preparing the Windows Forms application development environment

At the very beginning of the The Persistence, the Business, and the Service Layers tutorial we created the Visual Studio project corresponding to our smart client: Sample.SmartClient.

First of all, we need to include the client configuration that CodeFluent generated, so that our smart client knows to which WCF services to connect to. To do so:

Furthermore, we now need to add the needed references to consume the WCF services exposed. To do so:

At this point your Sample.SmartClient project should look like this:

Project 

Earlier in the tutorial, we configured the Sample project, so that the WCF services could be easily started and debugged. Now, we'll configure our solution so that on a debug action, it will launch: first the services, and then the client, so that we'll easily be able to debug the whole application. To do so:

This way, when we'll start debugging, Visual Studio will automatically launch our services, and then our smart client. Moreover, we'll be able to set breakpoints in both sides (client and server) or our application.

Now that our environment is properly configured, we can develop our User Interface (UI) that uses our CodeFluent generated components.

Developing the UI

The UI layer should only contain presentation code, all of the business logic should be in the Business Object Model (BOM). Hence, BOM objects are made to be data bound to, and provide advanced features to ease presentation layer developments such as validation capabilities, paging, sorting, caching, and so on. The Smart Client Object Model (SCOM) that we generated in the Sample.Proxy assembly is a remote version of the BOM containing the same capabilities (data binding, data validation, etc.) and taking care of all communication and transport matters. This way developer can truly focus on the UI and the business.

In this tutorial, we'll focus on a basic example that illustrates the data binding principle. We're going to create a Windows Form which allows end-users to create and view customers. You'll see that thanks to the SCOM, almost no code behind is needed, and using the WCF technology is completely abstracted. Hence, the presentation layer will contain exclusively presentation code: this is a capital concern in order to create highly evolutive and maintainable applications. Thanks to this architecture, adding user interfaces is made easier, since only the way of presenting the same business data and logic is changed.

In the Form1.cs class, we'll create our user interface. Create a user interface that contains:

In the OnCreateButtonClick method, we'll parse the content of the nameTextBox and the addressTextBox to create a new customer in database. Afterwards, we'll load all available customers from database and bind them to the customerListBox. The updateCustomerListButtonClick, will load all available customers from database. Finally, on the SelectedIndexChanged event of the ListBox, the selected customer will be bound to the propertyGrid control.

Here's the code behind corresponding to the here-before description:

Form1.cs Copy Code
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Sample.Proxy;
namespace Sample.SmartClient
{
   
public partial class Form1 : Form
   {
       
public Form1()
       {
           InitializeComponent();
       }
       
private void UpdateListBox()
       {
           customerListBox.DisplayMember =
"Name";
           customerListBox.ValueMember =
"Id";
           customerListBox.DataSource = CustomerCollection.LoadAll();
       }
       
private void OnCreateButtonClick(object sender, EventArgs e)
       {
           Customer customer =
new Customer();
           customer.Name = nameTextBox.Text;
           customer.Address = addressTextBox.Text;
           customer.Save();
           UpdateListBox();
       }
       
private void OnSelectedIndexChanged(object sender, EventArgs e)
       {
           propertyGrid.SelectedObject = customerListBox.SelectedItem;
       }
       
private void OnUpdateCustomerListButtonClick(object sender, EventArgs e)
       {
           UpdateListBox();
       }
   }
}

Pressing F5 should launch a console application hosting the WCF services, and then your Windows Forms application. Once the services are started, you can start using the WinForms client. Create several users and then list them.

Here's what you should get:

UI