标签:address 抽象 serialize 自己 localhost 创始人 item 泛型 回调函数
本篇幅会专门记录在工作中实际碰到的问题场景,和一些比较好的实现方法作为汇总,可以供各位借鉴和参考,当然 本人入行不深,能力有限,仅供各位借鉴和参考。欢迎补充
做项目大家都知道会有远程请求API的情况,现在假设做API的项目你能接触到并且Git下来。那么继续往下看。
将API项目中用到的Dto引入自己项目。这样方便我们将请求到的json字符串直接序列化成dto,然后再转成我们自己需要的ViewModel,具体怎么转呢,往下看:
使用Extension,具体怎么用,我们来用一个实际例子说一下吧。比如,我们现在要请求一个所有俱乐部的api。首先我们定义一个ViewModel用于存放俱乐部信息,Model如下:
①定义ViewModel
public class ClubBase { /// <summary> /// 主键Id /// </summary> public long Id { get; set; } /// <summary> /// 俱乐部名称 /// </summary> public string Name { get; set; } /// <summary> /// 俱乐部描述 /// </summary> public string Description { get; set; } /// <summary> /// 创始人 /// </summary> public string Creator { get; set; } /// <summary> /// 创建年份 /// </summary> public int Year { get; set; } /// <summary> /// 其他信息 /// </summary> public string Contactor { get; set; } /// <summary> /// 手机号 /// </summary> public string Phone { get; set; } /// <summary> /// 地址 例如 中国/深圳 /// </summary> public string Address { get; set; } /// <summary> /// 日期 /// </summary> public DateTime CreationDate { get; set; } }
②编写接口
/// <summary> /// 所有接口信息 /// </summary> public interface IDataServices { /// <summary> /// 获取所有俱乐部 /// </summary> /// <returns></returns> Task<IList<ClubBase>> GetClubs(); }
③实现Server接口
/// <summary> /// 接口实现类 动态请求api获取相应数据 /// </summary> public class DataServices: IDataServices { private readonly IDataHttpServers _dataHttpServers; public DataServices(IDataHttpServers dataHttpServers) { _dataHttpServers = dataHttpServers; } public async Task<IList<ClubBase>> GetClubs() { var clubsModelList = await _dataHttpServers.GetClubs(); return clubsModelList; } }
④编写Http请求接口
public interface IDataHttpServers { /// <summary> /// 获取所有俱乐部 /// </summary> /// <returns></returns> Task<IList<ClubBase>> GetClubs(); }
⑤实现此接口
/// <summary> /// 返回俱乐部列表 /// </summary> /// <returns></returns> public async Task<IList<ClubBase>> GetClubs() { return await Task.Run(() => GetClubApi()); }
/// <summary> /// 获取所有俱乐部API /// </summary> /// <returns></returns> public List<ClubBase> GetClubApi() { var list = HttpHelper.GetApi<string>("getclub"); List<ClubBase> viewClubList = new List<ClubBase>(); try { List<ClubInfoDto> apiClubList = JsonConvert.DeserializeObject<List<ClubInfoDto>>(list); foreach (var item in apiClubList) { //调用拓展方法解耦dto于ViewModel的赋值操作 var club = item.TranslateToClubBaseViewModel(); viewClubList.Add(club); } } catch (Exception e) { //请求接口api异常,异常描述 list } return viewClubList; }
重头戏也就是此方法了,其中 可以将请求api单独抽离出来写成泛型方法,此处是这样抽离的:
/// <summary> /// HTTPclient 泛型抽象类 /// </summary> public static class HttpHelper { public static T GetApi<T>(string apiName, string pragm = "") { var client = new RestSharpClient($"{SiteConfig.GetSite("Url")}"); var request = client.Execute(string.IsNullOrEmpty(pragm) ? new RestRequest($"{SiteConfig.GetSite($"{apiName}")}", Method.GET) : new RestRequest($"{SiteConfig.GetSite($"{apiName}")}/{pragm}", Method.GET)); if (request.StatusCode != HttpStatusCode.OK) { return (T)Convert.ChangeType(request.ErrorMessage, typeof(T)); } T result = (T)Convert.ChangeType(request.Content, typeof(T)); return result; } }
这里我请求API是使用的RestSharp提供的请求方法。具体可以看这儿 RestSharp ,需要NuGet安装RestSharp
这里我稍微贴一下 大概常用的一些接口方法:
1)接口
/// <summary> /// API请求执行者接口 /// </summary> public interface IRestSharp { /// <summary> /// 同步执行方法 /// </summary> /// <param name="request"></param> /// <returns></returns> IRestResponse Execute(IRestRequest request); /// <summary> /// 同步执行方法 /// </summary> /// <typeparam name="T">返回值</typeparam> /// <param name="request">请求参数</param> /// <returns></returns> T Execute<T>(IRestRequest request) where T : new(); /// <summary> /// 异步执行方法 /// </summary> /// <param name="request">请求参数</param> /// <param name="callback"></param> /// <returns></returns> RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action<IRestResponse> callback); /// <summary> /// 异步执行方法 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="request"></param> /// <param name="callback"></param> /// <returns></returns> RestRequestAsyncHandle ExecuteAsync<T>(IRestRequest request, Action<IRestResponse<T>> callback) where T : new(); }
2)实现
/// <summary> /// Rest接口执行者 /// </summary> public class RestSharpClient : IRestSharp { /// <summary> /// 请求客户端 /// </summary> private RestClient client; /// <summary> /// 接口基地址 格式:http://www.xxx.com/ /// </summary> private string BaseUrl { get; set; } /// <summary> /// 默认的时间参数格式 /// </summary> private string DefaultDateParameterFormat { get; set; } /// <summary> /// 默认验证器 /// </summary> private IAuthenticator DefaultAuthenticator { get; set; } /// <summary> /// 构造函数 /// </summary> /// <param name="baseUrl"></param> /// <param name="authenticator"></param> public RestSharpClient(string baseUrl, IAuthenticator authenticator = null) { BaseUrl = baseUrl; client = new RestClient(BaseUrl); DefaultAuthenticator = authenticator; //默认时间显示格式 DefaultDateParameterFormat = "yyyy-MM-dd HH:mm:ss"; //默认校验器 if (DefaultAuthenticator != null) { client.Authenticator = DefaultAuthenticator; } } /// <summary> /// 通用执行方法 /// </summary> /// <param name="request">请求参数</param> /// <remarks> /// 调用实例: /// var client = new RestSharpClient("http://localhost:82/"); /// var result = client.Execute(new RestRequest("api/values", Method.GET)); /// var content = result.Content;//返回的字符串数据 /// </remarks> /// <returns></returns> public IRestResponse Execute(IRestRequest request) { request.DateFormat = string.IsNullOrEmpty(request.DateFormat) ? DefaultDateParameterFormat : request.DateFormat; var response = client.Execute(request); return response; } /// <summary> /// 同步执行方法 /// </summary> /// <typeparam name="T">返回的泛型对象</typeparam> /// <param name="request">请求参数</param> /// <remarks> /// var client = new RestSharpClient("http://localhost:82/"); /// var result = client.Execute<List<string>>(new RestRequest("api/values", Method.GET)); /// </remarks> /// <returns></returns> public T Execute<T>(IRestRequest request) where T : new() { request.DateFormat = string.IsNullOrEmpty(request.DateFormat) ? DefaultDateParameterFormat : request.DateFormat; var response = client.Execute<T>(request); return response.Data; } /// <summary> /// 异步执行方法 /// </summary> /// <param name="request">请求参数</param> /// <param name="callback">回调函数</param> /// <remarks> /// 调用实例: /// var client = new RestSharpClient("http://localhost:62981/"); /// client.ExecuteAsync<List<string>>(new RestRequest("api/values", Method.GET), result => /// { /// var content = result.Content;//返回的字符串数据 /// }); /// </remarks> /// <returns></returns> public RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action<IRestResponse> callback) { request.DateFormat = string.IsNullOrEmpty(request.DateFormat) ? DefaultDateParameterFormat : request.DateFormat; return client.ExecuteAsync(request, callback); } /// <summary> /// 异步执行方法 /// </summary> /// <typeparam name="T">返回的泛型对象</typeparam> /// <param name="request">请求参数</param> /// <param name="callback">回调函数</param> /// <remarks> /// 调用实例: /// var client = new RestSharpClient("http://localhost:62981/"); /// client.ExecuteAsync<List<string>>(new RestRequest("api/values", Method.GET), result => /// { /// if (result.StatusCode != HttpStatusCode.OK) /// { /// return; /// } /// var data = result.Data;//返回数据 /// }); /// </remarks> /// <returns></returns> public RestRequestAsyncHandle ExecuteAsync<T>(IRestRequest request, Action<IRestResponse<T>> callback) where T : new() { request.DateFormat = string.IsNullOrEmpty(request.DateFormat) ? DefaultDateParameterFormat : request.DateFormat; return client.ExecuteAsync<T>(request, callback); } }
好了,我们接着上面说,大家可以看到,当我们通过请求api获取到数据之后得到的是下面这个Json字符串如下:
为什么我们需要引入api的dto而不是直接使用ViewModel来赋值我们需要的呢,这里有两个好处,第一个,我们可以快速完成json字符串到对象的转变,第二个,我们可以使用我们下面说的一个小技巧快速过滤出我们想组装的ViewModel即可。
注意下我们的这个方法TranslateToClubBaseViewModel ,是我们用于操作dto和ViewModel的主要方法
/// <summary> /// 此方法用于将Dto类型 数据 赋值到需要操作的ViewModel上 /// </summary> /// <param name="clubInfoDto">dto数据集</param> /// <returns></returns> public static ClubBase TranslateToClubBaseViewModel(this ClubInfoDto clubInfoDto) { ClubBase club = new ClubBase() { #region 实体按需赋值 Id = clubInfoDto.Id, Address = clubInfoDto.Address, Contactor = clubInfoDto.Contactor, Creator = clubInfoDto.Creator, Description = clubInfoDto.Description, Name = clubInfoDto.Name, Phone = clubInfoDto.Phone, Year = clubInfoDto.Year #endregion }; return club; }
这样操作的好处,我们就不用维护具体的业务层,专注于ViewModel上面。达到了解耦效果。
未完待续。。。
标签:address 抽象 serialize 自己 localhost 创始人 item 泛型 回调函数
原文地址:https://www.cnblogs.com/zhangxiaoyong/p/9419202.html