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 architecture type 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:
• Select the solution in the Solution Explorer, right-click, and select Add, then New Project...
• In the project types tree view select the Windows type, and select the Windows Forms Application project template,
• Name it Sample.WinFormsApplication,
• Reference the Sample project, the CodeFluent.Runtime.dll, and WindowsBase.dll,
• Add an application configuration file named App.config to the project.
At this point your Sample.WinFormsApplication should look like this:
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:
• Open Microsoft SQL Management Studio,
• Connect to the instance pointed by your defined connection string,
• Select the Security folder in the instance and expand it,
• Select the Logins folder in the Security folder, and expand it,
• Select your login, right-click on it and select Properties,
• In the Login Properties window, select the User Mappings page,
• Map the Sample database to your account and grant it the db_owner rights and click OK.
![]() |
Note: CodeFluent Entities 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. |
Setting the Target framework
By default, Microsoft Visual Studio sets the target framework to .NET Framework 4 Client Profile. We're going to change the target to .NET Framework 4 in order to use the standard libraries.
![]() |
Note: Skip this section of the tutorial if you're using Microsoft Visual Studio 2008 |
Go in the Sample.WinFormsApplication project properties (right-click on the project -> Properties) and under Application -> Target framework, choose .NET framework 4.
Click Yes on the information popup.
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 which 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 flexible 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:
• Two TextBoxes named nameTextBox and addressTextBox,
• A Button named createButton, with the Click event handled in the OnCreateButtonClick method,
• A ListBox named customerListBox with the SelectedIndexChanged event handled in the OnSelectedIndexChanged method,
• and a PropertyGrid named propertyGrid.
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:

