Implementation of C # 93-tier architecture

Source: Internet
Author: User

2.1 Building a three-tier structure using a dataset

How are datasets used in the presentation layer, business logic layer, and data access layer layers when developing a three-tier architecture application system? The hierarchy of the dataset in a three-tier structure is as follows:


It can be seen that in the three-layer structure, the construction and parsing of the dataset is done mainly in the presentation layer, the data access layer, and the business logic layer mainly processes, processes and transmits the data in the dataset. Simply put, a dataset is a medium for data passing through a three-tier structure.

2.2 Use of datasets in a three-tier structure

2.2.1 Using a dataset in the presentation layer

There are two things you need to do to use a dataset in the presentation layer.

(1) Show the data in the dataset to the user.

In WinForm form controls, DataGridView (data table) controls, ComboBox (drop-down list) controls, and so on, they all have a data source property (DataSource), In general, we can bind a dataset or DataTable to the DataSource property for data presentation.

(2) Populate the dataset with the user's request data.

To populate a dataset with the user's request data, we first need to build a DataTable with the same structure as the user request data structure, populate the user's request with the built DataTable, and finally add the DataTable to the dataset.

The DataSet2.3 of the presentation layer is shown.



Completing the implementation process of presenting the data in a dataset to the user and populating the user's request data into a dataset is demonstrated in the following comprehensive example.

2.2.2 Using datasets in business logic

There are a few things you need to do to use a dataset at the business logic level:

(1) Pass the received dataset to the next layer.

When the business logic layer receives a dataset returned by the data access layer, it either passes the dataset to the presentation layer or passes the dataset that represents the request to the data access layer.

(2) The data in the dataset is processed according to the user's request.

When the business logic layer receives a dataset for a request or response, the data in the dataset is processed based on the user's request (for example, conditional filtering data) or a business rule.

The dataset for the business logic layer is as follows:




2.2.3 using datasets in the data access layer

Using datasets in the data access layer

Using a dataset in the data Access layer requires the following things:

(1) Populate the dataset with data from the database.

When a request is queried by a user, the data access layer needs to implement query access to the database and populate the dataset with the response results.

(2) Save the data in the dataset to the database.

When a user requests a data save request, the data access layer first parses the received dataset and then saves the parsed data to the database.

The dataset for the data access layer is as follows:




From the discussion above, we find that the dataset plays an important role as a data carrier in every layer of the three-tier structure, and each layer basically contains several steps to create a dataset, populate the data, pass a dataset, extract data from the dataset, and so on.

2.3 How to create a dataset

There are two ways to build a dataset.

(1) The data is populated directly into the dataset using the Fill method of the DataAdapter (data adapter).

(2) Manually encode the custom DataTable (data table), DataColumn (data column), DataRow (data row), and then add the data table to the dataset.

First, we review the DataSet, where a dataset is made up of multiple DataTable, and a DataTable is made up of multiple DataColumn and multiple DataRow.

Next, we will discuss the DataTable, DataColumn, and DataRow in depth separately.

1. DataTable

A DataTable is a relational data table in memory that can be created independently or used as a member of a dataset. How do I use a DataTable as a member of a dataset? First, we need to create a DataTable object, and then add it to the Tables collection of the DataSet object by using the Add method, as shown here:

DataSet Dsclass=newdataset ();

DataTable dtclass=newdatatable ("Class");

DSCLASS.TABLES.ADD (Dtclass);

If we add a DataTable to a DataSet without specifying a DataTable name, the table gets a default table name (example: Table0, Table1, Table2) that is incremented from "0".

2. DataColumn

DataColumn is the basis for creating a DataTable, and we define the structure of the DataTable by adding one or more DataColumn objects to the DataTable. DataColumn has some common properties for restricting input data, such as data type, data length, default value, and so on, as shown in the following table:

< strong>  

say    Ming

AllowDBNull

Whether null values are allowed

ColumnName

DataColumn name

DataType

stored data type

maxlength

Gets or sets the maximum length of a text column

DefaultValue

default

Table

The name of the DataTable that belongs to

unique

DataColumn The value is unique

There are two methods for defining DataColumn, for example One, example two:

Example one:

Datacolumnclassname=new DataColumn ();

Classname.columnname= "ClassName";

Classname.datatype=system.type.gettype ("System.String");

classname.maxlength=50;

Example two:

DataColumn classname=newdatacolumn ("ClassName", typeof (String));

classname.maxlength=50;

3. DataRow

The DataRow represents the actual data contained in the DataTable, and we can add the data to the DataTable defined in DataColumn with a DataRow, as shown in example three:

DataColumn classname=newdatacolumn ("ClassName", typeof (String));

classname.maxlength=50;

Create a new row of data

Datarowdrclass=dtclass.newrow ();

drclass["ClassName"]=this.txtclassname.text.trim ();

2.4 How to customize a dataset

The main steps for customizing the DataSet are as follows:

①, creates a DataSet object.

②, creating a DataTable object.

③, create DataColumn object build table structure.

④, add the created table structure to the table.

⑤, creating a DataRow object adds data.

⑥, inserting data into the table.

⑦, adds a table to the dataset.

Examples are as follows:

DataSet Dsclass=newdataset ();

Create a class table

DataTable dtclass=newdatatable ("Class");

Create a Grade ID column

Datacolumndcclassname=new DataColumn ("ClassName", typeof (String));

classname.maxlength=50;

Create a Grade ID column

Datacolumndcgradeid=new DataColumn ("Gradeid", typeof (int));

Add a well-defined column to the class table

DTCLASS.COLUMNS.ADD (Dcclassname);

DTCLASS.COLUMNS.ADD (Dcgradeid);

Create a new row of data

Datarowdrclass=dtclass.newrow ();

drclass["ClassName"]=this.txtclassname.text.trim ();

drclass["Gradeid"]=objgrade.getgradeidbygradename (This.cboGrade.Text.Trim ());

Insert a new row of data into the class table

DTCLASS.ROWS.ADD (Drclass);

Add a class table to a dataset

DSCLASS.TABLES.ADD (Dtclass);

As mentioned above, we learned how to build a dataset and how to populate it with data in a dataset.

2.5 How to get data from a dataset

There are two ways to get data from a dataset:

(1) Obtain data by specifying a row of a specific DataTable in the dataset.

The steps are as follows:

①, gets the specified DataTable from the dataset, through the table name.

②, retrieves the specified DataRow from the DataTable by index.

Gets the data for the specified column from the DataRow, ③, by column name.

To get class information, for example:

Examples are as follows:

Get class Name

dsclass.tables["Class"]. rows[0]["ClassName"];

Get Grade ID

dsclass.tables["Class"]. rows[0]["Gradeid"];

(2) Bind the dataset's data directly to the data control.

2.6 Implementing the data access layer

In the first chapter of the project on the basis of the implementation of the business logic layer to improve and modify.

#region Public Methods

<summary>

Get all student Information

</summary>

<returns> All student information Datasets </returns>

Public DataSet getallstudents ()

{

DataSet ds=new DataSet ();

SqlConnection conn=new SqlConnection (connstring);

SqlDataAdapter Objadapter=new SqlDataAdapter ("Usp_selstudentinfo", conn);

Usp_selstudentinfo to find student stored procedure information

Objadapter.selectcommand.commandtype=commandtype.storedprocedure;

Objadapter.fill (ds, "stutable");

Conn. Close ();

Conn. Dispose ();

return DS;

}

#endregion

To obtain the class information according to the grade number, the reference code is as follows:

Public Datasetgetclassbygradeid (int gradeid)

{

DataSet ds = new DataSet ();

SqlConnection conn = new SqlConnection (connstring);

SqlDataAdapter objadapter = Newsqldataadapter ("Usp_selectclassesbygradeid", conn);

ObjAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;

OBJADAPTER.SELECTCOMMAND.PARAMETERS.ADD ("@GradeID", SqlDbType.Int). Value = Gradeid;

Objadapter.fill (ds, "classtable");

Conn. Close ();

Conn. Dispose ();

return DS;

}

Filter student information by gender and sort by name

Public dataviewgetstudentbysex (String sex)

{//Instantiate DataView object

DataView dvstudent = new DataView ();

Get the Student information table returned from the data layer

dvstudent.table = Studentcontroller.selectallstudent (). tables["Studenttable"];

Filter information by condition

if (sex. Trim () = = "Male")

Dvstudent.rowfilter = "sex= ' man";

if (sex. Trim () = = "female")

Dvstudent.rowfilter = "sex= ' female";

Sort by student name in descending order

Dvstudent.sort = "Studentname DESC";

Returns the filtered view of the data

return dvstudent;

}

Other code see class case.

When developing an application with a three-tier architecture, first edit the interface data presentation according to requirements and then implement the data access layer, business logic layer, presentation layer in the order of the bottom-to-top level.

When implementing the data access layer, in order to make the application we develop easy to maintain, we often encapsulate the data access code of different tables in different classes, and in general, a class corresponds to a table.

Summary

n When implementing a three-tier structure application with ADO, the main function of the dataset is the carrier of data transfer between layers three.

n When implementing a three-tier structure application system with ADO, the main classes used by the data access layer are:

Üsqlconnection class to implement database connections.

Üsqlcommand class, executes the SQL command.

Üsqldatareader class, reading data.

The Üsqldataadapter class, which executes the SQL command, returns a DataSet.

The Üdataset class encapsulates the user request data.

n when executing SQL commands with parameters with ADO, you need to add parameters to the SQL command using the Add method of the parameterized class parameters, including the parameter name, parameter type. Use the ADO section to reuse parameterized objects to re-code the data access layer and the business logic layer.

n The business logic layer realizes data transfer and processing, it first references the data access layer, then instantiates the data Access object, finally invokes the data access layer function, and realizes data processing.

Homework:

Improve the first chapter of their own well-structured mis system, rich and perfect data access layer, business logic layer and presentation layer, requirements:

1. Data transfer using the dataset explained in this chapter;

Use stored procedures to manipulate data.

Implementation of C # 93-tier architecture

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.