Let's illustrate with some code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void OldBadWay() | |
{ | |
//Warning! This is bad code! Don't use this! | |
SoapExample.LocalLibrary locallib = new SoapExample.LocalLibrary(); | |
remoteSoap.RemoteLibrarySoapClient remoteclient = new remoteSoap.RemoteLibrarySoapClient(); | |
var ssCall = new RemoteSSClient(); | |
Console.Clear(); | |
DateTime start; | |
DateTime end; | |
Console.ForegroundColor = ConsoleColor.Green; | |
start = DateTime.UtcNow; | |
Console.WriteLine("{0}: {1} + {2} = ", "Local", 1, 2); | |
var local_Sum = locallib.AddThis(1, 2); | |
Console.WriteLine("{0} Answer: {1}", "Local", local_Sum); | |
end = DateTime.UtcNow; | |
Console.WriteLine("{0} Ticks: {1}", "Local", GetDiff(start, end)); | |
Console.WriteLine("-----"); | |
Console.ForegroundColor = ConsoleColor.Red; | |
start = DateTime.UtcNow; | |
Console.WriteLine("{0}: {1} + {2} = ", "RemoteXML", 1, 3); | |
var remoteXML_Sum = remoteclient.AddThis(1, 3); | |
Console.WriteLine("{0} Answer: {1}", "RemoteXML", remoteXML_Sum); | |
end = DateTime.UtcNow; | |
Console.WriteLine("{0} Ticks: {1}", "RemoteXML", GetDiff(start, end)); | |
Console.WriteLine("-----"); | |
Console.ForegroundColor = ConsoleColor.Cyan; | |
start = DateTime.UtcNow; | |
Console.WriteLine("{0}: {1} + {2} = ", "RemoteSS", 1, 4); | |
var remoteSS_Sum = remoteclient.AddThis(1, 4); | |
Console.WriteLine("{0} Answer: {1}", "RemoteSS", remoteSS_Sum); | |
end = DateTime.UtcNow; | |
Console.WriteLine("{0} Ticks: {1}", "RemoteSS", GetDiff(start, end)); | |
Console.WriteLine("-----"); | |
} |
I know, I know. It's bad code, but the save button isn't a lock button, so we can refactor. Furthermore, by using the dynamic keyword, we can refactor to this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void RunTest(dynamic service,string name, int num1, int num2, ConsoleColor color) | |
{ | |
Console.ForegroundColor = color; | |
DateTime start = DateTime.UtcNow; | |
Console.WriteLine("{0}: {1} + {2} = ",name, num1, num2); | |
var local = service.AddThis(num1, num2); | |
Console.WriteLine("{0} Answer: {1}",name, local); | |
DateTime end = DateTime.UtcNow; | |
Console.WriteLine("{0} Ticks: {1}",name, GetDiff(start, end)); | |
Console.WriteLine("-----"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RunTest(locallib, "Local Assembly", 100, 100, ConsoleColor.Green); | |
RunTest(remoteclient, @"SOAP/XML", 100, 100, ConsoleColor.Red); | |
RunTest(ssCall, "Service Stack", 100, 100, ConsoleColor.Cyan); |
Keep in mind, none of these objects inherit from the same base class or implement the same interface. By convention I made sure that the AddThis method would be called the same way every time.
Nothing magical here, but it might get you out of a bind. I'm not sure that I'd want to put this in production code, at least not for every long, but it works.
No comments:
Post a Comment