In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to understand the FluentHtml and continuous interface in ASP.NET MVC. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
We strive to make the page layer code concise and readable. On the ASP.NET MVC platform, we use a new starting point to achieve this goal. MvcContrib. FluentHtml and Spark ViewEngine set an example for us. This paper will take MvcContrib.FluentHtml as an example to explore its implementation mechanism: Fluent Interface.
In the application of MvcContrib.FluentHtml, we can see the following code everywhere:
< %= this.TextBox(x =>X.Person.Name) .title ("Enter the person's name") .Label ("Name:")% >...
< %= this.Select(x =>X.Person.Gender) .options (Model.Genders) .size (5) .Label ("Gender:") .title ("Select the person's gender")% >
The code generated in the browser is:
< LABEL id=Person_Name_Label for=Person_Name>Name:
< /LABEL> < INPUT id=Person_Name title="Enter the person's name" value=Jeremy maxLength=50 name=Person.Name>.
< 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>It's interesting to generate TextBox and Select code dynamically. We use the usual way to generate the same client code on the page. The CS code looks something like this:
Label label = new Label (); label.Text = "Name"; TextBox textbox= new TextBox (); textbox.ToolTip = "Enter the person's name"; textbox.ID = "No.10001"; textbox.ID = "Person.Name"
The way FluentHtml creates page elements reminds us of the use of StringBuilder:
StringBuilder stringbuilder = new StringBuilder (); stringbuilder.Append ("Hello") .Append (") .Append (" World! ")
The programming method of Fulent Interface is "Fluent Interface", which is not a new concept. In 2005, Eric Evans and Martin Fowler named this implementation. Source documents can be described in Wikipedia to get a basic understanding of 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.
Let's break down the above words:
It is an implementation of object-oriented API to increase the readability of the code. Since we are most familiar with StringBuilder, let's follow this clue: open Reflector and it's easy to find the Append method of StringBuilder:
Public StringBuilder Append (string value) {if (value! = null) {stringstringValue = this.m_StringValue; IntPtr currentThread = Thread.InternalGetCurrentThread (); if (this.m_currentThread! = currentThread) {stringstringValue = string.GetStringForStringBuilder (stringValue, stringValue.Capacity);} int length = stringValue.Length; int requiredLength = length + value.Length; if (this.NeedsAllocation (stringValue, requiredLength)) {string newString = this.GetNewString (stringValue, requiredLength); newString.AppendInPlace (value, length) This.ReplaceString (currentThread, newString);} else {stringValue.AppendInPlace (value, length); this.ReplaceString (currentThread, stringValue);}} return this;}
There are two special points to pay attention to in reading this paragraph: 1. The return value of the method is the StringBuilder type 2. For a deep understanding, let's write a simple StringBuilder: return this;:
Public interface IContentBuilder {void WriteContent (); IContentBuilder Append (string partialContent);} public class TestContentBuilder: IContentBuilder {string temp; # region IContentBuilder Members void IContentBuilder.WriteContent () {Console.Write (temp);} IContentBuilder IContentBuilder.Append (string partialContent) {temp + = partialContent; return this;} # endregion} … ... / / call code IContentBuilder t = new TestContentBuilder (); t.Append ("test") .Append ("Hello") .WriteContent ()
Run the code, and the effect is the same as StringBuilder. It can also be seen from the above applications that Fluent Interface is often used to complete object construction and attribute assignment.
Back to the point: FluentHTML has learned about Fluent Interface. Let's take a look at the implementation of MVCContrib.FluentHTML. Here we take TextBox as an example. Let's first take a look at its inheritance relationship:
Public class TextBox: TextInput public abstract class TextInput: Input, ISupportsMaxLength where T: TextInput public abstract class Input: FormElement where T: Input, Ielement
Generics is a high-level algorithmic abstraction, and we can get a glimpse of it through Input:
Public abstract class Input: FormElement where T: Input, IElement {protected object elementValue; protected Input (string type, string name): base (HtmlTag.Input, name) {builder.MergeAttribute (HtmlAttribute.Type, type, true);} protected Input (string type, string name, MemberExpression forMember, IEnumerable behaviors): base (HtmlTag.Input, name, forMember, behaviors) {builder.MergeAttribute (HtmlAttribute.Type, type, true);} / Set the 'value' attribute. / The value for the attribute. Public virtual T Value (object value) {elementValue = value; return (T) this;} / Set the 'size' attribute. / The value for the attribute. Public virtual T Size (int value) {Attr (HtmlAttribute.Size, value); return (T) this;} protected override void PreRender () {Attr (HtmlAttribute.Value, elementValue); base.PreRender ();}} take Size method as an example, we can see that this is a typical Fluent Interface implementation: public virtual T Size (int value) {Attr (HtmlAttribute.Size, value); return (T) this;}
At this point, there is something strange about the above statement, which is the part of the Lambda expression:
This.TextBox (x = > x.Person.Name) .title ("Enter the person's name") .Label ("Name:")
We don't see support for Lambda expressions in the TextBox implementation code. Where was it finished? By following up, we came to ViewDataContainerExtensions, which is IViewDataCon
Namespace MvcContrib.FluentHtml {/ Extensions to IViewDataContainer / public static class ViewDataContainerExtensions {/ Generate an HTML input element of type 'text' and set its value from ViewData based on the name provided. / The view. / / Value of the 'name' attribute of the element.Also used to derive the' id' attribute. Public static TextBox TextBox (this IViewDataContainer view, string name) {return new TextBox (name) .value (view.ViewData.Eval (name));}... ...
Extension Method of tainer:
Take a look at return new TextBox (name) .value (view.ViewData.Eval (name)); so here is the step for TextBox to define the method chain.
Summary of FluentHtml and continuous Interface
In order to construct HTML elements succinctly and clearly in View, htmlHelper.InputHelper is used to construct page elements in Asp.net MVC. HTML is also the Extension Method of htmlHelper used in the page layer. In contrast, htmlHelper provides the basic definition and construction of page controls, while FluentHTML is more flexible.
On how to understand the FluentHtml and continuous interface in ASP.NET MVC to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.