ASP.NET Web開發(fā)框架之二 數(shù)據(jù)輸入窗體
Web框架要達到快速開發(fā),又便于維護,進行了一系列的努力。
請看最初始的ASP.NET頁面,對數(shù)據(jù)進行操作的代碼,頁面的基本代碼如下所示
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- LoadData();
- }
- }
- private void LoadData()
- {
- UserEntity current=GetUser();
- tbxName.Text = current.Name;
- tbxRemark.Text = current.Remark;
- }
- protected void btnSave_Click(object sender, EventArgs e)
- {
- int id = GetQueryIntValue("id");
- IXRoleManager menuManager = ClientProxyFactory.CreateProxyInstance<IXRoleManager>();
- XRoleEntity item = menuManager.GetXRole(id);
- item.Name = tbxName.Text.Trim();
- item.Remark = tbxRemark.Text.Trim();
- menuManager.SaveXRole(item);
- ExtAspNet.Alert.Show("Save successfully");
- }
在Page_Load中加載數(shù)據(jù),并綁定到控件中去。在保存按鈕事件中,把用戶修改過的值,再寫回到數(shù)據(jù)庫中。這樣的代碼,在項目中要重復(fù)很多次,數(shù)據(jù)項越多,所需要的代碼量越大。有沒有一種辦法,可以實現(xiàn)自動綁定數(shù)據(jù)到控件中,在保存中,又自動將數(shù)據(jù)寫回到數(shù)據(jù)庫中去呢? Enterprise Solution以下面的方法來實現(xiàn)。
Enterprise Solution對于要輸入數(shù)據(jù)并保存到數(shù)據(jù)庫中的這一類操作,統(tǒng)一提供相同的界面,對數(shù)據(jù)快速操作。以記事本為例子,它的最終效果是這樣的
工具欄按鈕由框架自動加載,當你的類型繼承自EntryPageBase時,它就會加載工具欄,用于操作數(shù)據(jù)。
- [Function("AIITRL", "~/module/note.aspx")]
- public partial class note : EntryPageBase
- {
- protected override void PageLoadEvent(object sender, EventArgs e)
- {
- if (!IsPostBack)
- TransactionType = "BlotterEntity";
- base.PageLoadEvent(sender, e);
- }
- }
重寫基類的PageLoadEvent方法,傳入TransactionType ,框架以TransactionType 來識別界面的實體對象,自動實現(xiàn)讀寫操作。頁面中的加載,保存,刪除按鈕事件的代碼如下,可以看到,它們都是在重寫基類的方法
- public override EntityBase2 LoadEntity(string customerNo)
- {
- IBlotterManager manager = ClientProxyFactory.CreateProxyInstance<IBlotterManager>();
- BlotterEntity customer = manager.GetBlotter(Convert.ToInt16(customerNo));
- return customer;
- }
- public override void DeleteEntity(EntityBase2 entity)
- {
- BlotterEntity user = (BlotterEntity)entity;
- IBlotterManager manager = ClientProxyFactory.CreateProxyInstance<IBlotterManager>();
- manager.DeleteBlotter(user);
- }
- public override void SaveEntity(EntityBase2 entity)
- {
- BlotterEntity user = (BlotterEntity)entity;
- IBlotterManager manager = ClientProxyFactory.CreateProxyInstance<IBlotterManager>();
- manager.SaveBlotter(user);
- }
如你所看到的,這就是所有的代碼,關(guān)于數(shù)據(jù)加載,保存,刪除的代碼,沒有數(shù)據(jù)綁定,也沒有數(shù)據(jù)回寫到數(shù)據(jù)庫中的代碼。***,來看一下,ASPX頁面,是如何達到這個目的的
- <ext:NumberBox ID="TextBox3" AutoFind="true" runat="server" Label="Title" DataBindingString="BlotterEntity:Id"></ext:NumberBox>
每一個需要綁定數(shù)據(jù)的ExtAspNet控件,附帶一個DataBindingString屬性,指出綁定到對象的屬性名。這個數(shù)字輸入框是綁定到記事本的Id屬性,在設(shè)計時,你可以這樣指定它
Web框架提供了快速的數(shù)據(jù)屬性綁定支持,請先在配置文件中指定需要反射的程序集完整路徑。
- <appSettings>
- <add key="Assembly" value="E:\Solution\Enterprise Solution\Build\Benin.BusinessLogic.dll"/>
- </appSettings>
DataBindingString的編輯器,反射此程序集,把它的屬性顯示在ListView中,用于綁定。
應(yīng)用此模型,明顯的減少了代碼量。比如,有100個控件,就要寫100行讀取值并到綁定到界面中的代碼,在保存時,再寫100行代碼,把值回寫到數(shù)據(jù)庫中。而此開發(fā)方法,數(shù)據(jù)的綁定是自動的,您只需要指定必要的屬性,框架會為你做好其它的事情。再來看看,要實現(xiàn)此方法,背后要做出的努力
1 需要指定要反射的類型,TransactionType = "BlotterEntity"; 這一句的作用相當關(guān)鍵。
2 將反射的值,綁定到控件。依據(jù)反射,賦值的代碼,如下所示
- ReflectionHelper.SetPropertyValue(textbox, targetProperty, obj);
這一句就是用來給值的,把從數(shù)據(jù)庫中取到值,轉(zhuǎn)化為可用的類型,賦給textbox的Text屬性,完成數(shù)據(jù)綁定。
3 回寫值到數(shù)據(jù)庫中。依然是反射,把值取到,賦給Entity
- object obj = ReflectionHelper.GetPropertyValue(textbox, targetProperty);
- object converted = Convert.ChangeType(obj, type);
- ReflectionHelper.SetPropertyValue(entity, arr[1], converted);
如代碼所示,取到值,回寫到實體類的屬性中。起關(guān)鍵作用的,還是DataBindingString字符串。
再來看看,主從表數(shù)據(jù)的讀寫,這比上面的單表讀寫,要復(fù)雜一些。
銷售單由表頭,參考編號和明細多行物料編號組成。表頭的讀寫,可以用上面的方法,在明細的數(shù)據(jù)讀取上,重寫方法
- protected override void InitNavigator(EntityBase2 entity)
- {
- SalesOrderEntity user = (SalesOrderEntity)entity;
- Grid1.DataSource = user.SalesOrderDetails;
- Grid1.DataBind();
- }
InitNavigator用于獲取當前實體,綁定值到明細列表中。Insert按鈕的的實現(xiàn)原理如下,它把表頭的主鍵值,帶到明細頁面中去,用HiddenField藏在頁面中。這樣,在保存明細時,以此值作為主鍵保存。當返回表頭時,刷新主表,重新獲取值,則可以顯示明細表增加的值。
原文鏈接:http://www.cnblogs.com/JamesLi2015/archive/2012/09/19/2693130.html


2009-07-29 17:16:47
2009-07-22 13:24:24




