Two data binding modes in the three-tier Web architecture: Clear Moon)

Source: Internet
Author: User
In this article, I will introduce two data binding modes in three-tier Web system development, and then, without exceeding the control knowledge you will already use, to introduce the xlib library file, which can greatly reduce the replacement of this data binding mode. Specifically, this article introduces the conventional data binding methods in the three-tier architecture, and then introduces how xlib improves the binding efficiency.

1. Data Binding Process

In the three-tier Web architecture, there are usually four steps to complete the data binding task:

1) load data from the database to the business logic object

2) place Web controls on a web form and fill data with business logic objects.

3) copy the web control value to the attributes of the business logic object.

4) Save the property value of the business logic object to the database.

Take a specific customer as an example.ProgramThe simplest data binding mode is as follows:

1) Load appropriate customer records from the database customer table

2) bind the customer record to the customer's Business Object

3) bind the customer business object to the Web Control

4) You enter data in the form and click Submit to submit the data.

5) bind the update event of the web control to the customer object.

6) Save the information on the customer to the table.

7) Save The information in the table to the customer

There are multiple ways to execute this process. I can summarize three methods:

1. display the data binding method generated-use the frontend method that everyone is familiar

2. Microsoft-use a typed dataset and formview

3. xlib-use reflection technology and other. NET features to analyze bindings-Get objects at runtime

1.2Code-- Business logic object and web page

To describe how to use these three methods, I will use the customer class and the editcustomer page for demonstration. The following code describes where to bind data.

Generally, the customer class looks similar to the following:

Public Class Customer
{
// List all properties
// Crud Methods
Public void load ()
{
// Binding #1
// Copy database record values into properties
}
Public void save ()
{
// Binding #4
// Copy properties values into database record
}

Public void Delete ();
// Other methods specific to customer
Public String getcustomerfullname ()..
}

The code for editing the customer page is similar to the following:

// Some instructions on the page
// Some customer attributes related to form forms
// Click the submit and cancel buttons.

The background code for editing user information is similar to the following:

Public partial class editcustomer
{
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback ){
// Check if adding new customer or updating
If (_ isupdatemode)
Loaddata ();
}
}

Protected void btnsubmit_click (Object sender, eventargs E)
{
If (! Page. isvalid)
Return;
Savedata ();

// Go back
}

Private void loaddata ()
{
Customer customer = new customer ();
Customerid = _ customerid;
Customer. Load ();

// Binding #2
// Copy customer properties into control values
}

Private void savedata ()
{
Customer customer = new customer ();
If (_ isupdatemode)
{
Customer. ID = _ mermerid;
Customer. Load ();
}

// Binding #3
// Copy control values into customer Properties
Customer. Save ();
}
}

 

2. Three Data Binding Methods

2.1 Method 1: explicitly declare the data binding method

One of the ways to edit customer information is to explicitly bind data, which means that the four steps mentioned above are required for each data binding. For example, for the load method of customer, the possible code is as follows:

Public void load ()
{
...
// Load customer record using data reader
_ Firstname = (string) datareader ["firstname"];
_ Lastname = (string) datareader ["lastname"];
...
}

In this case, when the customer object has more attributes, you need to write more code to complete the data binding function. If you want to add a new property for the customer, you have to make changes in six places:

1) Database

2) Data Binding 1 -- data business logic object

3) Data Binding 2 -- the business logic object is bound to the web control.

4) Add a new control on the web form

5) Data Binding 3 -- binding Web controls to business logic

6) Data Binding 4-business logic to the database

As you can see, the disadvantage of the above method-repetitive work and difficult maintenance

2.2 Use Microsoft's method-typed dataset and formview

Microsoft has provided many examples for this method. If your program is simple enough, you can generate a typed dataset from the customer table, and bind it to formview. The formview is used to add and edit customer objects. You can see how to use them in the following two places:

Creating Dal using typed Datasets
Modifying data using formview Web Control

For binding between database and Business Objects, you can use the typed dataset wizard. for binding between busiess and Web controls, you can use the inseritemtemplate and edititemtemplate templates of the formview control, and formulate binding rules, similar code:

<Asp: textbox id = "txtlastname" text = ''runat =" server "/>

You may have noticed that using this method in the example provided by Microsoft is really good for simple applications, but for a slightly complex application, you need to constantly expand your code.

This method can be used for simple data maintenance. For example, if you need to add a new attribute to the customer, you only need to change it in three places:

1. Database

2. Web form-edititemtemplate

3. Web form-insertitemtemplate

2.3 xlib binding

In addition to the two binding methods described earlier, xlib also adds flexibility in data maintenance. Xlib uses reflection technology to automatically map business logic objects to databases and web controls.

In terms of executing the database to the business logic object, it uses the xbusinessobjectbinder object. The following code snippet shows the code of the customer object:

Public Class Customer
{
...
Public void load ()
{
Datareader = new xdatareader ();

// Load data using auto-generated Query into xdatareader
// Xdatareader works just like data reader-since t it automatically
// Converts database values types into inulllable C # types

// Binding #1
Xbusinessobjectbinder. fromdatareader (this, datareader );
}

Public void save ()
{
Xdatawriter datawriter = new xdatawriter ();
// Xdatawriter automatically generates insert/update/delete SQL s
// Statements

// Binding #4
Xbusinessobjectbinder. todatawriter (this, datawriter)

Datawriter. Update ();
}
}

For binding business logic to Web controls, it provides the xwebcontrolsbinder control. The following code snippet shows the code on the customer editing page:

Public partial class editcustomer
{
Protected void page_load (Object sender, eventargs E)
{...}

Protected void btnsubmit_click (Object sender, eventargs E)
{...}

Private void loaddata ()
{
Customer customer = new customer ();
Customerid = _ customerid;
Customer. Load ();

// Binding #2
Xwebcontrolsbinder. fromobject (this, customer );
}

Private void savedata ()
{
Customer customer = new customer ();
If (_ isupdatemode)
{
Customer. ID = _ mermerid;
Customer. Load ();
}

// Binding #3
// Copy control values into customer Properties
Xwebcontrolsbinder. toobject (this, customer );

Customer. Save ();
}
}

As you can see, this method not only removes the disadvantages of the first method, but also has the disadvantages of the second method.

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.