我通过抽象一切来解决这个问题。
在表示层我想有一个服务抽象...
public interface IServiceAgent {
Task<SomeResultObject> GetSomething(string myParameter);
}
...抽象是我从网上API希望。主讲人不需要协调请求。演示者不关心数据来自何处。它只知道它需要一些东西并要求它(SoC)。这是服务代理的工作(SRP)。
服务代理实现可能需要调用不同的数据源。包括网页。所以抽象HttpClient
将放松与该实现的耦合。
一个简单的例子像...
public interface IHttpClient {
System.Threading.Tasks.Task<T> GetAsync<T>(string uri) where T : class;
System.Threading.Tasks.Task<T> GetAsync<T>(Uri uri) where T : class;
//...other members as needed : DeleteAsync, PostAsync, PutAsync...etc
}
一些示例实现可以是这样的......
public class MyPresenter {
public MyPresenter(IServiceAgent services) {...}
}
public class MyDefaultServiceAgent : IServiceAgent {
IHttpClient httpClient;
public MyDefaultServiceAgent (IHttpClient httpClient) {
this.httpClient = httpClient;
}
public async Task<SomeResultObject> GetSomething(string myParameter) {
var url = "http://localhost/my_web_api_endpoint?q=" + myParameter;
var result = await httpClient.GetAsync<SomeResultObject>(url);
return result;
}
}
public class MyDefaultHttpClient : IHttpClient {
HttpClient httpClient; //The real thing
public MyDefaultHttpClient() {
httpClient = createHttpClient();
}
/// <summary>
/// Send a GET request to the specified Uri as an asynchronous operation.
/// </summary>
/// <typeparam name="T">Response type</typeparam>
/// <param name="uri">The Uri the request is sent to</param>
/// <returns></returns>
public System.Threading.Tasks.Task<T> GetAsync<T>(string uri) where T : class {
return GetAsync<T>(new Uri(uri));
}
/// <summary>
/// Send a GET request to the specified Uri as an asynchronous operation.
/// </summary>
/// <typeparam name="T">Response type</typeparam>
/// <param name="uri">The Uri the request is sent to</param>
/// <returns></returns>
public async System.Threading.Tasks.Task<T> GetAsync<T>(Uri uri) where T : class {
var result = default(T);
//Try to get content as T
try {
//send request and get the response
var response = await httpClient.GetAsync(uri).ConfigureAwait(false);
//if there is content in response to deserialize
if (response.Content.Headers.ContentLength.GetValueOrDefault() > 0) {
//get the content
string responseBodyAsText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
//desrialize it
result = deserializeJsonToObject<T>(responseBodyAsText);
}
} catch (Exception ex) {
Log.Error(ex);
}
return result;
}
private static T deserializeJsonToObject<T>(string json) {
var result = JsonSerializer.Deserialize<T>(json);
return result;
}
}
通过通过允许单元测试与抽象那些你保持演示检验的依赖假冒/模仿的服务代理。您可以使用伪造/模拟的HTTP客户端来测试您的服务代理。如果您需要更改/交换/维护应用程序组件,它还允许您注入这些接口的任何具体实现。
我投票结束这个问题作为题外话,因为这个问题属于http://programmers.stackexchange.com/ –