成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

一句代碼實現批量數據綁定 下

數據庫
《上篇》主要介紹如何通過DataBinder實現批量的數據綁定,以及如何解決常見的數據綁定問題,比如數據的格式化。接下來,我們主要來談談DataBinder的設計,看看它是如何做到將作為數據源實體的屬性值綁定到界面對應的控件上的。

《上篇》主要介紹如何通過DataBinder實現批量的數據綁定,以及如何解決常見的數據綁定問題,比如數據的格式化。接下來,我們主要來談談DataBinder的設計,看看它是如何做到將作為數據源實體的屬性值綁定到界面對應的控件上的。此外,需要特別說明一點:《上篇》中提供了DataBinder最初版本的下載,但已經和本篇文章介紹的已經大不一樣了。***版本的主要解決兩個主要問題:通過Expression Tree的方式進行屬性操作(屬性賦值和取值),添加了“數據捕捉”(Data Capture)的功能,以實現將控件中的值賦給指定的實體。但是,這并不意味著這就是一個最終版本,這里面依然有一些問題,比如對空值的處理不不夠全面,比如在進行數據綁定的時候,有的控件類型需要進行HTML Encoding,等等。

目錄:

  1. 通過DataPropertyAttribute特性過濾實體的“數據屬性”
  2. Control/DataSource映射的表示:BindingMapping
  3. 如何建立Control/DataSource映射集合
  4. 通過映射集合實現數據綁定
  5. 通過映射集合實現數據捕捉

#p#

一、通過DataPropertyAttribute特性過濾實體的數據屬性

DataBinder在進行數據綁定的時候,并沒有對作為數據源的對象作任何限制,也就是說任何類型的對象均可作為數據綁定的數據源。控件(這里指TextBox、Label等這樣綁定標量數值的控件)綁定值來源于數據源實體的某個屬性。但是一個類型的屬性可能有很多,我們需要某種篩選機制將我們需要的“數據屬性”提取出來。這里我們是通過在屬性上應用DataPropertyAttribute一個特性來實現的。

簡單起見,我不曾為DataPropertyAttribute定義任何屬性成員。DataPropertyAttribute中定義了一個靜態的GetDataProperties方法,得到給定實體類型的所有數據屬性的名稱。但是為了避免頻繁地對相同實體類型進行反射,該方法對得到的屬性名稱數組進行了緩存。

  1. [AttributeUsage( AttributeTargets.Property, AllowMultiple = false,Inherited = true)]  
  2.  public class DataPropertyAttribute: Attribute  
  3.  {  
  4.      private static Dictionary<Type, string[]> dataProperties = new Dictionary<Type, string[]>();  
  5.      public static string[] GetDataProperties(Type entityType)  
  6.      {  
  7.          Guard.ArgumentNotNullOrEmpty(entityType, "entityType");  
  8.          if (dataProperties.ContainsKey(entityType))  
  9.          {  
  10.              return dataProperties[entityType];  
  11.          }  
  12.          lock (typeof(DataPropertyAttribute))  
  13.          {  
  14.              if (dataProperties.ContainsKey(entityType))  
  15.              {  
  16.                  return dataProperties[entityType];  
  17.              }  
  18.              var properties = (from property in entityType.GetProperties()  
  19.                                where property.GetCustomAttributes(typeof(DataPropertyAttribute), true).Any()  
  20.                                select property.Name).ToArray();  
  21.              dataProperties[entityType] = properties;  
  22.              return properties;  
  23.          }  
  24.      }  
  25.  }  

 #p#

二、Control/DataSource映射的表示:BindingMapping

不論是數據綁定(實體=〉控件),還是數據捕捉(控件=〉實體)的實現都建立在兩種之間存在著某種約定的映射之上,這個映射是整個DataBinder的核心所在。在這里,我定義了如下一個BindingMapping類型表示這個映射關系。

  1. public class BindingMapping: ICloneable  
  2. {  
  3.     public Type DataSourceType { get; private set; }  
  4.     public Control Control { get; set; }  
  5.     public string ControlValueProperty { get; set; }  
  6.     public string DataSourceProperty { get; set; }  
  7.     public bool AutomaticBind { get; set; }  
  8.     public bool AutomaticUpdate { get; set; }  
  9.     public string FormatString { get; set; }  
  10.     public Type ControlValuePropertyType  
  11.     {  
  12.         get { return PropertyAccessor.GetPropertyType(this.Control.GetType(), this.ControlValueProperty); }  
  13.     }  
  14.     public Type DataSourcePropertyType  
  15.     {  
  16.         get { return PropertyAccessor.GetPropertyType(this.DataSourceType, this.DataSourceProperty); }  
  17.     }  
  18.      
  19.     public BindingMapping(Type dataSourceType, Control control, string controlValueProperty, string dataSourceProperty)  
  20.     {  
  21.          //...  
  22.         this.DataSourceType         = dataSourceType;  
  23.         this.Control                = control;  
  24.         this.ControlValueProperty   = controlValueProperty;  
  25.         this.DataSourceProperty     = dataSourceProperty;  
  26.         this.AutomaticBind          = true;  
  27.         this.AutomaticUpdate        = true;  
  28.     }  
  29.     object ICloneable.Clone()  
  30.     {  
  31.         return this.Clone();  
  32.     }  
  33.     public BindingMapping Clone()  
  34.     {  
  35.         var bindingMapping = new BindingMapping(this.DataSourceType, this.Control, this.ControlValueProperty, this.DataSourceProperty);  
  36.         bindingMapping.AutomaticBind = this.AutomaticBind;  
  37.         bindingMapping.AutomaticUpdate = this.AutomaticBind;  
  38.         return bindingMapping;  
  39.     }  
  40. }  

這里我主要介紹一下各個屬性的含義:

DataSourceType:作為數據源實體的類型;

Control:需要綁定的控件;

ControlValueProperty:數據需要綁定到控件屬性的名稱,比如TextBox是Text屬性,而RadioButtonList則是SelectedValue屬性;

DataSourceProperty:實體類型中的數據屬性名稱

AutomaticBind:是否需要進行自動綁定,通過它阻止不必要的自動數據綁定行為。默認值為True,如果改成False,基于該條映射的綁定將被忽略;

AutomaticUpdate:是否需要進行自動更新到數據實體中,通過它阻止不必要的自動數據捕捉行為。默認值為True,如果改成False,基于該條映射的數據捕捉定將被忽略;

FormatString:格式化字符串;

ControlValuePropertyType:控件綁定屬性的類型,比如TextBox的綁定屬性為Text,那么ControlValuePropertyType為System.String;

DataSourcePropertyType:實體屬性類型。

需要補充一點的是:ControlValuePropertyType和DataSourcePropertyType使用到了之前定義的用于操作操作屬性的組件ProcessAccessor。BindingMapping采用了克隆模式。

#p#

三、如何建立Control/DataSource映射集合

BindingMapping表示的一個實體類型的數據屬性和具體控件之間的映射關系,而這種關系在使用過程中是以批量的方式進行創建的。具體來說,我們通過指定實體類型和一個作為容器的空間,如果容器中的存在滿足映射規則的子控件,相應的映射會被創建。映射的批量創建是通過DataBinder的靜態方法BuildBindingMappings來實現的。

在具體介紹BuildBindingMappings方法之前,我們需要先來討論一個相關的話題:在進行數據綁定的時候,如何決定數據應該賦值給控件的那個屬性。我們知道,不同的控件類型擁有不同的數據綁定屬性,比如TextBox自然是Text屬性,CheckBox則是Checked屬性。ASP.NET在定義控件類型的時候,采用了一個特殊性的特性ControlValuePropertyAttribute來表示那個屬性表示的是控件的“值”。比如TextBox和CheckBox分別是這樣定義的。

  1. [ControlValueProperty("Text")]  
  2. public class TextBox : WebControl, IPostBackDataHandler, IEditableTextControl, ITextControl  
  3. {  
  4.     //...  
  5. }  
  6.     
  7. ControlValueProperty("Checked")]  
  8. public class CheckBox : WebControl, IPostBackDataHandler, ICheckBoxControl  
  9. {  
  10.     //...  
  11. }  

在這里我們直接將ControlValuePropertyAttribute中指定的名稱作為控件綁定的屬性名,即BindingMapping的ControlValueProperty屬性。該值得獲取通過如下一個GetControlValuePropertyName私有方法完成。為了避免重復反射操作,這里采用了全局緩存。

  1. private static string GetControlValuePropertyName(Control control)  
  2. {  
  3.     if (null == control)  
  4.     {  
  5.         return null;  
  6.     }  
  7.     Type entityType = control.GetType();  
  8.     if (controlValueProperties.ContainsKey(entityType))  
  9.     {  
  10.         return controlValueProperties[entityType];  
  11.     }  
  12.     lock (typeof(DataBinder))  
  13.     {  
  14.         if (controlValueProperties.ContainsKey(entityType))  
  15.         {  
  16.             return controlValueProperties[entityType];  
  17.         }  
  18.         ControlValuePropertyAttribute controlValuePropertyAttribute = (ControlValuePropertyAttribute)entityType.GetCustomAttributes(typeof(ControlValuePropertyAttribute), true)[0];  
  19.         controlValueProperties[entityType] = controlValuePropertyAttribute.Name;  
  20.         return controlValuePropertyAttribute.Name;  
  21.     }  
  22. }  

最終的映射通過如下定義的BuildBindingMappings方法來建立,缺省參數suffix代表的是控件的后綴,其中已經在《上篇》介紹過了。

  1. public static IEnumerable<BindingMapping> BuildBindingMappings(Type entityType, Control container, string suffix = "")  
  2. {  
  3.     //...  
  4.     suffixsuffix = suffix??string.Empty;  
  5.     return (from property in DataPropertyAttribute.GetDataProperties(entityType)  
  6.             let control = container.FindControl(string.Format("{1}{0}", suffix, property))  
  7.             let controlValueProperty = GetControlValuePropertyName(control)  
  8.             where null != control  
  9.             select new BindingMapping(entityType, control, controlValueProperty, property)).ToArray();  
  10. }  

#p#

四、通過映射集合實現數據綁定

通過《上篇》我們知道,DataBinder提供兩種數據綁定方式:一種是直接通過傳入數據實體對象和容器控件對具有匹配關系的所有子控件進行綁定;另外一種則是通過調用上面BuildBindingMappings靜態方法建立的BindingMapping集合,然后再借助于這個集合進行數據綁定。這兩種方式的數據綁定對應于如下兩個重載的BindData方法:

  1. public class DataBinder  
  2. {  
  3.     //...  
  4.     public void BindData(object entity, Control container, string suffix = "");  
  5.     public void BindData(object entity,IEnumerable<BindingMapping> bindingMappings);  
  6. }  

已經上在內部,上面一個方法也是需要通過調用BuildBindingMappings來建立映射。數據綁定始終是根據BindingMapping集合進行的。由于在BindingMapping中已經定義了完成數據綁定所需的必要信息,數據綁定的邏輯變得很簡單。具體來說,數據綁定的邏輯是這樣的:遍歷所有的集合中每個BindingMapping,根據DataSourceProperty得到屬性名稱,然后進一步從數據源實體中得到具體的值。根據ControlValuePropertyType得到目標控件綁定屬性的類型,然后將之前得到的值轉換成該類型。***,通過ControlValueProperty得到控件的綁定屬性,將之前經過轉換的值給控件的這個屬性就可以了。整個數據綁定實現在如下一個OnBindData方法中。關于屬性操作則借助于PropertyAccessor這個組件。

  1. protected virtual void OnBindData(IEnumerable<BindingMapping> bindingMappings, object entity)  
  2. {  
  3.     foreach (var mapping in bindingMappings)  
  4.     {  
  5.         var bindingMapping = mapping.Clone();  
  6.         object value = PropertyAccessor.Get(entity, bindingMapping.DataSourceProperty);  
  7.         if (null != this.DataItemBinding)  
  8.         {  
  9.             var args = new DataBindingEventArgs(bindingMapping, value);  
  10.             this.DataItemBinding(this, args);  
  11.             value = args.DataValue;  
  12.         }  
  13.         if (!bindingMapping.AutomaticBind)  
  14.         {  
  15.             continue;  
  16.         }  
  17.    
  18.         if (!string.IsNullOrEmpty(bindingMapping.FormatString))  
  19.         {  
  20.             value = Format(value, bindingMapping.FormatString);  
  21.         }  
  22.           
  23.         Type controlValuePropertyType = PropertyAccessor.GetPropertyType(bindingMapping.Control.GetType(), bindingMapping.ControlValueProperty);  
  24.         value = ChangeType(value, controlValuePropertyType);  
  25.         if (null == value && typeof(ValueType).IsAssignableFrom(controlValuePropertyType))  
  26.         {  
  27.             value = Activator.CreateInstance(controlValuePropertyType);  
  28.         }  
  29.         PropertyAccessor.Set(bindingMapping.Control, bindingMapping.ControlValueProperty, value);  
  30.         if (null != this.DataItemBound)  
  31.         {  
  32.             this.DataItemBound(this, new DataBindingEventArgs(bindingMapping, value));  
  33.         }  
  34.     }  
  35. }  

DataBinder設計的目標是讓默認的綁定行為解決80%的問題,并且提供給相應的方式去解決余下的問題。為了讓開發者能夠有效解決余下的這20%的綁定問題,我們定義兩個事件:DataItemBinding和DataBound,它們分別在進行綁定之前和之后被觸發。關于事件的觸發,已經體現在OnBindData方法的定義中了。

#p#

五、通過映射集合實現數據捕捉

數據綁定使用到的實際上是Entity-〉Control映射,如果我們借助控件到Control-〉Entity,就能實現自動捕獲控件的值然后將其保存到給定的實體對象上。我為此在DataBinder上定義了兩個重載的UpdateData方法。

  1. public class DataBinder  
  2. {  
  3.     //...  
  4.     public void BindData( object entity,IEnumerable<BindingMapping> bindingMappings);  
  5.     public void UpdateData( object entity, Control container, string suffix = "");  
  6. }  

UpdateData方法的實現和BindData方法的邏輯基本一致,將Control和Entity呼喚一下而已,所以在這里我就不再贅言敘述了。
 

原文鏈接:http://www.cnblogs.com/artech/archive/2011/03/27/databinding2.html

【編輯推薦】

  1. DBA應用技巧:如何升級InnoDB Plugin
  2. 一句代碼實現批量數據綁定[上篇]
  3. DBA必備:MySQL數據庫常用操作和技巧
  4. MySQL日志操作教程:DBA們管理的利器
  5. MySQL觸發器如何正確使用

 

責任編輯:艾婧 來源: 博客園
相關推薦

2011-03-24 10:24:45

批量數據綁定

2012-02-09 09:41:22

2023-11-06 08:31:58

業務代碼多線程

2021-05-11 15:34:04

Task.Result代碼Winform

2009-03-10 18:10:12

LinuxUbuntu技巧

2013-03-22 10:53:42

PyConPython

2011-06-13 09:25:01

斷號

2015-08-03 10:21:04

設計模式表達

2022-08-01 10:01:11

JavaScript語言代碼庫

2020-11-27 09:57:11

Python代碼PyPy

2019-11-15 18:00:18

MySQLSQL數據庫

2013-05-10 10:56:09

2021-12-17 08:55:26

Python微博機器人

2023-07-12 08:01:28

FOADMROADMOXC

2023-09-05 23:34:52

Kubernetes云原生

2009-10-29 09:57:16

VB.NET實現數據綁

2014-12-16 08:58:17

甲骨文Oracle數據庫選件

2025-03-13 11:09:47

2016-09-12 15:26:06

戴爾

2023-08-25 17:10:14

LLM人工智能
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产在线拍偷自揄拍视频 | 99精品国产一区二区三区 | 国产精品视频在线播放 | 亚洲精品68久久久一区 | 欧美精品一二区 | 国产精品777一区二区 | 激情五月婷婷在线 | 午夜免费小视频 | 日韩国产一区二区三区 | 91久久久久久久久久久久久 | 免费视频99 | 日韩精品免费在线观看 | 日本午夜精品一区二区三区 | 亚洲精品一区二区在线观看 | 亚洲综合中文字幕在线观看 | 国产在线精品一区二区 | 欧美久久精品一级c片 | 亚洲一区二区三区在线观看免费 | 免费在线观看av的网站 | 欧美黄色一级毛片 | 二区成人 | 亚洲日本免费 | 亚洲一区二区三区在线视频 | 国产亚洲精品久久久久久牛牛 | 亚洲免费在线 | 精品在线一区 | 免费看日韩视频 | 中文字幕av第一页 | 视频一区 国产精品 | 99re99| 91亚洲欧美| 手机av在线 | 国产精品日韩欧美一区二区三区 | 在线免费视频一区 | 男人天堂午夜 | 韩日免费视频 | 成人黄色av网站 | 91视频大全 | 国产精品久久久久久久久免费 | 免费看91 | 91豆花视频 |