code_junkie

Model - View - ViewModel & WCF - is WCF the model?

Nov 13th, 2011
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. List<Sam.Alyza.WcfInterface.Website> rc = null;
  2. Service<Sam.Alyza.WcfInterface.IServiceWebsites>.Use(alyzaSvc =>
  3. {
  4. rc = new List<Sam.Alyza.WcfInterface.Website>(alyzaSvc.GetSites());
  5. });
  6.  
  7. List<Sam.Alyza.WcfInterface.Website> rc = new List<Sam.Alyza.WcfInterface.Website>(_datamodel.GetSites());
  8.  
  9. public delegate void UseServiceDelegate<T>(T proxy);
  10.  
  11. public static class Service<T>
  12. {
  13. public static ChannelFactory<T> _channelFactory;
  14.  
  15. public static void Use(UseServiceDelegate<T> codeBlock)
  16. {
  17. if (_channelFactory == null)
  18. _channelFactory = new ChannelFactory<T>("AlyzaServiceEndpoint");
  19. IClientChannel proxy = (IClientChannel)_channelFactory.CreateChannel();
  20. bool success = false;
  21. try
  22. {
  23. codeBlock((T)proxy);
  24. proxy.Close();
  25. success = true;
  26. }
  27. finally
  28. {
  29. if (!success)
  30. {
  31. proxy.Abort();
  32. }
  33. }
  34. }
  35. }
  36.  
  37. Service<Sam.Alyza.WcfInterface.IServiceWebsites>.Use(alyzaSvc =>
  38. {
  39. rc = new List<Sam.Alyza.WcfInterface.Website>(alyzaSvc.GetSites());
  40. });
  41.  
  42. public class MockWebsiteService : WcfInterface.IServiceWebsites
  43. {
  44. internal List<Sam.Alyza.WcfInterface.Website> _websites = new List<Sam.Alyza.WcfInterface.Website>();
  45. internal int _GetSitesCallCount;
  46.  
  47. IEnumerable<Sam.Alyza.WcfInterface.Website> Sam.Alyza.WcfInterface.IServiceWebsites.GetSites()
  48. {
  49. _GetSitesCallCount++;
  50. return _websites;
  51. }
  52. }
  53.  
  54. public static class Service<T>
  55. {
  56. #if DEBUG
  57. public static T DebugOverride = default(T);
  58. #endif
  59. public static ChannelFactory<T> _channelFactory;
  60.  
  61. public static void Use(UseServiceDelegate<T> codeBlock)
  62. {
  63. #if DEBUG
  64. if (!Object.Equals(DebugOverride, default(T)))
  65. {
  66. codeBlock(DebugOverride);
  67. return;
  68. }
  69. #endif
  70. if (_channelFactory == null)
  71. _channelFactory = new ChannelFactory<T>("AlyzaServiceEndpoint");
  72. IClientChannel proxy = (IClientChannel)_channelFactory.CreateChannel();
  73. bool success = false;
  74. try
  75. {
  76. codeBlock((T)proxy);
  77. proxy.Close();
  78. success = true;
  79. }
  80. finally
  81. {
  82. if (!success)
  83. {
  84. proxy.Abort();
  85. }
  86. }
  87. }
  88. }
  89.  
  90. MockWebsiteService mockmodel = new MockWebsiteService();
  91. Service<WcfInterface.IServiceWebsites>.DebugOverride = mockmodel;
  92. // run my tests here
  93.  
  94. interface IClientFactory
  95. {
  96. TClient CreateClient<TClient>();
  97. }
  98.  
  99. class ClientFactory : IClientFactory
  100. {
  101. TClient CreateClient<TClient>()
  102. {
  103. var channelFactory = new ChannelFactory<TClient>("AlyzaServiceEndpoint");
  104. var proxy = (TClient)channelFactory.CreateChannel();
  105. return proxy;
  106. }
  107. }
  108.  
  109. public ViewModel
  110. {
  111. public ViewModel(IClientFactory clientFactory)
  112. {
  113. _clientFactory = clientFactory;
  114. }
  115.  
  116. private void DoWcfStuff()
  117. {
  118. using (var proxy = _clientFactory.CreateClient<IClientChannel>())
  119. {
  120. var result = proxy.GetThings();
  121. }
  122. }
  123. }
  124.  
  125. public ViewModelTests
  126. {
  127. public void Setup()
  128. {
  129. _mockFactory = new MockClientFactory();
  130. _viewModel = new ViewModel(_mockFactory);
  131. }
  132.  
  133. [Test]
  134. public void Test()
  135. {
  136. var testResult = new Result();
  137. var mockClient = _mockFactory.CreateClient<IClientChannel>();
  138.  
  139. mockClient.SetResultForGetThings(testResult);
  140.  
  141. // put the viewmodel through its paces.
  142. }
  143.  
  144. private class MockClientFactory : IClientFactory
  145. {
  146. MockClient _mockClient;
  147.  
  148. public MockClientFactory()
  149. {
  150. _mockClient = new MockClient();
  151. }
  152.  
  153. public TClient CreateClient<TClient>()
  154. {
  155. if (typeof(TClient) == typeof(IClientChannel))
  156. {
  157. return _mockClient;
  158. }
  159. }
  160. }
  161.  
  162. private class MockClient : IClientChannel
  163. {
  164. void SetupGetThingsResult(Result result)
  165. {
  166. _result = result;
  167. }
  168.  
  169. Result GetThings()
  170. {
  171. return _result;
  172. }
  173. }
  174. }
Add Comment
Please, Sign In to add comment