C#链式编程

前言

昨天我写了一段这样的一段代码。

     var email = Email
                .From("1075094220@qq.com")
                .To("1075094220@qq.com")
                .CC("1075094220@qq.com")
                .Subject("邮件标题")
                .Body("<h1 align=\"center\">黑哥</h1><p>黑哥给你发来了消息</p>",true);

然后我说该库是使用链式编程,昨天就有小伙伴私信我什么是链式编程?.

链式编程,顾名思义 就是一条链子能一直链下去

链式编程的原理就是返回一个this对象,就是返回本身,达到链式效果。

像我们经常用的StringBuilder拼接字符串时的写法就是使用的链式编程

     StringBuilder sb = new StringBuilder();
            sb.Append("测试")
              .Append("测试")
              .Append("测试")
              .Append("测试")
              .Append("测试");

我们可以看下源码

       [SecuritySafeCritical]
        [__DynamicallyInvokable]
        public unsafe StringBuilder Append(string value)
        {
            if (value != null)
            {
                char[] chunkChars = m_ChunkChars;
                int chunkLength = m_ChunkLength;
                int length = value.Length;
                int num = chunkLength + length;
                if (num < chunkChars.Length)
                {
                    if (length <= 2)
                    {
                        if (length > 0)
                        {
                            chunkChars[chunkLength] = value[0];
                        }

                        if (length > 1)
                        {
                            chunkChars[chunkLength + 1] = value[1];
                        }
                    }
                    else
                    {
                        fixed (char* smem = value)
                        {
                            fixed (char* dmem = &chunkChars[chunkLength])
                            {
                                string.wstrcpy(dmem, smem, length);
                            }
                        }
                    }

                    m_ChunkLength = num;
                }
                else
                {
                    AppendHelper(value);
                }
            }

            return this;
        }

其实就是在返回的时候 返回当前的对象 。

例子

我定义了一个使用链式编程的UserInforMation类,有两个属性和三个方法

 public  class UserInforMation
    {
        public string UserName { get; set; }
        public int Age { get; set; }

        public UserInforMation SetNameAndAge(string name,int age)
        {
            UserInforMation userInforMation = new UserInforMation();
            userInforMation.UserName = name;
            userInforMation.Age = age;
            return userInforMation;
        }

        public  UserInforMation OutputName()
        {
            Console.WriteLine(this.UserName);
            return this;
        }
        public UserInforMation OutputAge()
        {
            Console.WriteLine(this.Age);
            return this;
        }

每个方法都是返回的当前对象。

我们用链式编程来写一段使用赋值方法和输出方法,首先我们肯定要实例化UserInforMation然后再赋值,最后调用方法.

   new UserInforMation()
                .SetNameAndAge("张三", 14)
                .OutputAge()
                .OutputName();

看到这里可能有的小伙伴会问像有一些框架根本就不用new,直接使用类名调用。那大家肯定就知道要实现这样的功能,肯定要在UserInforMation类里面写一个静态方法,返回一个UserInforMation实例这样我们就实现了类名来调用了,

   public  class UserInforMation
    {
      重复代码不copy了

        public static UserInforMation Builder()
        {
            return new UserInforMation();
        }
    }

修改后的代码就可以直接使用类名来调用了

UserInforMation .Builder()
                .SetNameAndAge("张三", 14)
                .OutputAge()
                .OutputName();

了解了链式编程,那链式编程有什么好处呢?个人感觉链式代码更加精炼简洁易读.

特别是在开发框架,或者写通用库的时候链式编程就用的多了,如果只是进行业务开发,在使用链式编程不就是有点脱了裤子放屁吗。