Building a .NET Desktop Application

Introduction

In my previous two articles, we first looked at the four common types of .NET applications we may find out in the field. We briefly discussed these four types of applications and then, in the second article, we looked at the design and architecture at a high level, which would apply to all these applications in general terms. We also looked at the details of the business case we will be developing. We now move on to build the first type of .NET application, the Desktop application.

Requirements

It is assumed the reader of this article has some basic knowledge and understanding of .NET development and C#, which is the programming language used for developing all the samples. In addition, the following tools would be required,

  1. SQL Server 2008 Express Edition
  2. SQL Server Management Studio
  3. Visual Studio 2019 Community Edition

All the above tools are free to download and use. I will be building the application using the .NET Framework 4.7.2. So, let’s begin.

The Data Source

The first step is to build the data source, where we will be saving the data. Here, I will create a new database called “EmployeeDB” and create an “Employee” table inside it, as shown below:

Data source

The Employee_ID field will be created as an identity field to store the unique key value for any employee record. The complete Employee table will look like below:

Database

Next, we will add one record into the table, as shown below:

Add one record

This completes the data store which will be holding our data.

The Data Access Layer

This will be the first piece we build using the .NET code and Visual Studio 2019. Start by creating a solution called “EmployeeDesktop” and project as below:

Class library

The first project we create will be to hold the “Employee” class which represents an employee record as it is in the data store. We keep it in a separate project in order to use it across different projects independently from the data access project.

Class

The final “Employee” class looks like below:

Employee store

As you can see, this is a direct representation of the “Employee” table in the database.

Next, we move onto create the data access layer project.

Add a new project, “EmployeeDAL”, to the solution:

Class library

Configure your new project

After creating the project add the “IEmployeeDAL” interface and “EmployeeDAL” class as shown below:

Employee dal

List of employee

This completes the data access layer and its related items. We now have the part of the solution which will read the data from the data store and store it in a .NET class which exactly matches the record in the data store.

The Business Logic Layer

Next, we will create the business logic layer. Here, we create a new project called “EmployeeBL” which contains the “IEmployeeBL” interface and the “EmployeeBL” class which implements this interface. In addition to this, we create a new class called “Employee” inside a new project called “EmployeeUI”. This is the employee class as it will be displayed on the user interface. Hence, the function of the business logic layer is to read the data from the data access layer and apply business logic to it and pass it on to the front end. In this example, there is no complex logic and we simply add the first and last name to get the employee name, add the address components to get the address and use the data of birth value read from the database to get the age of the employee. Please see the components as shown below:

Employee ui

Collection generic

EmployeeBL

Up to this point, we have three main pieces of the solution complete. We have the data store, we have the data access layer which reads the data from the data store and stores it in a format which matches the data store and finally, we have the business logic layer which converts this data to the format required by the front end to interact with the user.

The Front end or presentation Layer

This is the final piece of the puzzle. Here, we will display the data we have read from the data store and converted to the required format to the user. For this desktop application, I am using a WPF application using the MVVM design pattern. I will not discuss this pattern in detail here and would urge the reader not familiar with this pattern to read about it from the numerous articles available online. I have created a “CommonLibraries” project which stores some common MVVM handling code, as shown below:

Component

Finally, we create a WPF application called “EmployeeFrontEndApp”, which contains the view “EmployeeDetails.xaml” and the related view model “EmployeeDetailsVM.cs”, as below:

User control

Employee front

Finally, we place the Employee view inside the Main Window as shown below:

Main window

The structure of the completed solution looks like below:

Structure

We now build the application and run it, see below.

Employee details

And there we go, we now have a complete working .NET desktop application that covers all architecture layers. This ensures the performance, scalability, extensibility, and maintainability of the application. I would leave it to the reader to further expand this application to add new features like adding a new employee and deleting an employee from the data source via the application.

Summary

We have just covered a simple .NET desktop application where we went through the different layers to store the data, read the data, convert data to our requirements and then present it to the end-user. A few things might be missing here like dependency injection, Entity framework for data persistence, etc. I plan to cover these in my next article when I create a .NET web application using the latest .NET core programming foundation.


Recommended Free Ebook
Similar Articles