Guest User

Untitled

a guest
Mar 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. var gatewayFactory = new GatewayFactory();
  2. var masterClient = gatewayFactory.CreateMasterClient("someBaseAddress");
  3.  
  4. public IMasterService CreateMasterClient(string baseAddress)
  5. {
  6. return (MasterServiceClient)CreateHttpClient<IMasterService>($"{baseAddress}api/master/");
  7. }
  8.  
  9. private DalServiceBaseClient CreateHttpClient<T>(string baseAddress)
  10. {
  11. var httpClient = new HttpClient
  12. {
  13. BaseAddress = new Uri(baseAddress)
  14. };
  15.  
  16. if (typeof(T) == typeof(IMasterService))
  17. {
  18. return new MasterServiceClient(httpClient);
  19. }
  20. if (typeof(T) == typeof(ISlaveService))
  21. {
  22. return new SlaveServiceClient(httpClient);
  23. }
  24. if (typeof(T) == typeof(IRoleService))
  25. {
  26. return new RoleServiceClient(httpClient);
  27. }
  28.  
  29. // A lot more ifs..
  30.  
  31. throw new ArgumentException("No type exists for the provided interface");
  32. }
  33.  
  34. IDictionary<Type, Type> mappings; //Populated with all interface to implementation types
  35.  
  36. private DalServiceBaseClient CreateHttpClient<T>(string baseAddress) {
  37. var httpClient = new HttpClient {
  38. BaseAddress = new Uri(baseAddress)
  39. };
  40. var implementationType = mappings[typeof(T)];
  41. if (implementationType == null)
  42. throw new ArgumentException("No type exists for the provided interface");
  43.  
  44. return (DalServiceBaseClient)Activator.CreateInstance(implementationType, httpClient);
  45. }
  46.  
  47. mappings = new Dictionary<Type, Type>();
  48. mappings[typeof(IMasterService)] = typeof(MasterServiceClient);
  49. mappings[typeof(ISlaveService)] = typeof(SlaveServiceClient);
  50.  
  51. //...other types;
  52.  
  53. IDictionary<Type, Func<HttpClient, DalServiceBaseClient>> mappings; //Populated with all interface to implementation types
  54.  
  55. private DalServiceBaseClient CreateHttpClient<T>(string baseAddress) {
  56. var httpClient = new HttpClient {
  57. BaseAddress = new Uri(baseAddress)
  58. };
  59. var implementationFactory = mappings[typeof(T)];
  60. if (implementationFactory == null)
  61. throw new ArgumentException("No type exists for the provided interface");
  62.  
  63. return implementationFactory(httpClient);
  64. }
  65.  
  66. mappings = new Dictionary<Type, Func<HttpClient, DalServiceBaseClient>>();
  67. mappings[typeof(IMasterService)] = (httpClient) => new MasterServiceClient(httpClient);
  68. mappings[typeof(ISlaveService)] = (httpClient) => new SlaveServiceClient(httpClient);
  69.  
  70. //...other types;
  71.  
  72. private HttpClient CreateHttpClient(string baseAddress)
  73. {
  74. return new HttpClient()
  75. {
  76. BaseAddress = new Uri(baseAddress)
  77. };
  78. }
  79.  
  80. public IMasterService CreateMasterClient(string baseAddress)
  81. {
  82. return new MasterServiceClient(
  83. CreateHttpClient($"{baseAddress}api/master/")
  84. );
  85. }
Add Comment
Please, Sign In to add comment