Last active
July 9, 2017 13:57
Calling static methods from a WCF service
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
// The WCF service class | |
public class Service1 : IService1 | |
{ | |
public string GetData(int value) | |
{ | |
return ServiceLogic.GetData(value); | |
} | |
} | |
// Class with static methods and a static instance of DB/service instances | |
public class ServiceLogic | |
{ | |
private static Context context; | |
internal static Context Context | |
{ | |
get | |
{ | |
if (context == null) | |
{ | |
initializeContext(); | |
} | |
return context; | |
} | |
} | |
// Used for unit testing. Must be set back to null or previous value after test or before next test. | |
internal static void InjectContext(Context context) | |
{ | |
ServiceLogic.context = context; | |
} | |
static void initializeContext() | |
{ | |
context = new Context(); | |
} | |
public static string GetData(int value) | |
{ | |
return Context.GetData(value); | |
} | |
} | |
public class Context | |
{ | |
private DataLayer dataLayer; | |
public Context() | |
{ | |
this.dataLayer = new DataLayer(); | |
} | |
public string GetData(int value) | |
{ | |
return this.dataLayer.GetData(value); | |
} | |
} | |
public class DataLayer | |
{ | |
public string GetData(int value) | |
{ | |
return string.Format("You entered: {0}", value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment