ASP.NET MVC中MvcContrib.FluentHtml如何使用

2023-05-13

ASP.NET MVC中MvcContrib.FluentHtml如何使用,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

ASP.NET MVC

在MvcContrib.FluentHtml的应用中,我们随处可以见到下面的代码:

<%=this.TextBox(x=>x.Person.Name).Title("Entertheperson'sname").Label("Name:")%><br/>  ……  <%=this.Select(x=>x.Person.Gender).Options(Model.Genders).Size(5).Label("Gender:")  .Title("Selecttheperson'sgender")%><br/>  <labelidlabelid="Person_Name_Label"for="Person_Name">Name:</label> <inputidinputid="Person_Name"type="text"value="Jeremy"title="Entertheperson'sname"name="Person.Name"maxlength="50"/> .  <selectidselectid="Person_Gender"title="Selecttheperson'sgender"size="5"name="Person.Gender"> <optionvalueoptionvalue="M"selected="selected">Male</option> <optionvalueoptionvalue="F">Female</option> </select>

这种实现编程方式就是"Fluent Interface",这并不是什么新概念,2005年Eric Evans 和Martin Fowler就为这种实现方式命名.源文档 <http://www.martinfowler.com/bliki/FluentInterface.html> 可以通过维基百科中对Fluent Interface的描述获得一个基本的了解: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.

我们分解上面的话:
◆它是面向对象API的一种实现方式
◆目的是增加代码的可读性

既然我们最熟悉的是StringBuilder,我们就从这个线索追下去:打开Reflector,很容易找到StringBuilder的Append方法:

publicStringBuilderAppend(stringvalue)  {  if(value!=null)  {  stringstringValue=this.m_StringValue;  IntPtrcurrentThread=Thread.InternalGetCurrentThread();  if(this.m_currentThread!=currentThread)  {  stringstringValue=string.GetStringForStringBuilder(stringValue,stringValue.Capacity);  }  intlength=stringValue.Length;  intrequiredLength=length+value.Length;  if(this.NeedsAllocation(stringValue,requiredLength))  {  stringnewString=this.GetNewString(stringValue,requiredLength);  newString.AppendInPlace(value,length);  this.ReplaceString(currentThread,newString);  }  else  {  stringValue.AppendInPlace(value,length);  this.ReplaceString(currentThread,stringValue);  }  }  returnthis;  }

了解了Fluent Interface,我们来看一下MVCContrib.FluentHTML的实现,这里以TextBox为例进行考察,首先看一下它的继承关系:

public class TextBox : TextInput< TextBox>

public abstract class TextInput< T> : Input< T>, ISupportsMaxLength where T : TextInput< T>

public abstract class Input< T> : FormElement< T> where T : Input< T>, Ielement

泛型是一种高层次的算法抽象,我们就通过Input< T>一窥端倪:

  1. publicabstractclassInput<T>:FormElement<T>whereT:Input<T>,IElement  

  2. {  

  3. protectedobjectelementValue;  

  4.  

  5. protectedInput(stringtype,stringname):base(HtmlTag.Input,name)  

  6. {  

  7. builder.MergeAttribute(HtmlAttribute.Type,type,true);  

  8. }  

  9.  

  10. protectedInput(stringtype,stringname,MemberExpressionforMember,
    IEnumerable<IBehaviorMarker>behaviors)  

  11. :base(HtmlTag.Input,name,forMember,behaviors)  

  12. {  

  13. builder.MergeAttribute(HtmlAttribute.Type,type,true);  

  14. }  

  15.  

  16. ///<summary> 

  17. ///Setthe'value'attribute.  

  18. ///</summary> 

  19. ///<paramnameparamname="value">Thevaluefortheattribute.</param> 

  20. publicvirtualTValue(objectvalue)  

  21. {  

  22. elementValue=value;  

  23. return(T)this;  

  24. }  

  25.  

  26. ///<summary> 

  27. ///Setthe'size'attribute.  

  28. ///</summary> 

  29. ///<paramnameparamname="value">Thevaluefortheattribute.</param> 

  30. publicvirtualTSize(intvalue)  

  31. {  

  32. Attr(HtmlAttribute.Size,value);  

  33. return(T)this;  

  34. }  

  35.  

  36. protectedoverridevoidPreRender()  

  37. {  

  38. Attr(HtmlAttribute.Value,elementValue);  

  39. base.PreRender();  

  40. }  

关于ASP.NET MVC中MvcContrib.FluentHtml如何使用问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注本站行业资讯频道了解更多相关知识。

《ASP.NET MVC中MvcContrib.FluentHtml如何使用.doc》

下载本文的Word格式文档,以方便收藏与打印。