Remember a .NET code refactoring (Part 1)

黄舟
Release: 2017-02-06 14:37:00
Original
1196 people have browsed it

Requirements: This is the case. To develop a template for sending text messages, different customers may use different templates, and the variable parameters used by different customers are also different.


For emergency purposes, a text message template function for sending text messages has been completed online. The text message template table has also been created, and a new record has been added to the table. I only need to make an interface for adding, deleting, modifying and checking text message templates. It seems that my task is quite simple. An experienced driver should know that he has made a mess.


The picture below shows the originally created table

Remember a .NET code refactoring (Part 1)

The SQL creation script is as follows:

Remember a .NET code refactoring (Part 1)

Before this, an API interface for sending text messages has been developed for customers to call. In other words, the caller (customer) will not modify the code, only me. to modify. Although I am extremely reluctant to take on half-finished tasks, I have no choice but to start all development tasks from scratch.


The entity class code is as follows:

Remember a .NET code refactoring (Part 1)

DOT class:

Remember a .NET code refactoring (Part 1)

##This is the previous code, the business entity class MessageModuleBusiness.cs code is as follows:

public class MessageModuleBusiness : GenericRepository<Model.MessageModule>
    {
        private UnitOfWork.UnitOfWork unitOfWork = new UnitOfWork.UnitOfWork();
        /// 获取模版内容
        public string GetContent(MessageContext messageContext)
        {
            string messageContent = "";
            string TypeCode = string.IsNullOrEmpty(messageContext.serviceCode) ? "001" : messageContext.serviceCode;
            try
            {
                var Module = unitOfWork.MessageModule.Get(c => c.Type == messageContext.channel && c.TypeNo == TypeCode).FirstOrDefault();
                //Content的内容:【一应生活】您有一件单号为expressNumbers company,
                已到communityName收发室,请打开一应生活APP“收发室”获取取件码进行取件。点击下载http://a.app.qq.com/o/simple.jsp?pkgname=com.ening.life
                if (!string.IsNullOrEmpty(Module.Content))
                {
                    var content = Module.Content;
                    content = content.Replace("company", messageContext.company);
                    content = content.Replace("expressNumbers", messageContext.expressNumbers);
                    content = content.Replace("communityName", messageContext.communityName);
                    content = content.Replace("Id", messageContext.Id);
                    content = content.Replace("receiveTime", messageContext.receiveTime);
                    content = content.Replace("fetchCode", messageContext.fetchCode);
                    messageContent = content;
                }
                return messageContent;
            }
            catch (Exception ex) {}
            return "";
        } 
        #endregion
}
Copy after login

MessageContext class, this is an entity object transmitted and called by the client. There are many dynamic tag variables similar to text messages in the object.

public class MessageContext{
        /// 手机号码
        public string phone { get; set; }
        /// 发送信息
        public string message { get; set; }
        /// 签名
        public string sign { get; set; }
        /// 渠道
        public string channel { get; set; }
        /// 内容
        public string content { get; set; }
        /// 取件码
        public string fetchCode { get; set; }
        /// 快递公司
        public string company { get; set; }
        /// 快递单号
        public string expressNumbers { get; set; }
        /// 社区名称
        public string communityName { get; set; }
        /// 到件时间
        public string receiveTime { get; set; }
        /// 序号
        public string Id { get; set; }
        /// 业务代码
        public string serviceCode { get; set; }
    }
Copy after login

Controller method externalMerchantSendMessage, which is for external calls

    /// 外部商户发送信息
        public ActionResult externalMerchantSendMessage(MessageContext param)
        {
            logger.Info("[externalMerchantSendMessage]param:" + param);
            bool isAuth = authModelBusiness.isAuth(param.channel, param.phone, param.sign);
            if (!isAuth)
            {
                return Json(new Result<string>()
                {
                    resultCode = ((int)ResultCode.NoPermission).ToString(),
                    resultMsg = "签名或无权限访问"
                }, JsonRequestBehavior.AllowGet);
            }
            var meaage = messageModuleBusiness.GetContent(param);

            if (string.IsNullOrEmpty(meaage))
            {
                return Json(new Result<string>()
                {
                    resultCode = ((int)ResultCode.failure).ToString(),
                    resultMsg = "发送失败"
                }, JsonRequestBehavior.AllowGet);
            }
            SMSHelper helper = new SMSHelper();
            helper.SendSMS(meaage, param.phone);
            return Json(new Result<string>()
            {
                resultCode = ((int)ResultCode.success).ToString(),
                resultMsg = "发送成功"
            }, JsonRequestBehavior.AllowGet);
        }
Copy after login

The above are the functions that I have implemented before receiving the development task. It seems that my task is quite simple, but many years of development experience tell me that this needs to be refactored. If I don't care about anything now and just make an interface for adding, deleting, modifying and checking SMS templates, the maintenance people in the future will definitely go crazy. .


Do you see any problem?


This interface method externalMerchantSendMessage is called for all customers, and different customers use different SMS templates, and different templates have different variable parameters. Now all variable parameters are encapsulated in the MessageContext class. The problem is that we cannot determine all variable parameters at once and keep them unchanged.


So, that is to say, once variable parameters need to be added, the code in the MessageContext class must be modified, and the code in the GetContent method is hard-coded and needs to be modified accordingly. . This forms a cycle, constantly adding variable parameters, constantly changing the code, and constantly releasing interface versions...


When I have enough time, I naturally If you are a disciplined programmer, then start refactoring.


Before refactoring, what comes to mind is not various design patterns, but the basic principles of object-oriented design. Various design patterns are like various martial arts routines or moves. Martial arts practitioners should be like Zhang Wuji practicing Tai Chi sword, first learn various routines, and then forget all the routines to master them.


Because moves are dead, people are alive. Every move has its flaws. There is no sure-win move at all. It’s like there is no universal design pattern. Any design All models have shortcomings.


#The core idea of ​​object-oriented design is to encapsulate changes, so first find the change points. From the above analysis, we have discovered the changing point, which is the variable parameters in the SMS template, and these variable parameters are passed by the customer caller, and the parameter variables passed by different customers may be different.


Let’s take a look first, what is passed by the customer? Let's take a look at the customer calling code. There are two calling methods: Get and Post.

function sendMsg() {
            //var appParam ="phone=15914070649&sign=78a7ce797cf757916c2c7675b6865b54&channel=weijiakeji&content=&fetchCode=1
&company=%E9%A1%BA%E4%B8%B0%E5%BF%AB%E9%80%92&expressNumbers=123456&communityName=%E9%95%BF%E5%9F%8E%E4%B8%80%E8%8A%B1%E5%9B%AD&receiveTime=5&Id=1231";
            //Get("/Message/externalMerchantSendMessage?" + appParam, {});

            var data = {
                "phone": "15914070649", "sign": "78a7ce797cf757916c2c7675b6865b54", "channel": "weijiakeji",
                "fetchCode": 1, "company": "%E9%A1%BA%E4%B8%B0%E5%BF%AB%E9%80%92", "Id": "1231"
            };
            Post(&#39;/Message/externalMerchantSendMessage&#39;, data);
        }
        //WebAPI Post方法
        function Post(url, data) {
            $.ajax({
                url: url,
                contentType: "application/json",
                type: "POST",
                dataType: "json",
                async: true,
                cache: false,
                data: JSON.stringify(data),
                success: function (response) {
                    $(&#39;#response&#39;).text(JSON.stringify(response));
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        };
        //// WebApi Get方法
        function Get(url, data) {
            $.ajax({
                url: url,
                contentType: "application/json",
                type: "GET",
                dataType: "json",
                async: true,
                cache: false,
                //data: JSON.stringify(data),
                success: function (response) {
                    $(&#39;#response&#39;).text(JSON.stringify(response));
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        };
Copy after login

It can be seen that what the customer passes is a collection of key-value pairs, which is an object in JSON format. According to the previous code bool isAuth = authModelBusiness.isAuth(param.channel, param.phone, param.sign);, it can be analyzed that there are three parameters that all calling customers must pass, that is: channel, phone, sign, The other parameters are the variable parameters and parameter values ​​of the SMS template.


Then the parameter in the method externalMerchantSendMessage(MessageContext param) is a variable object. Isn't there a dynamic in C# 4.0 that is used to describe mutable objects?

Then the first step is to modify the incoming parameter type. It used to be a hard-coded strongly typed MessageContext. Now it does not rely on this type, but dynamic analysis. Modify the externalMerchantSendMessage method code as follows:

##
dynamic param = null;
                string json = Request.QueryString.ToString();

                if (Request.QueryString.Count != 0) //ajax get请求
                {
                    //兼容旧的客户调用写法,暂时硬编了
                    if (json.Contains("param."))
                    {
                        json = json.Replace("param.", "");
                    }
                    json = "{" + json.Replace("=", ":&#39;").Replace("&", "&#39;,") + "&#39;}";
                }
                else  //ajax Post请求
                {
                    Request.InputStream.Position = 0; //切记这里必须设置流的起始位置为0,否则无法读取到数据
                    json = new StreamReader(Request.InputStream).ReadToEnd();
                }
                var serializer = new JavaScriptSerializer();
                serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
                param = serializer.Deserialize(json, typeof(object));
Copy after login

DynamicJsonConverter is used to convert JSON strings into Object objects. The code is as follows:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

public sealed class DynamicJsonConverter : JavaScriptConverter
{
    public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
    {
        if (dictionary == null)
            throw new ArgumentNullException("dictionary");

        return type == typeof(object) ? new DynamicJsonObject(dictionary) : null;
    }

    public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override IEnumerable<Type> SupportedTypes
    {
        get { return new ReadOnlyCollection<Type>(new List<Type>(new[] { typeof(object) })); }
    }
  #region Nested type: DynamicJsonObject

    private sealed class DynamicJsonObject : DynamicObject
    {
        private readonly IDictionary<string, object> _dictionary;

        public DynamicJsonObject(IDictionary<string, object> dictionary)
        {
            if (dictionary == null)
                throw new ArgumentNullException("dictionary");
            _dictionary = dictionary;
        }

        public override string ToString()
        {
            var sb = new StringBuilder("{");
            ToString(sb);
            return sb.ToString();
        }

        private void ToString(StringBuilder sb)
        {
            var firstInDictionary = true;
            foreach (var pair in _dictionary)
            {
                if (!firstInDictionary)
                    sb.Append(",");
                firstInDictionary = false;
                var value = pair.Value;
                var name = pair.Key;
                if (value is string)
                {
                    sb.AppendFormat("{0}:\"{1}\"", name, value);
                }
                else if (value is IDictionary<string, object>)
                {
                    new DynamicJsonObject((IDictionary<string, object>)value).ToString(sb);
                }
                else if (value is ArrayList)
                {
                    sb.Append(name + ":[");
                    var firstInArray = true;
                    foreach (var arrayValue in (ArrayList)value)
                    {
                        if (!firstInArray)
                            sb.Append(",");
                        firstInArray = false;
                        if (arrayValue is IDictionary<string, object>)
                            new DynamicJsonObject((IDictionary<string, object>)arrayValue).ToString(sb);
                        else if (arrayValue is string)
                            sb.AppendFormat("\"{0}\"", arrayValue);
                        else
                            sb.AppendFormat("{0}", arrayValue);

                    }
                    sb.Append("]");
                }
                else
                {
                    sb.AppendFormat("{0}:{1}", name, value);
                }
            }
            sb.Append("}");
        }
Copy after login

The above is the content of a .NET code reconstruction (Part 1). Please pay attention to more related content. PHP Chinese website (www.php.cn)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!