如何写在的HtmlHelper语法流利 [英] How to write HtmlHelper in Fluent syntax

查看:104
本文介绍了如何写在的HtmlHelper语法流利的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的标签生成器,看起来像这样:

 公共静态MvcHtmlString标记(此的HtmlHelper帮手,字符串变量,字符串内容)
{
    VAR tagBuilder =新TagBuilder(标签){innerHTML的内容=};
    返回MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.NormalTag));
}

和,我可以用这样的:

  @的HTML.Tag(EM,Model.Title)

主要生产:

 < EM>在标题< / EM>

怎么可以这样写使用流利的语法,因此它的使用是这样的:

  @的HTML.Tag(EM)。内容(Model.Title)


解决方案

您必须定义一个建设者接口和实现。我希望我的例子,可以提供一些指导:

 公共静态类MyHtmlExtensions
{
    公共静态IMyTagBuilder标记(此的HtmlHelper帮手,字符串标记)
    {
        返回新MyTagBuilder(标签);
    }
}

然后你定义你的建设者接口和实现:

  public接口IMyTagBuilder:IHtmlString
{
    IHtmlString内容(字符串内容);
}公共类MyTagBuilder:IMyTagBuilder
{
    私人只读TagBuilder _tagBuilder;    公共MyTagBuilder(字符串标记)
    {
        _tagBuilder =新TagBuilder(标签);
    }    公共IHtmlString内容(字符串内容)
    {
        _tagBuilder.InnerHtml =内容;
        返回此;
    }    公共字符串ToHtmlString()
    {
        返回_tagBuilder.ToString(TagRenderMode.NormalTag);
    }
}

由于 IMyTagBuilder 工具 IHtmlString ,它可以有或没有调用使用。内容()之后。

一个伟大的把戏实现流畅的接口它使用 IFluentInterface 隐藏对象成员(的ToString 等于 GetHash $ C $ ç的GetType 从智能感知),它会删除一些噪音。

修改:从构建Funq <建立一个流畅的API是丹尼尔Cazzulino的截屏的重要资源href=\"https://www.youtube.com/watch?v=Zi9K9N9KtEc&index=8&list=PLpBzqAJhzCLfPHtdcPEy1jj3W16MAeX1M\"相对=nofollow>这里

I have a simple tag builder that looks like this:

public static MvcHtmlString Tag(this HtmlHelper helper, string tag, string content)
{
    var tagBuilder = new TagBuilder(tag){InnerHtml = content};
    return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.NormalTag));
}

And, I can use it like this:

@Html.Tag("em", Model.Title)

which produces:

<em>The Title</em>

How can this be written to use a Fluent Syntax so it's use would look like this:

@Html.Tag("em").Content(Model.Title)

解决方案

You have to define a builder interface and implementation. I hope my example can provide some guidance:

public static class MyHtmlExtensions
{
    public static IMyTagBuilder Tag(this HtmlHelper helper, string tag)
    {
        return new MyTagBuilder(tag);
    }
}

Then you define your builder interface and implementation:

public interface IMyTagBuilder : IHtmlString
{
    IHtmlString Content(string content);
}

public class MyTagBuilder : IMyTagBuilder
{
    private readonly TagBuilder _tagBuilder;

    public MyTagBuilder(string tag)
    {
        _tagBuilder = new TagBuilder(tag);
    }

    public IHtmlString Content(string content)
    {
        _tagBuilder.InnerHtml = content;
        return this;
    }

    public string ToHtmlString()
    {
        return _tagBuilder.ToString(TagRenderMode.NormalTag);
    }
}

Since IMyTagBuilder implements IHtmlString, it can be used either with or without calling .Content() afterwards.

A great trick to use when implementing fluent interfaces it to use a IFluentInterface to hide object members (ToString, Equals, GetHashCode and GetType) from IntelliSense, it removes some noise.

EDIT: A great resource for building fluent APIs is Daniel Cazzulino's screencast from building Funq here

这篇关于如何写在的HtmlHelper语法流利的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆