ASP.NET Web開發架構之二 資料輸入表單

來源:互聯網
上載者:User

Web架構要達到快速開發,又便於維護,進行了一系列的努力。

請看最初始的ASP.NET頁面,對資料進行操作的代碼,頁面的基本代碼如下所示

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中載入資料,並綁定到控制項中去。在儲存按鈕事件中,把使用者修改過的值,再寫回到資料庫中。這樣的代碼,在項目中要重複很多次,資料項目越多,所需要的代碼量越大。有沒有一種辦法,可以實現自動綁定資料到控制項中,在儲存中,又自動將資料寫回到資料庫中去呢? Enterprise Solution以下面的方法來實現。

Enterprise Solution對於要輸入資料並儲存到資料庫中的這一類操作,統一提供相同的介面,對資料快速控制項目。以記事本為例子,它的最終效果是這樣的

工具列按鈕由架構自動載入,當你的類型繼承自EntryPageBase時,它就會載入工具列,用於操作資料。

[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 來識別介面的實體物件,自動實現讀寫操作。頁面中的載入,儲存,刪除按鈕事件的代碼如下,可以看到,它們都是在重寫基類的方法

       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);        }    

如你所看到的,這就是所有的代碼,關於資料載入,儲存,刪除的代碼,沒有資料繫結,也沒有資料回寫到資料庫中的代碼。最後,來看一下,ASPX頁面,是如何達到這個目的的

<ext:NumberBox ID="TextBox3" AutoFind="true"  runat="server" Label="Title" DataBindingString="BlotterEntity:Id"></ext:NumberBox>

每一個需要綁定資料的ExtAspNet控制項,附帶一個DataBindingString屬性,指出綁定到對象的屬性名稱。這個數字輸入框是綁定到記事本的Id屬性,在設計時,你可以這樣指定它

 

Web架構提供了快速的資料屬性綁定支援,請先在設定檔中指定需要反射的程式集完整路徑。

<appSettings>

   <add key="Assembly" value="E:\Solution\Enterprise Solution\Build\Benin.BusinessLogic.dll"/>

</appSettings>

DataBindingString的編輯器,反射此程式集,把它的屬性顯示在ListView中,用於綁定。

應用此模型,明顯的減少了代碼量。比如,有100個控制項,就要寫100行讀取值併到綁定到介面中的代碼,在儲存時,再寫100行代碼,把值回寫到資料庫中。而此開發方法,資料的綁定是自動的,您只需要指定必要的屬性,架構會為你做好其它的事情。再來看看,要實現此方法,背後要做出的努力

1  需要指定要反射的類型,TransactionType = "BlotterEntity"; 這一句的作用相當關鍵。

2  將反射的值,綁定到控制項。依據反射,賦值的代碼,如下所示

ReflectionHelper.SetPropertyValue(textbox, targetProperty, obj);

這一句就是用來給值的,把從資料庫中取到值,轉化為可用的類型,賦給textbox的Text屬性,完成資料繫結。

3  回寫值到資料庫中。依然是反射,把值取到,賦給Entity

object obj = ReflectionHelper.GetPropertyValue(textbox, targetProperty);object converted = Convert.ChangeType(obj, type);ReflectionHelper.SetPropertyValue(entity, arr[1], converted);

如代碼所示,取到值,回寫到實體類的屬性中。起關鍵作用的,還是DataBindingString字串。

 

再來看看,主從表資料的讀寫,這比上面的單表讀寫,要複雜一些。


銷售單由表頭,參考編號和明細多行物料編號組成。表頭的讀寫,可以用上面的方法,在明細的資料讀取上,重寫方法

protected override void InitNavigator(EntityBase2 entity){       SalesOrderEntity user = (SalesOrderEntity)entity;       Grid1.DataSource = user.SalesOrderDetails;       Grid1.DataBind();}

InitNavigator用於擷取當前實體,綁定值到明細列表中。Insert按鈕的的實現原理如下,它把表頭的主索引值,帶到明細頁面中去,用HiddenField藏在頁面中。這樣,在儲存明細時,以此值作為主鍵儲存。當返回表頭時,重新整理主表,重新擷取值,則可以顯示明細表增加的值。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.