Home > Backend Development > C#.Net Tutorial > Remember a .NET code refactoring (Part 2)

Remember a .NET code refactoring (Part 2)

黄舟
Release: 2017-02-06 14:40:29
Original
1213 people have browsed it

public override bool TryGetMember(GetMemberBinder binder, out object result)
{
            if (!_dictionary.TryGetValue(binder.Name, out result))
            {
                result = null;
                return true;
            }
            var dictionary = result as IDictionary<string, object>;
            if (dictionary != null)
            {
                result = new DynamicJsonObject(dictionary);
                return true;
            }
            var arrayList = result as ArrayList;
            if (arrayList != null && arrayList.Count > 0)
            {
                if (arrayList[0] is IDictionary<string, object>)
result = new List<object>(arrayList.Cast<IDictionary<string, object>>().Select(x => new DynamicJsonObject(x)));
                else
                    result = new List<object>(arrayList.Cast<object>());
            }
            return true;
        }
    }
    #endregion
}
Copy after login

The next step is the GetContent method. The purpose of this method is very simple. It is to assemble the final SMS content based on the template variable parameter key-value pairs passed by the customer and the SMS template content. Previously, this method was hard-coded. , now we need to change it to dynamic acquisition.

Example of text message template content:

【一应生活】您有一件单号为expressNumbers company,已到communityName收发室,请打开一应生活APP“收发室”获取取件码进行取件。
点击下载http://a.app.qq.com/o/simple.jsp?pkgname=com.ening.life
Copy after login

I found that there is a problem with the content of such a template. The variable parameters in the template are directly expressed in English words, and our text message content may have Sometimes there will be English words, so I will add {} to all variable parameters. The modification is as follows:

【一应生活】您有一件单号为{expressNumbers} {company},已到{communityName}收发室,
请打开一应生活APP“收发室”获取取件码进行取件。点击下载http://a.app.qq.com/o/simple.jsp?pkgname=com.ening.life
Copy after login

We need to replace the variable parameters in the SMS template with the values ​​corresponding to the variable parameters based on the object passed by the customer. Then we must first parse the key-value pair information in this object.

  /// 把object对象的属性反射获取到字典列表中
        /// </summary>
        /// <param name="data">object对象</param>
        /// <returns>返回Dictionary(属性名,属性值)列表</returns>
         static Dictionary<string, string> GetProperties(object data)
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();

            Type type = data.GetType();
            string[] propertyNames = type.GetProperties().Select(p => p.Name).ToArray();
            foreach (var prop in propertyNames)
            {
                object propValue = type.GetProperty(prop).GetValue(data, null);
                string value = (propValue != null) ? propValue.ToString() : "";
                if (!dict.ContainsKey(prop))
                {
                    dict.Add(prop, value);
                }
            }
            return dict;
        }
Copy after login

The next step is to match the text message template content through regular expressions.

  /// 多个匹配内容
        /// </summary>
        /// <param name="sInput">输入内容</param>
        /// <param name="sRegex">表达式字符串</param>
        /// <param name="sGroupName">分组名, ""代表不分组</param>
        static List<string> GetList(string sInput, string sRegex, string sGroupName)
        {
            List<string> list = new List<string>();
            Regex re = new Regex(sRegex, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
            MatchCollection mcs = re.Matches(sInput);
            foreach (Match mc in mcs)
            {
                if (sGroupName != "")
                {
                    list.Add(mc.Groups[sGroupName].Value);
                }
                else
                {
                    list.Add(mc.Value);
                }
            }
            return list;
        }
        public static string ReplaceTemplate(string template, object data)
        {
            var regex = @"\{(?<name>.*?)\}";
            List<string> itemList = GetList(template, regex, "name"); //获取模板变量对象
            Dictionary<string, string> dict = GetProperties(data);
            foreach (string item in itemList)
            {
                //如果属性存在,则替换模板,并修改模板值
                if (dict.ContainsKey(item))
                {
                    template = template.Replace("{"+item+"}", dict.First(x => x.Key == item).Value);
                }
            }
            return template;
}
Copy after login

In this way, the object passed by the customer is decoupled from our parsing code. The object passed by the customer no longer depends on our code implementation, but depends on the configuration of the template content in our data table.

I have written these methods, and I will run a unit test to verify whether it has the effect I want. Unfortunately, unit testing is not used in this project at all. There is no way, I Create a unit test yourself

[TestClass]
    public class MatchHelperTest
    {
        [TestMethod]
        public void ReplaceTemplate()
        {
            //模板文本
            var template = "【一应生活】您有一件单号为{expressNumbers} {company},已到{communityName}收发室,
            请打开一应生活APP“收发室”获取取件码进行取件。点击下载http://a.app.qq.com/o/simple.jsp?pkgname=com.ening.life";
            //数据对象
            var data = new { expressNumbers = "2016", company = "长城", communityName = "长怡花园"};
            string str = "【一应生活】您有一件单号为2016 长城,已到长怡花园收发室,
            请打开一应生活APP“收发室”获取取件码进行取件。点击下载http://a.app.qq.com/o/simple.jsp?pkgname=com.ening.life";
            string str1=MatchHelper.ReplaceTemplate(template, data);

            Assert.AreEqual(str1,str);

            //重复标签的测试
            template = "【一应生活】您有一件单号为{expressNumbers} {company},已到{communityName}收发室,单号:{expressNumbers}";
            str = "【一应生活】您有一件单号为2016 长城,已到长怡花园收发室,单号:2016";
            str1=MatchHelper.ReplaceTemplate(template, data);
            Assert.AreEqual(str1, str);
        }
    }
Copy after login

Speaking of unit testing, I believe it is not used in many companies for too many reasons. I also think that if the business is simple, there is no need to write unit tests. Too many entrepreneurial companies in China are very fast on their projects. If it is said that writing unit tests does not take time, it is definitely a lie. As for writing unit tests, it can improve the efficiency of the project. Development efficiency and reduction of rework rate. Personally, it’s hard to say this, because even if you don’t write unit tests, you can still make up for it through many other means. Personal opinion, don’t criticize.

Next, modify the GetContent method as follows:

public string GetContent(dynamic messageContext)
{
            string strMsg = "";
            string TypeCode = string.IsNullOrEmpty(messageContext.serviceCode) ? "001" : messageContext.serviceCode;
            string channel = messageContext.channel;
            try{
var Module = unitOfWork.MessageModule.Get(c => c.Type == channel && c.TypeNo == TypeCode).FirstOrDefault();
                if (!string.IsNullOrEmpty(Module.Content))
                {
                    var content = Module.Content;
                    strMsg = MatchHelper.ReplaceTemplate(content, messageContext);
                }
                return strMsg;
            }
            catch (Exception ex)
            {
                strMsg = ex.Message;
            }
            return strMsg;
        }
Copy after login

(Besides: Let me complain about the previous variable naming, MessageContext messageContext and string messageContent, they look too similar, I reconstructed them at the beginning Sometimes I made a mistake. It is recommended not to use similar variable names in the same method to avoid confusion. Damn it, I was cheated again. I was angry and unbearable, so I renamed it decisively.)


It turns out that the controller calls the business logic code directly like this

MessageModuleBusiness message
ModuleBusiness = new MessageModuleBusiness()
Copy after login

It depends on the implementation of the concrete class, and we know that the concrete is unstable, and the abstract is To be stable, we should program towards interfaces. Today you are sending text messages, tomorrow you may be sending emails, or adding log records, etc.

public interface IMessageModuleBusiness
{
        /// <summary>
        /// 组装消息内容
        /// </summary>
        /// <param name="messageContext">动态参数对象</param>
        /// <returns>组装后的消息内容</returns>
        string GetContent(dynamic messageContext);
}
Copy after login

Then the calling code is modified to:

private IMessageModuleBusiness message
ModuleBusiness = new MessageModuleBusiness();
Copy after login

The final externalMerchantSendMessage code is:

    /// 外部商户发送信息
        public ActionResult externalMerchantSendMessage()
        {
            try
            {
                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));
                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);
            }
            catch (Exception ex)
            {
                return Json(new Result<string>()
                {
                    resultCode = ((int)ResultCode.failure).ToString(),
                    resultMsg = "发送失败"+ex.Message
                }, JsonRequestBehavior.AllowGet);
            }
        }
Copy after login

In this case, it will be convenient even if it is decoupled again through reflection or IOC in the future.


Okay, through this step-by-step reconstruction, I have encapsulated the change points without modifying the original table structure and without affecting customer calls. , when the customer's template parameter variables change, there is no need to change the code, only the template content in the table needs to be modified.

Remember a .NET code refactoring (Part 2)

When refactoring, it is a very good habit to draw class diagrams. The code structure is clear at a glance. Here I attach the class diagram.

The above is the content of a .NET code reconstruction (Part 2). For more related content, please pay attention to the 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