猿问

在 WinFroms 中集中 API CRUD 调用

我需要在 Winforms 应用程序中进行 CRUD 调用。为此,我想为 Post、Put、Delete 和 Get 创建辅助方法。这个方法需要将数据返回给我。


对于 post 和 put,我可能会传递 List、myobject、string、int、bool 等。我的返回类型可能与我输入的模型不同。


为了实现上述目标,我正在考虑以下方面...


返回给调用者的类


public class ApiCallResult<X> where X: class 

{

    public HttpStatusCode StatusCode { get; set; }

    public string ReasonPhrase { get; set; }


    public bool IsError { get; set; }

    public bool IsException { get; set; }

    public string Message { get; set; }


    public X Response { get; set; } //deserialized object, could be List, int string or just a single object

}

但显然这有多个问题

  1. 异步可以返回一个任务,我想返回 apiCallResult

  2. var apiCallResult = new ApiCallResult<T> {IsError = true, Message = "No run"}; 线导致 The type 'T' must be a reference type in order to use it as parameter 'T'

  3. apiCallResult.Response = JsonConvert.DeserializeObject<List<X>>(jsonResponseString).ToList(); 线导致 Cannot convert source type 'System.Collections.Generic.List<X>' to target type 'T'

我怎样才能最好地做这样的事情?不想编写多个方法来执行此操作。


杨魅力
浏览 173回答 1
1回答

泛舟湖上清波郎朗

也许像这样?public static async Task<ApiCallResult<X>> Post<T, X>(T data, X returnModel, string apiUrl) where X: class{&nbsp; &nbsp; var apiCallResult = new ApiCallResult<X> { IsError = true, Message = "No run" };&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //json string&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var jsonString = JsonConvert.SerializeObject(data);&nbsp; &nbsp; &nbsp; &nbsp; using (var client = new HttpClient())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var response = await client.PostAsync(apiUrl, httpContent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var jsonResponseString = await response.Content.ReadAsStringAsync();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //fill&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; apiCallResult.StatusCode = response.StatusCode;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; apiCallResult.ReasonPhrase = response.ReasonPhrase;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (response.IsSuccessStatusCode)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //deserialize&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (typeof(X).GetGenericTypeDefinition() == typeof(List<>))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // X is a generic list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; apiCallResult.Response = JsonConvert.DeserializeObject<X>(jsonResponseString);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //single object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; apiCallResult.Message = JsonConvert.DeserializeObject<X>(jsonResponseString).ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; apiCallResult.IsError = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //error response&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; apiCallResult.Message = jsonResponseString;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; catch (Exception ex)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;apiCallResult.IsException = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;apiCallResult.Message = ex.Message;&nbsp; &nbsp; }&nbsp; &nbsp; return apiCallResult;}
随时随地看视频慕课网APP
我要回答