Use a layer-4 structure to develop an information management system

Source: Internet
Author: User

In general, layered application constructor has the following advantages: it facilitates code reuse, facilitates program changes, improves code robustness, and facilitates division of labor.
The following uses a simple database application to obtain user information as an example to describe how to construct a layer-4 structure information management application:
1. Database Layer
// In comparison, the stored procedure with parameters is much more efficient than the SQL statement constructed by adding strings and variables in the code,
// Therefore, we recommend that you write common queries as stored procedures for calling.
Create table dbo. users (
FID int not null,
FName nvarchar (50) not null,
FPassword nvarchar (50) NULL,
FRemark ntext NULL
)

Create procedure dbo. sp_sel_user
@ Pid int
AS
Select * from users where fid = @ pid

2. Data Component layer
// Return an application object in a certain format based on the database object information such as the SQL statement, stored procedure name, and parameters provided by the caller.
// The number of data components is relatively small, but it is widely used. Generally, comprehensive transactions and exception handling are required,
// The space limit is omitted.
Public class CDatabase
{
Private string mConnectString = "Provider = SQLOLEDB.1; Data Source = mymachine; Initial Catalog = mydatabase; User ID = sa; Password = pwd ;";
Public bool RunProcedure (string sProcedureName, System. Data. OleDb. OleDbParameter [] oParams, out OleDbDataReader oReader)
{
OleDbConnection oConnection = new OleDbConnection (this. mConnectString );
OleDbCommand oCommand = new OleDbCommand (sProcedureName, oConnection );
OCommand. CommandType = CommandType. StoredProcedure;

If (oParams! = Null)
{
Foreach (OleDbParameter oParam in oParams)
OCommand. Parameters. Add (oParam );
}
OReader = oCommand. ExecuteReader (System. Data. CommandBehavior. CloseConnection );
Return true;
}

Public string ConnectString
{
Get
{
Return this. mConnectString;
}
Set
{
This. mConnectString = value;
}
}
}
3. Business logic layer
// Obtain all user information based on the user ID provided by the caller.
// The main task of the business layer is to construct the Business Object and call the data component to obtain data from the database,
// This is the most complicated implementation layer and requires familiarity with the database structure,
// Design the Business Objects and database objects based on the needs of the user layer.
Public class CUser
{
Private int mID;
Private string mName;
Private string mPassword;
Private string mRemark;
Public CUser (int pID)
{
CDatabase cdb = new CDatabase ();
Oledbparameter oparam = new oledbparameter ();
Oparam. parametername = "@ PID ";
Oparam. oledbtype = oledbtype. Integer;
Oparam. value = PID;
Oledbparameter [] oparams = {oparam };
Oledbdatareader oreader;
CDB. runproc ("sp_sel_user", oparams, out oreader );
This. Mid = PID;
This. mname = oreader ["fname"];
This. mpassword = oreader ["fpassword"];
This. mremark = oreader ["fremark"];
}
Public int ID
{
Get
{
Return mid;
}
Set
{
Mid = value;
}
}
Public string Name
{
Get
{
Return mName;
}
Set
{
MName = value;
}
}
Public string Password
{
Get
{
Return mPassword;
}
Set
{
MPassword = value;
}
}
Public string Remark
{
Get
{
Return mRemark;
}
Set
{
MRemark = value;
}
}
}
4. User Interface Layer
// Obtain detailed user information based on the entered user ID and write it to the TextBox element on the page.
// You do not need to deal with the database at the page layer. You only need to call the corresponding service components,
// The interface can be easily changed.
Private void button#click (object sender, System. EventArgs e)
{
CUser user = new CUser(this.txt ID. Text );
This.txt Name. Text = user. Name;
This.txt Password. Text = user. Password;
This.txt Remark. Text = user. Remark;
}


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.