zoukankan      html  css  js  c++  java
  • C#中扩展StringBuilder支持链式方法

    本篇体验扩展StringBuilder使之支持链式方法。

    这里有一个根据键值集合生成select元素的方法。

    private static string BuilderSelectBox(IDictionary<int, string> options, string id, bool includeUnknown)
    {
        var html = new StringBuilder();
        html.AppendFormat("<select id="{0}" name="{0}">", id);
        html.AppendLine();
        
        if(includeUnknown)
        {
            html.AppendLine("	<option>Unknown</option>");
        }
        
        foreach(var opt in options)
        {
            html.AppendFormat("	<option value="{0}">{1}</option>", opt.Key, opt.Value);
            html.AppendLine();
        }
        
        html.AppendLine("</select>");
        
        return html.ToString();
    }

    以上,

    html.AppendFormat("<select id="{0}" name="{0}">", id);
    html.AppendLine();

    可以对这两个语句封装,扩展StringBuilder。
                
                
    改成

    public static class StringBuilderExtensions
    {
        public static StringBuilder AppendFormattedLine(this StringBuilder @this, string format, prams object[] args) => @this.AppendFormat(format, args).AppendLine();
    }
    
    private static string BuilderSelectBox(IDictionary<int, string> options, string id, bool includeUnknown)
    {
        var html = new StringBuilder()
            .AppendFormattedLine("<select id="{0}" name="{0}">", id);
    
        
        if(includeUnknown)
        {
            html.AppendLine("	<option>Unknown</option>");
        }
        
        foreach(var opt in options)
        {
            html.AppendFormattedLine("	<option value="{0}">{1}</option>", opt.Key, opt.Value);
            html.AppendLine();
        }
        
        html.AppendLine("</select>");
        
        return html.ToString();
    }

    以上,

        if(includeUnknown)
        {
            html.AppendLine(" <option>Unknown</option>");
        }

    可以对如上语句进行封装,继续扩展StringBuilder.

    public static class StringBuilderExtensions
    {
        public static StringBuilder AppendFormattedLine(this StringBuilder @this, string format, prams object[] args) => @this.AppendFormat(format, args).AppendLine();
        
        public static StringBuilder AppendLineWhen(this StringBuilder @this, Func<bool> predicate, string value) =>
        predicate() 
            ? @this.AppendLine(value)
            : @this;
        
    }
    
    private static string BuilderSelectBox(IDictionary<int, string> options, string id, bool includeUnknown)
    {
        var html = new StringBuilder()
            .AppendFormattedLine("<select id="{0}" name="{0}">", id)
            .AppendLineWhen(() => includeUnknown, "	<option>Unknown</option>");
    
        
        foreach(var opt in options)
        {
            html.AppendFormattedLine("	<option value="{0}">{1}</option>", opt.Key, opt.Value);
        }
        
        html.AppendLine("</select>");
        
        return html.ToString();
    }

    public static class StringBuilderExtensions
    {
        public static StringBuilder AppendFormattedLine(this StringBuilder @this, string format, prams object[] args) => @this.AppendFormat(format, args).AppendLine();
        
        public static StringBuilder AppendLineWhen(this StringBuilder @this, Func<bool> predicate, string value) =>
        predicate() 
            ? @this.AppendLine(value)
            : @this;
            
        public static StringBuilder AppendWhen(this StringBuilder @this, Func<bool> predicate, Func<StringBuilder, StringBuilder> fn) =>
        predicate()
            ? fn(@this)
            : @this;
        
    }
    
    private static string BuilderSelectBox(IDictionary<int, string> options, string id, bool includeUnknown)
    {
        var html = new StringBuilder()
            .AppendFormattedLine("<select id="{0}" name="{0}">", id)
            .AppendWhen(
                () => includeUnknown,
                sb => sb.AppendLine("	<option>Unknown</option>")
            );
    
        
        foreach(var opt in options)
        {
            html.AppendFormattedLine("	<option value="{0}">{1}</option>", opt.Key, opt.Value);
        }
        
        html.AppendLine("</select>");
        
        return html.ToString();
    }

    以上,

        foreach(var opt in options)
        {
            html.AppendFormattedLine(" <option value="{0}">{1}</option>", opt.Key, opt.Value);
        }
        
    对遍历语句进行封装,扩展StringBuilder,最终:   

    public static class StringBuilderExtensions
    {
        public static StringBuilder AppendFormattedLine(this StringBuilder @this, string format, prams object[] args) => @this.AppendFormat(format, args).AppendLine();
        
        public static StringBuilder AppendLineWhen(this StringBuilder @this, Func<bool> predicate, string value) =>
        predicate() 
            ? @this.AppendLine(value)
            : @this;
            
        public static StringBuilder AppendWhen(this StringBuilder @this, Func<bool> predicate, Func<StringBuilder, StringBuilder> fn) =>
        predicate()
            ? fn(@this)
            : @this;
            
        public static StringBuilder AppendSequence<T>(this StringBuilder @this, IEnumerable<T> seq, Func<StringBuilder, T, StringBuilder> fn) => seq.Aggregate(@this, fn);
        
    }
    
    private static string BuilderSelectBox(IDictionary<int, string> options, string id, bool includeUnknown)
    {
        var html = new StringBuilder()
            .AppendFormattedLine("<select id="{0}" name="{0}">", id)
            .AppendWhen(
                () => includeUnknown,
                sb => sb.AppendLine("	<option>Unknown</option>")
            )
            .AppendSequence(options, (sb, opt) => sb.AppendFormattedLine("	<option value="{0}">{1}</option>", opt.Key, opt.Value))
            .AppendLine("</select>")
            .ToString();
    }
  • 相关阅读:
    31天重构学习笔记18. 使用条件判断代替异常
    31天重构学习笔记12. 分解依赖
    31天重构学习笔记2. 移动方法
    .NET 技术社区谈之中文篇
    31天重构学习笔记10. 提取方法
    31天重构学习笔记14. 分离职责
    31天重构学习笔记20. 提取子类
    31天重构学习笔记16. 封装条件
    31天重构学习笔记17. 提取父类
    .NET 技术社区谈之英文篇
  • 原文地址:https://www.cnblogs.com/darrenji/p/5271696.html
Copyright © 2011-2022 走看看