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

Glossary Item Box

Creating the foundations of your application

Before taking care of the user interface, 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. Since those two layers are common to all types of applications, their creation won't be detailed in this article; instead this article extends the The Persistence and the Business Layers one by using them in a Windows Forms rich client.

Preparing the WinForms application development environment

 First of all we're going to add a new WinForms Application project to our solution. To do so:

At this point your Sample.WinFormsApplication should look like this:

Project

Before coding our Windows Forms application, we need to configure it properly. First, we're going to add a configuration section to the App.config file of our project that indicates our Business Object Model (BOM) to which database to connect to.

Open the App.config file in Visual Studio and add the following below the configuration XML node:

App.config Copy Code
<configSections>
   
<section name="Sample" type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime" />
</
configSections>
<
Sample connectionString="server=(local);database=Sample;Integrated Security=true"/>

Then verify that your account has the permissions to connect to the Sample database on your SQL server.

To do so:

Note: CodeFluent doesn't necessarily need the db_owner rights on a database. For simplicity's sake, this tutorial grants them in order to avoid any unsufficient permissions later on.

Now that our environment is properly configured (our application has a connection string pointing to its database, and is executed under an account with the rights to read and update data), 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.

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 BOM, almost no code behind is needed, 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. 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;
namespace Sample.WinFormsApplication
{
   
public partial class Form1 : Form
   {
       
public Form1()
       {
           InitializeComponent();
           UpdateListBox();
       }
       
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;
       }
   }
}

In the end, the Windows Forms Application will look as so:

UI