public interface IServiceProxyOne { void ProcessRequest(string data); }
public interface IServiceProxyTwo { void ProcessRequest(string data); } public class ServiceProxyOne : IServiceProxyOne { public void ProcessRequest(string data) { /* do something.*/} } public class ServiceProxyTwo : IServiceProxyTwo { public void ProcessRequest(string data) { /* do something.*/} } public class MockServiceProxyOne : IServiceProxyOne { public void ProcessRequest(string data) { /* do something.*/} } public class MockServiceProxyTwo : IServiceProxyTwo { public void ProcessRequest(string data) { /* do something.*/} }
Service Factory implementation code is below,
public static class ServiceFactory
{ public static IServiceProxyOne CreateServiceProxyOne() { return GetProxyInstance<IServiceProxyOne, MockServiceProxyOne, ServiceProxyOne>(); } public static IServiceProxyTwo CreateServiceProxyTwo() { return GetProxyInstance<IServiceProxyTwo, MockServiceProxyTwo, ServiceProxyTwo>(); } public static T1 GetProxyInstance<T1, T2, T3>() where T2 : T1, new() where T3 : T1, new() { return IsMockEnvironment() ? (T1)new T2() : (T1)new T2(); } public static bool IsMockEnvironment() { return !default(bool); } }
To test the code we can use below,
static void Main(string[] args)
{ ServiceFactory.CreateServiceProxyOne().ProcessRequest("Service Proxy one test."); ServiceFactory.CreateServiceProxyTwo().ProcessRequest("Service Proxy two test."); }
thanks mohammad.
Few C# and Application Design books from Amazon, | |||||
---|---|---|---|---|---|
Thanks for posting this.
ReplyDeleteSample contracts
Thank you :)
ReplyDelete