引言
本文我將介紹在三層web體系開發中的兩種數據綁定模式,然后在不超過你已經會用的控件知識的情況下,來介紹能夠極大減少這種數據綁定模式的替代品--xlib庫文件。具體的說,本文開始我們介紹在三層體系結構里常規的數據綁定方法,然后介紹xlib是如何提高這種綁定效率的。
1、 數據綁定流程
在三層web體系結構里,通常有四步來完成數據綁定任務:
1)從數據庫里加載數據到業務邏輯對象
2)在web窗體上放置web控件并使用業務邏輯對象進行填充數據。
3)將web控件的值拷貝到業務邏輯對象的屬性里
4)保存業務邏輯對象的屬性值到數據庫。
以具體的customer為例,在三層應用程序里最簡單的數據綁定模式的步驟如下:
1)從數據庫customer表里加載合適的顧客記錄
2)將顧客記錄綁定到customer業務對象上
3)將customer業務對象綁定到web控件上
4)用戶在窗體里輸入數據并單擊submit進行提交數據
5)將web控件的更新事件綁定到customer對象上
6)把customer上的信息保存到表里
7)將表里的信息保存到customer上
有多種方式執行這個流程,我概括起來有三種:
1、顯示生成數據綁定方式--使用大家都熟悉的前臺方式
2、microsoft的方式--使用類型化的dataset和formview
3、xlib方式--使用反射技術和其他的.net特性來分析綁定--在運行時獲取對象
1.2 代碼--業務邏輯對象和web 頁面
為了具體說明這三種方式的使用方法,我將使用customer類和editcustomer頁面作為演示。下面是一些代碼,它將說明具體在什么地方進行數據綁定。
通常,customer類看起來類似如下:
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()..
}
編輯顧客頁面的代碼類似如下:
//頁面的一些指令
//與form窗體有關的一些顧客屬性
//提交和取消按鈕
編輯用戶信息的后臺代碼類似如下:
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=_customerid;
customer.load();
}
//binding #3
//copy control values into customer properties
customer.save();
}
}
2 三種數據綁定方式
2.1 方法1:顯式聲明數據綁定方式
編輯顧客信息的方法之一是顯式的數據綁定方式,這意味這對于每一個數據綁定都需要執行前面說的四個步驟,例如對于customer的load方法,可能的代碼類似如下:
public void load()
{
…
//load customer record using data reader
_firstname=(string)datareader["firstname"];
_lastname=(string)datareader["lastname"];
…
}
在這種情況下,當customer對象有更多屬性時,您就需要編寫更多的代碼來完成數據綁定功能。如果您想為customer新增加一個屬性,你不得不在6個地方進行更改:
1)數據庫
2)數據綁定1--數據業務邏輯對象
3)數據綁定2--業務邏輯對象在綁定到web控件上
4)在web窗體上添加新的控件
5)數據綁定3--web控件綁定到業務邏輯上
6)數據綁定4--業務邏輯到數據庫上
正如您所看到的上面方法的缺點--重復工作大且維護困難
2.2 使用微軟的方式--類型化的dataset和formview
對于這個方法,微軟已經為我們提供了很多例子了,,如果您的程序足夠簡單,那么您就可以從customer表里生成一個類型化的dataset,并將其綁定到formview上,由formview來執行添加和編輯customer對象的功能,您可以在下面兩個地方發現如何使用他們:
creating dal using typed datasets
modifying data using formview web control
對于 database和business對象之間的綁定,您可以使用類型化的dataset向導完成,對于busiess和web控件之間的綁定您可以使用formview控件的inseritemtemplate和edititemtemplate 模板完成,并制定綁定規則,類似代碼如下:
<asp:textbox id="txtlastname" text='' runat="server" />
您可能已經注意到了在微軟提供的例子里使用這種方式對簡單應用程序來說,工作的確實相當的好,但是對于稍微復雜的應用程序來說,您就需要不斷擴展自己的代碼。
這種方式可以簡單數據的維護,例如你需要為customer增加一個新的屬性,你就只需要更改三處就可以了:
1、數據庫
2、web form - edititemtemplate
3、web form - insertitemtemplate
2.3 xlib方式的綁定
xlib在同時能夠提供前面介紹的兩種綁定方式外,還增加了數據維護方面的靈活性。xlib使用反射技術來自動從業務邏輯對象到數據庫,到web控件之間的映射。
在執行數據庫到業務邏輯對象方面,它使用了xbusinessobjectbinder對象,下面的代碼片斷樣式了customer對象的代碼:
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();
}
}
對于業務邏輯到web控件的綁定,它提供了xwebcontrolsbinder 控件,下面代碼片斷顯示了顧客編輯頁面的代碼:
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();
}
}
正如您所看到的,這種方法既去掉了第一種方法的缺點,又具有第二中方法的有點。菜鳥學堂: