. NET Program Development implements a layer-3 architecture in c #

Source: Internet
Author: User

This article discusses how to implement a layer-3 architecture in c # and use the MS Access database to store data. Here, I implemented a small reusable component in the three-layer architecture to save customer data. It also provides the function of adding, updating, and searching customer data.

Background

First, I will introduce some theoretical knowledge about the layer-3 architecture. Simple Description: What is a layer-3 architecture? What are the advantages of a layer-3 architecture?

What is a layer-3 architecture?

The layer-3 architecture is a "client-server" architecture in which user interfaces, business logic, data storage, and data access are designed as independent modules. There are three main layers: the first layer (presentation layer, GUI layer), the second layer (Business Object, business logic layer), and the third layer (data access layer ). These layers can be developed and tested separately.

Why do we divide program code into three layers. Separating the user interface layer, business logic layer, and data access layer has many advantages.

Commercial logic components are reused in rapid development. We have added, updated, deleted, and searched for customer data components in the system. This component has been developed and tested. We can use this component in other projects that want to save customer data.

The system is relatively easy to migrate. the business logic layer and the data access layer are separated. Modifying the data access layer does not affect the business logic layer. If the system migrates data from SQL Server storage to Oracle storage, you do not need to modify the business logic layer components and GUI components.

The system is easy to modify. If there is a small modification on the business layer, we do not need to reload the entire system on the user's machine. We only need to update the business logic component.

Application developers can develop separate layers in parallel.

Code

This component has three layers. The first layer, or GUI layer, is implemented using form and is called FrmGUI. The second layer, or the business logic layer, is BOCustomer, short for Bussniess Object Customer. The third layer or Data layer is called DACustomer, which is short for Data Access Customer. For convenience, I compiled three layers into a project.

User Interface Layer

The following is a piece of code generated by the user interface. I only selected some code that calls the business logic layer.

// This function get the details from the user via GUI

// Tier and callthe Add method of business logic layer.

Private void upload add_click (object sender, System. EventArgs e)

{

Try

{

Cus = new BOCustomer ();

CusID = txtID. Text. ToString ();

Cus. LName = txtLName. Text. ToString ();

Cuus. FName = txtFName. Text. ToString ();

Cuus. Tel = txtTel. Text. ToString ();

Cuus. Address = txtAddress. Text. ToString ();

Cus. Add ();

}

Catch (Exception err)

{

MessageBox. Show (err. Message. ToString ());

}

}

 

// This function gets the ID from the user and finds

// Customer details and return the details in the form

// A dataset via busniss object layer. Then it loops through

// The content of the dataset and fills the controls.

 

Private void upload find_click (object sender, System. EventArgs e)

{

Try

{

String cusID = txtID. Text. ToString ();

 

BOCustomer thisCus = new BOCustomer ();

DataSet ds = thisCus. Find (cusID );

 

 

DataRow row;

Row = ds. Tables [0]. Rows [0];

 

// Via looping

Foreach (DataRow rows in ds. Tables [0]. Rows)

{

TxtFName. Text = rows ["CUS_F_NAME"]. ToString ();

TxtLName. Text = rows ["CUS_L_NAME"]. ToString ();

TxtAddress. Text = rows ["CUS_ADDRESS"]. ToString ();

TxtTel. Text = rows ["CUS_TEL"]. ToString ();

}

}

Catch (Exception err)

{

MessageBox. Show (err. Message. ToString ());

}

 

}

 

// This function used to update the customer details.

Private void cmdUpdate_Click (object sender, System. EventArgs e)

{

Try

{

Cus = new BOCustomer ();

CusID = txtID. Text. ToString ();

Cus. LName = txtLName. Text. ToString ();

Cuus. FName = txtFName. Text. ToString ();

Cuus. Tel = txtTel. Text. ToString ();

Cuus. Address = txtAddress. Text. ToString ();

 

Cus. Update ();

}

Catch (Exception err)

{

MessageBox. Show (err. Message. ToString ());

}

}

Business logic layer

The following is all the code at the business logic layer, mainly including defining the attributes of the customer object. However, this is only a fictitious customer object. If necessary, you can add other attributes. The business logic layer also includes adding, updating, searching, and other methods.

The business logic layer is an intermediate layer in the GUI layer and data access layer. He has a reference to the Data access layer cusData = new DACustomer (). It also references the System. Data namespace. The business logic layer uses DataSet to return data to the GUI layer.

Using System;

Using System. Data;

Namespace _ 3 tierarchitecture

{

/// <SUMMARY>

/// Summary description for BOCustomer.

/// </SUMMARY>

Public class BOCustomer

{

// Customer properties

Private String fName;

Private String lName;

Private String cusId;

Private String address;

Private String tel;

Private DACustomer cusData;

Public BOCustomer ()

{

// An instance of the Data access layer!

CusData = new DACustomer ();

}

/// <SUMMARY>

/// Property FirstName (String)

/// </SUMMARY>

Public String FName

{

Get

{

Return this. fName;

}

Set

{

Try

{

This. fName = value;

If (this. fName = "")

{

Throw new Exception (

"Please provide first name ...");

}

}

Catch (Exception e)

{

Throw new Exception (e. Message. ToString ());

}

}

}

/// <SUMMARY>

/// Property LastName (String)

/// </SUMMARY>

Public String LName

{

Get

{

Return this. lName;

}

Set

{

// Cocould be more checkings here eg revmove 'chars

// Change to proper case

// Blah

This. lName = value;

If (this. LName = "")

{

Throw new Exception ("Please provide name ...");

}

}

}

/// <SUMMARY>

/// Property Customer ID (String)

/// </SUMMARY>

Public String cusID

{

Get

{

Return this. cusId;

}

Set

{

This. cusId = value;

If (this. cusID = "")

{

Throw new Exception ("Please provide ID ...");

}

}

}

/// <SUMMARY>

/// Property Address (String)

/// </SUMMARY>

Public String Address

{

Get

{

Return this. address;

}

Set

{

This. address = value;

 

If (this. Address = "")

{

Throw new Exception ("Please provide address ...");

}

}

}

/// <SUMMARY>

/// Property Telephone (String)

/// </SUMMARY>

Public String Tel

{

Get

{

Return this. tel;

}

 

Set

{

This. tel = value;

If (this. Tel = "")

{

Throw new Exception ("Please provide Tel ...");

}

}

}

/// <SUMMARY>

/// Function Add new customer. CILS

/// The function in Data layer.

/// </SUMMARY>

Public void Add ()

{

CusData. Add (this );

}

/// <SUMMARY>

/// Function Update customer details.

/// Callthe function in Data layer.

/// </SUMMARY>

Public void Update ()

{

CusData. Update (this );

}

/// <SUMMARY>

/// Function Find customer. callthe

/// Function in Data layer.

/// It returns the details of the customer using

/// Customer ID via a Dataset to GUI tier.

/// </SUMMARY>

Public DataSet Find (String str)

{

If (str = "")

Throw new Exception ("Please provide ID to search ");

DataSet data = null;

Data = cusData. Find (str );

Return data;

}

}

}

 

Data access layer

The data layer includes details about how to process the MS Access database. All these details are transparent and will not affect the business logic layer. The data access layer has a reference BOCustomer cus pointing to the business logic layer. To facilitate applications and support other databases.
Using System;

Using System. Data. OleDb;

Using System. Data;

Namespace _ 3 tierarchitecture

{

/// <SUMMARY>

/// Summary description for DACustomer.

/// </SUMMARY>

Public class DACustomer

{

Private OleDbConnection cnn;

// Change connection string as per

// Folder you unzip the files

Private const string CnnStr =

"Provider = Microsoft. Jet. OLEDB.4.0; Data" +

"Source = D :\\ Rahman_Backup \ Programming \" +

"Csharp \ 3tierarchitecture \ customer. mdb ;";

// Local variables

Private String strTable = "";

Private String strFields = "";

Private String strValues = "";

Private String insertStr = "";

// This needs to be changed based on customer

// Table fields 'name of the database!

Private const String thisTable = "tblCustomer ";

Private const String cus_ID = "CUS_ID ";

Private const String cus_LName = "CUS_L_NAME ";

Private const String cus_FName = "CUS_F_NAME ";

Private const String cus_Tel = "CUS_TEL ";

Private const String cus_Address = "CUS_ADDRESS ";

Public DACustomer ()

{

}

Public DACustomer (BOCustomer cus)

{

// A reference of the business object class

}

// Standard dataset function that adds a new customer

Public void Add (BOCustomer cus)

{

String str = BuildAddString (cus );

OpenCnn ();

// Open command option-cnn parameter is imporant

OleDbCommand cmd = new OleDbCommand (str, cnn );

// Execute connection

Cmd. ExecuteNonQuery ();

// Close connection

CloseCnn ();

}

// Standard dataset function that updates

// Details of a customer based on ID

Public void Update (BOCustomer cus)

{

OpenCnn ();

String selectStr = "UPDATE" + thisTable +

"Set" + cus_LName + "= '" + cus. LName + "'" +

"," + Cus_FName + "= '" + cus. FName + "'" +

"," + Cus_Address + "= '" + cus. Address + "'" +

"," + Cus_Tel + "= '" + cus. Tel + "'" +

"Where cus_ID = '" + cusID. cusID + "'";

OleDbCommand cmd = new OleDbCommand (selectStr, cnn );

Cmd. ExecuteNonQuery ();

CloseCnn ();

}

// Standard dataset function that finds and

// Return the detail of a customer in a dataset

Public DataSet Find (String argStr)

{

DataSet ds = null;

Try

{

OpenCnn ();

String selectStr = "select * from" + thisTable +

"Where cus_ID = '" + argStr + "'";

OleDbDataAdapter da =

New OleDbDataAdapter (selectStr, cnn );

Ds = new DataSet ();

Da. Fill (ds, thisTable );

CloseCnn ();

}

Catch (Exception e)

{

String Str = e. Message;

}

Return ds;

}

Private void OpenCnn ()

{

// Initialise connection

String cnnStr = CnnStr;

Cnn = new OleDbConnection (cnnStr );

// Open connection

Cnn. Open ();

}

Private void CloseCnn ()

{

// 5-step five

Cnn. Close ();

}

// Just a supporting function that builds

// And return the insert string for dataset.

Private String BuildAddString (BOCustomer cus)

{

// These are the constants

// Set in the top of this module.

StrTable = "Insert into" + thisTable;

StrFields = "(" + cus_ID +

"," + Cus_LName +

"," + Cus_FName +

"," + Cus_Address +

"," + Cus_Tel + ")";

// These are the attributes of

// Customer business object.

StrValues = "Values ('" + cus. cusID +

"','" + Cus. LName +

"','" + Cus. FName +

"','" + Cus. Address +

"','" + Cus. Tel + "')";

InsertStr = strTable + strFields + strValues;

Return insertStr;

}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.