当前位置: 代码迷 >> C# >> 求一段代码的优化,多谢!
  详细解决方案

求一段代码的优化,多谢!

热度:78   发布时间:2016-05-05 03:36:53.0
求一段代码的优化,谢谢!!
现在我有两个方法,内容基本一样现在我想优化这段代码,using{}会变的,有什么办法可以优化?
我不想新建一个方法,然后传类似sp_wechat_up_vip参数进去.因为using里面的方法有可能会变化好大.
是不是可以用委托?求大神给点意见!


public string InsVip(string content)
        {
            Competence.checkOK();
            KS.Data.JSON json = new KS.Data.JSON();
            json.Add("state", 0);
            json.Add("msg", "ok");

            try
            {
                content = RSALibrary.RSA.Decrypt(content);
                json = KS.Data.JSON.Decode(content);
                string[] ay = new string[] { "name", "gender", "birthday", "mobile", "region" };
                json.Only(ay);
                json.Required(ay);

                using (KS.Data.MicrosoftSQL db = new KS.Data.MicrosoftSQL())
                {
                    json = db.Procedure2JSON("sp_wechat_ins_vip", json)[0];
                    json["state"] = 1;
                }
            }
            catch (Exception ex)
            {
                json["msg"] = ex.Message;
            }

            Competence.markLog(content, json);
            return json.ToString();
        }


public string UpVip(string content)
        {
            Competence.checkOK();
            KS.Data.JSON json = new KS.Data.JSON();
            json.Add("state", 0);
            json.Add("msg", "ok");

            try
            {
                content = RSALibrary.RSA.Decrypt(content);
                json = KS.Data.JSON.Decode(content);
                string[] ay = new string[] { "vno", "name", "gender", "birthday", "mobile", "region" };
                json.Only(ay);
                json.Required(ay);

                using (KS.Data.MicrosoftSQL db = new KS.Data.MicrosoftSQL())
                {
                    json = db.Procedure2JSON("sp_wechat_up_vip", json)[0];
                    json["state"] = 1;
                }
            }
            catch (Exception ex)
            {
                json["msg"] = ex.Message;
            }

            Competence.markLog(content, json);
            return json.ToString();
        }

------解决思路----------------------
将ay 抽出来作为参数传入,sp_wechat_up_vip作为参数传入……
------解决思路----------------------
private string CommonVip(string content,string[] ay,string procName)
        {
            Competence.checkOK();
            KS.Data.JSON json = new KS.Data.JSON();
            json.Add("state", 0);
            json.Add("msg", "ok");
 
            try
            {
                content = RSALibrary.RSA.Decrypt(content);
                json = KS.Data.JSON.Decode(content);
                //string[] ay = new string[] { "name", "gender", "birthday", "mobile", "region" };
                json.Only(ay);
                json.Required(ay);
 
                using (KS.Data.MicrosoftSQL db = new KS.Data.MicrosoftSQL())
                {
                    json = db.Procedure2JSON(spName, json)[0];
                    json["state"] = 1;
                }
            }
            catch (Exception ex)
            {
                json["msg"] = ex.Message;
            }
 
            Competence.markLog(content, json);
            return json.ToString();
        }
public string UpVip(string content)
{
        return this.CommonVip(content,new string[] { "name", "gender", "birthday", "mobile", "region" },"sp_wechat_ins_vip");
}
public string InsVip(string content)
{
return this.CommonVip(content,new string[] { "vno", "name", "gender", "birthday", "mobile", "region" };,"sp_wechat_up_vip");
}
  相关解决方案