web| Data | system
Introduction
In this article I'll introduce two data-binding patterns in three-tier web system development, and then, without exceeding the knowledge of the controls you already have, introduce alternatives to--xlib library files that can drastically reduce this data-binding pattern. Specifically, this article begins with a general approach to data binding in a three-tier architecture, and then describes how Xlib improves this binding efficiency.
1. Data binding Process
In a three-tier web architecture, there are usually four steps to complete the data-binding task:
1 Download data from the database to the business logic object
2 Place Web controls on a Web form and populate the data with business logic objects.
3 Copy the value of the Web control into the properties of the business logic object
4 Save the property value of the business logic object to the database.
For a specific customer, for example, the simplest data-binding pattern in a three-tier application is as follows:
1 Download the appropriate customer record from the database Customer table
2 bind the customer record to the customer business object
3 Bind the Customer business object to the Web control
4 Users 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 a number of ways to perform this process, and I can summarize three:
1, show the way to generate data binding-using the front desk you are familiar with
2, Microsoft's approach--using typed datasets and FormView
3, Xlib Method--using reflection technology and other. NET features to analyze bindings--getting objects at run time
1.2 Code--Business logic objects and Web pages
To illustrate how these three methods are used, I'll use the Customer class and Editcustomer page as a demo. Here's some code that shows exactly where to make data binding.
Typically, 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 the database record
}
public void Delete ();
Other methods specific to customer
public string Getcustomerfullname ().
}
The code for the Edit Customer page resembles the following:
Some instructions for the page
Some customer properties related to form forms
Submit and Cancel buttons
The background code for editing user information resembles 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 the control values
}
private void SaveData ()
{
Customer Customer=new customer ();
If (_isupdatemode)
{
Customer.id=_customerid;
Customer. Load ();
}
Binding #3
Copy control values into customer properties
Customer. Save ();
}
}
23 Methods of data binding
2.1 Method 1: Explicitly declaring the data-binding method
One way to edit customer information is by explicit data binding, which means that for each data binding you need to perform the preceding four steps, for example, for the customer's Load method, the possible code looks like this:
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 properties, you need to write more code to complete the data binding functionality. If you want to add a new attribute for customer, you have to make changes in 6 places:
1) database
2 Data-bound 1--data Business logic Object
3 Data binding 2--business logic objects on a Web control
4 Add a new control on a Web Form
5 The data-bound 3--web control is bound to the business logic
6 Data binding 4--business logic to the database
As you can see, the disadvantages of the above method-repetitive work and maintenance difficulties
2.2 The way to use Microsoft-typed datasets and FormView
For this method, Microsoft has provided us with a lot of examples, if your program is simple enough, then you can generate a typed dataset from the Customer table and bind it to the FormView. by FormView to add and edit the customer objects, you can find out how to use them in the following two places:
Creating DAL using Typed datasets
modifying Data using FormView Web control
For binding between the database and business objects, you can use the Typed DataSet Wizard to complete the For bindings between busiess and Web controls, you can use the Inseritemtemplate and EditItemTemplate templates of the FormView control and make binding rules, similar to the following code:
<asp:textbox id= "Txtlastname" text= ' runat= "Server"/>
You may have noticed that in the example provided by Microsoft, it worked really well for a simple application, but for a slightly more complex application, you need to constantly expand your code.
This way you can maintain simple data, for example, if you need to add a new attribute to the customer, you just need to change three places:
1. Database
2, Web form-edititemtemplate
3, Web Form-insertitemtemplate
Binding of 2.3 Xlib mode
Xlib also provides the flexibility to maintain data in addition to the two binding methods described earlier. Xlib uses reflection technology to automatically map from business logic objects to databases to Web controls.
In the execution of the database to the business logic object, it uses the Xbusinessobjectbinder object, and the following code fragment styles the code for 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-except 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 the binding of the business logic to the Web control, it provides the Xwebcontrolsbinder control, and the following code fragment shows the code for the customer's edit 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=_customerid;
Customer. Load ();
}
Binding #3
Copy control values into customer properties
Xwebcontrolsbinder.toobject (this, customer);
Customer. Save ();
}
}
As you can see, this approach removes both the drawbacks of the first method and the second method.