These days, take over a colleague's code, about the development of micro-letter interface, that a heap of if, see Brother egg pain, this problem is also a lot of beginners easy to make, so specially to write this refactoring.
Let's take a look at the problem with this code, if the code block logic in the if else inside is hard to change, making it reusable 0 and difficult to read. Of course, if there are only one or two, or 3, this is not a very good question to write.
But if more, this kind of code will make maintenance become difficult.
if (Strmsgtype = = "Text") {textcontentclient = Rootelement.selectsinglenode ("Content").
InnerText;
strresult = Setmsgtype_text (Strclientname, textcontentclient, DB, strServerName, Identity);
System.Diagnostics.Trace.WriteLine (strresult);
Return Content (strresult); else if (Strmsgtype = = "Event") {string eventtype = Rootelement.selectsinglenode ("event").
Innertext.tolower ();
if (EventType = = "Subscribe") {string keycode = ""; if (Rootelement.selectsinglenode ("Eventkey")!= null) KeyCode = Rootelement.selectsinglenode ("Eventkey").
Innertext.tolower ();
strresult = Formateventsubscribe (keycode);
Recordreplymessage ();
Return Content (strresult); else if (EventType = = "Scan") {string keycode = Rootelement.selectsinglenode ("Eventkey").
Innertext.tolower ();
var outletname = "Welcome attention";
var outletdb = shoppingcontext.createinstance (Identity);var Outletmodel = OutletDB.Outlets.FirstOrDefault (o => O.sceneid = = Int32.Parse (keycode));
if (Outletmodel!= null) Outletname + = Outletmodel.name;
Return Content (Gettexttemp (Strclientname, strServerName, Outletname)); else if (EventType = = "click") {string keycode = Rootelement.selectsinglenode ("Eventkey").
Innertext.tolower ();
strresult = Fomatmenumessage (keycode);
Return Content (strresult); else if (EventType = = "Unsubscribe") {var subids = db. Replyrecords.where (r => R.fromopenid = = this. Clientid.tostring () && R.emesstype = = EEventType.Subscribe.ToString () && r.keyword!= null). Select (o => o.keyword).
ToArray (); var unsubids = db. Replyrecords.where (r => R.fromopenid = = this. Clientid.tostring () && R.emesstype = = EEventType.Unsubscribe.ToString () && r.keyword!= null). Select (o => o.keyword).
ToArray (); var sencesid =""; foreach (var k in subids) {if (!unsubids.contains (k)) {this.
Replymodel.keyword = k;
Break }} this.
Replymodel.emesstype = EEventType.Unsubscribe.ToString ();
Recordreplymessage (); } else if (strmsgtype.tolower () = = "Location") {string strlocation_x = Rootelement.selectsinglenode ("location_x"). I
Nnertext; String strlocation_y = Rootelement.selectsinglenode ("location_y").
InnerText; strresult = formatoutletlbs (double. Parse (strlocation_x), double.
Parse (strlocation_y), 10);
strresult = formattextmessage (strlocation_x + "|" + strlocation_y);
Return Content (strresult); else if (strmsgtype.tolower () = = "Image") {string strimgurl = Rootelement.selectsinglenode ("Picurl").
InnerText; }
A better way to do this is to extract the code from the block of statements and write it as a function, as shown in the following:
public class Messageprocesser {public replymessage Process (string xml) {var msg = postm Essage.
FromXml (XML); Switch (MSG. Msgtype) {case Models.PostMessageType.Event:var EventType = (Eventmessa GE) msg).
Event; Switch (eventtype) {case EventType.Click:return
Processclickevent ((clickevent) msg);
Case EventType.Location:return Processlocationevent ((locationevent) msg);
Case EventType.Scan:return Processscanevent ((scanevent) msg);
Case EventType.Subscribe:return Processsubscribeevent ((subscribeevent) msg);
Case EventType.Unsubscribe:return Processunsubscribeevent ((unsubscribeevent) msg); } break;
Case Models.PostMessageType.Image:return Processimagemessage ((imagemessage) msg);
Case Models.PostMessageType.Link:return Processlinkmessage ((linkmessage) msg);
Case Models.PostMessageType.Location:return Processlocationmessage ((locationmessage) msg);
Case Models.PostMessageType.Text:return Processtextmessage ((textmessage) msg);
Case Models.PostMessageType.Video:return Processvideomessage ((videomessage) msg);
Case Models.PostMessageType.Voice:return Processvoicemessage ((voicemessage) msg);
return null; Protected virtual Replymessage processclickevent (clickevent msg) {return defaultproces
S (msg); Protected virtual Replymessage processlocationevent (LoCationevent msg) {return defaultprocess (msg); Protected virtual Replymessage processscanevent (scanevent msg) {return defaultprocess (
msg); Protected virtual Replymessage processsubscribeevent (subscribeevent msg) {return Defau
Ltprocess (msg); Protected virtual Replymessage processunsubscribeevent (unsubscribeevent msg) {return D
Efaultprocess (msg); Protected virtual Replymessage processimagemessage (imagemessage msg) {return DEFAULTPR
Ocess (msg); Protected virtual Replymessage processlinkmessage (linkmessage msg) {return Defaultproc
ESS (MSG); Protected virtual Replymessage processlocationmessage (locationmessage msg) {return Def
Aultprocess (msg); } protected Virtual Replymessage ProcesstextmessagE (TextMessage msg) {return defaultprocess (msg); Protected virtual Replymessage processvideomessage (videomessage msg) {return DEFAULTPR
Ocess (msg); Protected virtual Replymessage processvoicemessage (voicemessage msg) {return DEFAULTPR
Ocess (msg); Protected virtual Replymessage defaultprocess (PostMessage msg) {var reply = new Textre
Ply (MSG); if (Msg. Msgtype = = postmessagetype.event) {reply. Content = string. Format (' {0} ' not processed. ', ((eventmessage) msg).
Event); else {reply. Content = string. Format (' {0} ' is ' not processed. ', MSG.
Msgtype);
return reply; }
}