How to write HtmlHelper in Fluent syntax

前端 未结 1 1814
耶瑟儿~
耶瑟儿~ 2021-02-10 22:27

I have a simple tag builder that looks like this:

public static MvcHtmlString Tag(this HtmlHelper helper, string tag, string content)
{
    var tagBuilder = new          


        
1条回答
  •  清歌不尽
    2021-02-10 23:10

    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

    0 讨论(0)
提交回复
热议问题