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"). InnerText;
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 here:
public class Messageprocesser
{
Public replymessage Process (string xml)
{
var msg = postmessage.fromxml (XML);
Switch (MSG. Msgtype)
{
Case Models.PostMessageType.Event:
var EventType = ((eventmessage) 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 defaultprocess (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 defaultprocess (msg);
}
Protected virtual Replymessage processunsubscribeevent (unsubscribeevent msg)
{
return defaultprocess (msg);
}
Protected virtual Replymessage processimagemessage (imagemessage msg)
{
return defaultprocess (msg);
}
Protected virtual Replymessage processlinkmessage (linkmessage msg)
{
return defaultprocess (msg);
}
Protected virtual Replymessage processlocationmessage (locationmessage msg)
{
return defaultprocess (msg);
}
Protected virtual Replymessage processtextmessage (textmessage msg)
{
return defaultprocess (msg);
}
Protected virtual Replymessage processvideomessage (videomessage msg)
{
return defaultprocess (msg);
}
Protected virtual Replymessage processvoicemessage (voicemessage msg)
{
return defaultprocess (msg);
}
Protected virtual Replymessage defaultprocess (PostMessage msg)
{
var reply = new textreply (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;
}
}