Build a simple message with multi-layer architecture

zhaozj2021-02-11  198

Build a simple message with multi-layer architecture

Kaneboy@163.net

ASP.NET can finally let web developers take out from the Noodles code of the ASP, build in a new way

Web site, just like Windows Application, we can also use object-oriented, multi-storey ways

Weaving and build a web application.

The following is a very simple message of this program, which is aimed to reveal the powerful capacity and brand new in ASP.NET.

Development method. If only relative to the message book itself, everyone may doubt so simple to achieve so much strength.

Whether the program is worth it, but I said that the example is just to explain the problem and describe the solution. In fact, I think that from maintenance and

From the perspective of the expansion, even if a simple program, it is also very worthwhile to perform well from the beginning.

The message will be built in a multi-layer approach. The following introduction is in order to facilitate everyone to understand, not followed by the order

Introduction:

First, data entities (CMessageData class)

CMessageData is derived from DataSet, used to maintain message data, in the constructor, call

CreateDataTables () To add a DataTable used to save message data, add itself

DataAble collection. Static attribute TableMapping is used to describe this DataTable DataColumn and numbers

According to the mapping relationship of physical fields in the library, the data access layer will use this property to populate data into CMessageData.

Object.

Public Class CMessageData: DataSet {

Public cMessageData () {

THIS.CREATEDATATABLES ();

}

Public static datatablemapping TableMapping {

Get {

DataTableMapping Result = New DataTableMapping ("t_gbook_postinfo", "messagetable");

Result.columnMappings.add ("ID", "ID");

Result.columnmappings.add ("Last_reply_time", "LastReplyTime");

// ... ..

Return Result;

}

}

Private void createData tables () {

DataTable DT = New DataTable ("MessageTable");

Dt.columns.add ("ID", TypeOf (int32));

Dt.columns.add ("LastReplyTime", TypeOf (DateTime));

// ... ..

Dt.columns ["id"]. autoincrement = true;

Dt.columns ["ID"]. AutoIncremeTseed = 0;

Dt.columns ["ID"]. AutoIncremenstep = -1;

Dt.primaryKey = new Datacolumn [] {dt.columns ["id"]};

THIS.TABLES.ADD (DT);

}

The AddedNewrow property returns a newly added data sheet, empty DataRow for filling the logical layer.

FillDataFormDatabase () two overloaded calls the data access layer to fill a new

CMessageData objects and returns. UpdateToDatabase is used to talk to your own data change update back to the database.

Public DataRow AddedNewrow () ... Public Static CMessageData FillDataFromDatabase (INT32 StartRecord, INT32 MaxRecord) ...

Public Static CMessageData FillDataFromDatabase (INT32 ID) ...

Public void updatetodatabase () ...

Second, data access layer (CDataAccess class)

Responsible for connecting the database, perform SIUD (SIUD, INSERT, UPDATE, DELETE). Data connection information is placed

APPAAMETERS.XML files.

Two overloads of FillMessageData () create new CMessageData objects, populate data, then return

Back:

Public Static CMessageData FillMessageData (INT32 StartRecord, INT32 MAXRECORD)

Public Static CMessageData FillMessageData (INT32 ID)

UpdateMessageData () updates the changes made by the CMessageData object in the parameter

Library:

Public Static Int32 UpdateMessageData (CMessageData MessageData) {

OLEDBCONNECTION CONN = New OLEDBConnection (CAPPParameters.OleDbconnectionsTring);

OleDbCommand cmdSelect = new OleDbCommand ( "Select username, last_reply_time, guest_name, guest_email, guest_website_name, guest_website_url, guest_oicq, guest_ip, guest_post_time, guest_text, reply_data From t_gbook_postinfo", conn);

OLEDBCommand cmdinsert = new oledbcommand ();

CmdInsert.connection = conn;

cmdInsert.CommandText = "Insert Into t_gbook_postinfo (last_reply_time, guest_name, guest_email, guest_website_name, guest_website_url, guest_oicq, guest_ip, guest_post_time, guest_text, reply_data) Values ​​(@ last_reply_time, @ guest_name, @ guest_email, @ guest_website_name, @ guest_website_url, @ guest_oicq, @ Guest_ip, @ Guest_POST_TIME, @ guest_text, @ reply_data);

CmdInsert.Parameters.add ("@ last_reply_time", oledbtype.dbdate, 0, "las_reply_time");

CmdInsert.Parameters.Add ("@ Guest_name", OLEDBTYPE.VARWCHAR, 255, "Guest_name");

// ...

OLEDBCommand cmdupdate = new oledbcommand ();

cmdUpdate.Connection = conn; cmdUpdate.CommandText = "Update t_gbook_postinfo Set last_reply_time = @ last_reply_time, guest_name = @ guest_name, guest_email = @ guest_email, guest_website_name = @ guest_website_name, guest_website_url = @ guest_website_url, guest_oicq = @ guest_oicq, guest_ip = @ guest_ip, guest_post_time = @ guest_post_time, guest_text = @ guest_text, reply_data = @ reply_data where (id = @ Original_ID) ";

cmdupdate.Parameters.Add ("@ last_reply_time", oledbtype.dbdate, 0, "las_reply_time");

cmdupdate.parameters.add ("@ Guest_name", OLEDBTYPE.VARWCHAR, 255, "Guest_name");

// ...

OLEDBCommand cmddelete = new oledbcommand ();

CMDDelete.Connection = conn;

Cmddelete.commandtext = @ "delete from t_gbook_postinfo where (id = @original_id)";

CMDDelete.Parameters.Add ("@ Original_ID", OLEDBTYPE.INTEGER, 0, "ID"). SourceVersion = DataRowVersion.original

OLEDBDataAdapter Ada = New OLEDBDataAdapter (cmdselect);

Ada.insertcommand = cmdinsert;

Ada.UpdateCommand = cmdupdate;

Ada.deleteCommand = cmddelete;

Ada.tableMappings.add (cMessageData.tablemapping);

Return Ada.Update (MessageData, "T_Gbook_PostInfo");

}

The advantage of separateing the data access layer separately is that the other layers will not be transferred directly with the database, if

We replace the database from Access to SQLServer only need to replace the current CDataAccess class

I.e. In the source code, there is a CODBCDataAccess that uses ODBC.NET.

Replace CDataAccess does not have any impact on other parts of the program, we can use

ODBC.NET access capabilities, change the database to Oracle, FORPRO, etc.

Third, logical layer

The logical layer of this message is very simple, consisting of three classes, cMessage is used to describe a message,

CREPLY is used to describe a reply, and the CreplyCollection set class is used to describe multiple replies.

CMessage provides an overloaded constructor:

Public CMessage (DataRow Row)

We can initially generate a CMessage object with a DataRow data in CMessageData. Public void FillDataRow (DATAROW ROW)

This function fills its own data into the DATAROW object in the parameter. We use similar:

GetMessage (). FillDataRow (MessageData.AddedNewRow ())

Such a code can add a new message content to a cMessageData object, where

GetMessage () is a method that collects users on the page and returns a method of cMessage.

Public CREPLYCOLLECTION Replys

This property is used to publicly reply to itself.

Fourth, interface layer - user control

In order to facilitate us to bind a message on a cMessage object and page,

A CREPLY object is binding with a reply displayed on the page, we have two

UserControl. The MessageBlock control is used to display a message, it passes the property:

Public CMessage Message

Came object public CMessage interface, we only need to assign a CMessage object to this property,

This control can be made to display the contents of the message represented by the CMessage object.

The ReplyBlock control is used to display a reply, equally attribute:

Public CREPLY Reply

Open a CREPLY type interface.

In the MessageBlock control, we contain it in accordance with the corresponding CMessage object's replys properties

Reply, dynamically load the ReplyBlock control through the loadControl () method, and place it in one

PlaceHolder type web control.

V. Interface layer - page

Now the display of the page is very simple, we create one in the main page (Default.aspx.cs)

CMessageData object, fill data, use loadControl () method to load MessageBlock control

There is a message to show a message.

CMessageData MessageData = CMESSAGEDATA.FILLDATAFROMDATABASE ((iPage - 1) * ipageSize, ipageSize;

For (int32 i = 0; i

MessageBlock Msg = (MessageBlock) loadControl ("MessageBlock.ascx);

Msg.Message = new cMessage (MessageTa.Tables ["MessageTable"]. Rows [i]);

HLDMESSAGE.CONTROLS.ADD (MSG);

}

Taking a fully equipped message measure, we have the lack of deletions, management functions, and management functions.

However, as long as the infrastructure is coming, the improvement and extension functions are very simple.

The above message This showed a foundation architecture, and the architecture of the real large program may be complicated.

many. For example, a data entity class we can put field information and mapping information in an XML file, then

We only need to create a universal data entity class, which can be described by loading different XML files.

The same data entity; in the case of complex data, it is also a challenge to maintain the relationship between the various data.

Source code can be downloaded at http://www.86soft.com/clsoft/kaneboy/gBook.zip, if

You want to use, remember to modify the AppParameters.xml file, at least the path to the Access file of the connection string is to change (you can simply use mappath () in global.asax to be dynamically obtained

This path :), the value of the _sapparameterfilename string in the cappparameters.cs file

Also make changes according to the actual situation (same you can use mappath () :), then build a one in IIS

The virtual directory named GBook beared to host this project.

转载请注明原文地址:https://www.9cbs.com/read-3705.html

New Post(0)