日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
淺談ASP.NETMVC中的FluentHtml與連續(xù)接口

我們力求頁面層代碼簡潔并具有較好的可讀性,在ASP.NET MVC的平臺上,我們以新的起點(diǎn)來實(shí)現(xiàn)這一目標(biāo).MvcContrib.FluentHtml和Spark ViewEngine給我們做出了榜樣.本文將以MvcContrib.FluentHtml為例探究它的實(shí)現(xiàn)機(jī)制:Fluent Interface.

10余年的彭山網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整彭山建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“彭山網(wǎng)站設(shè)計(jì)”,“彭山網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

在MvcContrib.FluentHtml的應(yīng)用中,我們隨處可以見到下面的代碼:

 
 
 
  1. < %= this.TextBox(x => x.Person.Name).Title("Enter the person's name").Label("Name:") %> 
  2. ……   
  3.  < %= this.Select(x => x.Person.Gender).Options(Model.Genders).Size(5).Label("Gender:")  
  4. .Title("Select the person's gender") %> 

 瀏覽器中生成的代碼為:

 
 
 
  1. < LABEL id=Person_Name_Label for=Person_Name>Name:< /LABEL> 
  2. < INPUT id=Person_Name title="Enter the person's name" value=Jeremy maxLength=50 name=Person.Name> 
  3.  .  
  4. < SELECT id=Person_Gender title="Select the person's gender" size=5 name=Person.Gender>< OPTION selected value=M>Male< /OPTION>< OPTION value=F>Female< /OPTION>< /SELECT> 

上面對動態(tài)生成TextBox和Select的代碼很有意思,我們使用普通的方式在頁面上生成同樣的客戶端代碼,CS代碼大致是這樣的: 

 
 
 
  1. Label label = new Label();  
  2.  label.Text = "Name";  
  3.  TextBox textboxnew TextBox();  
  4.  textbox.ToolTip ="Enter the person's name";  
  5.  textbox.ID = "No.10001";  
  6.  textbox.ID = "Person.Name"

而FluentHtml創(chuàng)建頁面元素的方式讓我們很容易聯(lián)想到StringBuilder的使用: 

 
 
 
  1. StringBuilder stringbuilder = new StringBuilder();  
  2.  stringbuilder.Append("Hello").Append(" ").Append("World!"); 

Fulent Interface 這種實(shí)現(xiàn)編程方式就是"Fluent Interface",這并不是什么新概念,2005年Eric Evans 和Martin Fowler就為這種實(shí)現(xiàn)方式命名.源文檔 可以通過維基百科中對Fluent Interface的描述獲得一個(gè)基本的了解:In software engineering, a fluent interface (as first coined by Eric Evans and Martin Fowler) is a way of implementing an object oriented API in a way that aims to provide for more readable code.

我們分解上面的話:

它是面向?qū)ο驛PI的一種實(shí)現(xiàn)方式,目的是增加代碼的可讀性.。既然我們最熟悉的是StringBuilder,我們就從這個(gè)線索追下去:打開Reflector,很容易找到StringBuilder的Append方法:

 
 
 
  1. public StringBuilder Append(string value)  
  2. {  
  3. if (value != null)  
  4. {  
  5.  string stringValue = this.m_StringValue;  
  6.  IntPtr currentThread = Thread.InternalGetCurrentThread();  
  7.  if (this.m_currentThread != currentThread)  
  8.  {  
  9.  stringstringValue = string.GetStringForStringBuilder(stringValue, stringValue.Capacity);  
  10.  }  
  11.  int length = stringValue.Length;  
  12.  int requiredLength = length + value.Length;  
  13.  if (this.NeedsAllocation(stringValue, requiredLength))  
  14.  {  
  15.  string newString = this.GetNewString(stringValue, requiredLength);  
  16.  newString.AppendInPlace(value, length);  
  17.  this.ReplaceString(currentThread, newString);  
  18.  }  
  19.  else  
  20.  {  
  21.  stringValue.AppendInPlace(value, length);  
  22.  this.ReplaceString(currentThread, stringValue);  
  23.  }  
  24. }  
  25. return this;  

閱讀這段有兩個(gè)特別要注意的點(diǎn):1.方法的返回值是StringBuilder類型 2.***一句:return this;為了深刻理解,我們寫一個(gè)簡單的StringBuilder:

 
 
 
  1. public interface IContentBuilder  
  2. {  
  3.  void WriteContent();  
  4.  IContentBuilder Append(string partialContent);  
  5. }  
  6. public class TestContentBuilder : IContentBuilder  
  7. {  
  8.  string temp;  
  9.  #region IContentBuilder Members  
  10.  
  11.  void IContentBuilder.WriteContent()  
  12.  {  
  13.  Console.Write(temp);  
  14.  }  
  15.  
  16.  IContentBuilder IContentBuilder.Append(string partialContent)  
  17.  {  
  18.  temp += partialContent;  
  19.  return this;  
  20.  }  
  21.  
  22.  #endregion  
  23. }  
  24. … …  
  25. //調(diào)用代碼  
  26. IContentBuilder t = new TestContentBuilder();  
  27.  t.Append("test").Append("Hello").WriteContent(); 

跑一下代碼,和StringBuilder效果是一樣的.從上面的應(yīng)用也可以看出:Fluent Interface經(jīng)常用來完成對象的構(gòu)造和屬性賦值.

言歸正傳:FluentHTML了解了Fluent Interface,我們來看一下MVCContrib.FluentHTML的實(shí)現(xiàn),這里以TextBox為例進(jìn)行考察,首先看一下它的繼承關(guān)系:

 
 
 
  1. public class TextBox : TextInput  
  2. public abstract class TextInput : Input, ISupportsMaxLength where T : TextInput  
  3. public abstract class Input : FormElement where T : Input, Ielement 

泛型是一種高層次的算法抽象,我們就通過Input 一窺端倪:

 
 
 
  1. public abstract class Input : FormElement where T : Input, IElement  
  2. {  
  3. protected object elementValue;  
  4. protected Input(string type, string name) : base(HtmlTag.Input, name)  
  5. {  
  6.  builder.MergeAttribute(HtmlAttribute.Type, type, true);  
  7. }  
  8. protected Input(string type, string name, MemberExpression forMember, IEnumerable behaviors)  
  9.  : base(HtmlTag.Input, name, forMember, behaviors)  
  10. {  
  11.  builder.MergeAttribute(HtmlAttribute.Type, type, true);  
  12. }  
  13. ///   
  14. /// Set the 'value' attribute.  
  15. ///   
  16. /// The value for the attribute.  
  17. public virtual T Value(object value)  
  18. {  
  19.  elementValue = value;  
  20.  return (T)this;  
  21. }  
  22. ///   
  23. /// Set the 'size' attribute.  
  24. ///   
  25. /// The value for the attribute.  
  26. public virtual T Size(int value)  
  27. {  
  28.  Attr(HtmlAttribute.Size, value);  
  29.  return (T)this;  
  30. }  
  31. protected override void PreRender()  
  32. {  
  33.  Attr(HtmlAttribute.Value, elementValue);  
  34.  base.PreRender();  
  35. }  
  36. }  
  37. 以Size方法為例,可以看出這是一種典型的Fluent Interface實(shí)現(xiàn):  
  38. public virtual T Size(int value)  
  39. {  
  40. Attr(HtmlAttribute.Size, value);  
  41. return (T)this;  

分析到這里,上面的語句中還有一點(diǎn)比較奇怪,就是Lambda表達(dá)式的部分:

 
 
 
  1. this.TextBox(x => x.Person.Name).Title("Enter the person's name").Label("Name:") 

TextBox的實(shí)現(xiàn)代碼里面我們沒有看到對Lambda表達(dá)式的支持.那是在什么地方完成的呢?通過跟進(jìn),我們來到了ViewDataContainerExtensions,它是IViewDataCon

 
 
 
  1. namespace MvcContrib.FluentHtml  
  2. {  
  3. ///   
  4. /// Extensions to IViewDataContainer  
  5. ///   
  6. public static class ViewDataContainerExtensions  
  7. {  
  8.  ///   
  9.  /// Generate an HTML input element of type 'text' and set its value from ViewData based on the name provided.  
  10.  ///   
  11.  /// The view.  
  12.  /// Value of the 'name' attribute of the element.Also used to derive the 'id' attribute.  
  13.  public static TextBox TextBox(this IViewDataContainer view, string name)  
  14.  {  
  15.  return new TextBox(name).Value(view.ViewData.Eval(name));  
  16.  }  
  17. … …  

tainer 的Extension Method:

看一下return new TextBox(name).Value(view.ViewData.Eval(name));所以這里就成了TextBox定義方法鏈的***步.

FluentHtml與連續(xù)接口總結(jié)

為了能夠在View中能夠簡潔清晰的構(gòu)造HTML元素,Asp.net MVC中通過htmlHelper.InputHelper來實(shí)現(xiàn)頁面元素的構(gòu)造. 頁面層所使用的<%= Html.TextBox("username") %>,HTML也是htmlHelper的Extension Method.相比較起來,htmlHelper提供了基礎(chǔ)的頁面控件定義和構(gòu)造,而FluentHTML表現(xiàn)的更為靈活.除了FluentHTML,著名的Spark View Engine也有類似的實(shí)現(xiàn),大家可以關(guān)注一下.

【編輯推薦】

  1. VS2010 Beta 1的ASP.NET MVC安裝包發(fā)布
  2. ASP.NET中性能和擴(kuò)展性的秘密
  3. ASP.NET 3.5圖表控件親密接觸
  4. 自己動手實(shí)現(xiàn)Asp.net的MVC框架
  5. ASP.NET中防止用戶多次登錄的方法

名稱欄目:淺談ASP.NETMVC中的FluentHtml與連續(xù)接口
文章網(wǎng)址:http://m.5511xx.com/article/dhohisi.html