服务端接口定义另类方法
来源:互联网 发布:红色气质微电影 知乎 编辑:程序博客网 时间:2024/04/25 20:14
内部接口的定义与实现各有千秋,本文所提到的是这样的思路:通过内部通用的方法完成“路由”转发,这样做的好处,不言而喻:单点控制风险、通用逻辑校验、流量控制……等等。
直接上代码:
public class MerchantPayment : IMerchantPayment { #region IPaymentClient 成员 /// <summary> /// 执行API请求 /// </summary> /// <typeparam name="T">领域对象</typeparam> /// <param name="request">具体的API请求</param> /// <param name="parser">自定义解析器</param> /// <returns>领域对象</returns> public T Execute<T>(IRequest request, IParser<T> parser) where T : IResponse { try { int RetCode = ToolUtils.CheckInvokeParameter(request); if (RetCode != Consts.Success) { return parser.Parse(RetCode); } InterfaceFactory factory = new InterfaceFactory(); factory.ApiName = request.GetApiName(); return factory.PaymentInstance.Payment<T>(request); } catch (System.Exception e) { LogHelper.Error(e); return parser.Parse(e.Message); } } public SinglePaymentResponse SingleAccount(SinglePaymentAccount request) { return this.Execute<SinglePaymentResponse>(request, new SinglePaymentParser()); } public SinglePaymentResponse SingleCard(SinglePaymentCard request) { return this.Execute<SinglePaymentResponse>(request, new SinglePaymentParser()); } public BatchPaymentResponse BatchPayment(BatchPaymentRequest request) { ((IApiNameChanger)request).SetName(Consts.ApiName.BatchAccountPay); return this.Execute<BatchPaymentResponse>(request, new BatchPaymentParser()); } public CheckPaymentResponse ValidatePayment(BatchPaymentRequest request) { ((IApiNameChanger)request).SetName(Consts.ApiName.BatchAccountCheck); return this.Execute<CheckPaymentResponse>(request, new CheckPaymentParser()); } #endregion } /// <summary> /// 服务类对象 /// </summary> public IPaymentService PaymentInstance { get { switch (ApiName) { case Consts.ApiName.SinglePay: return new SinglePaymentService(); case Consts.ApiName.BatchAccountPay: return new BatchPaymentService(); case Consts.ApiName.BatchAccountCheck: return new ValidateService(); } return new NullService(); } }/// <summary> /// 支付服务 /// </summary> public interface IPaymentService { /// <summary> /// 付款 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="request">请求对象</param> /// <returns></returns> T Payment<T>(IRequest request) where T : IResponse; } /// <summary> /// Summary description for SinglePaymentService /// </summary> public class SinglePaymentService : IPaymentService { #region IPaymentService 成员 public T Payment<T>(IRequest request) where T : IResponse { SinglePaymentResponse response = new SinglePaymentResponse(); //....... return response as T; } #endregion } public class ValidateService : IPaymentService { T IPaymentService.Payment<T>(IRequest request) { BatchPaymentRequest req = request as BatchPaymentRequest; //...... return GetResponse(Consts.Success) as T; } } /// <summary> /// Summary description for BatchPaymentService /// </summary> public class BatchPaymentService : IPaymentService { /// <summary> /// 批量付款 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="request"></param> /// <returns></returns> T Payment<T>(IRequest request) where T : IResponse { BatchPaymentRequest batch = request as BatchPaymentRequest; //....... return new BatchPaymentResponse { RetCode = Consts.Success, Message = "success" } as T; } } /// <summary> /// 默认服务, 异常兼容 /// </summary> public class NullService : IPaymentService { #region IPaymentService 成员 T IPaymentService.Payment<T>(IRequest request) { throw new NotImplementedException(); } #endregion }
接口调用时,根据需要可以自行执行解析器、指定验证器等。
- 服务端接口定义另类方法
- 一种服务端接口身份验证方法
- 适配远程接口调用的另类方法
- 与服务端通信异步接口的定义方案
- API开发第四篇:定义客户端/服务端接口协议
- 另类的定义
- 第十九条:接口只用来定义方法
- Java中接口中的方法定义规则
- CharSequence接口在方法定义中的应用
- 委托和接口另类理解
- 另类调试方法
- 缩小日志另类方法
- 隐藏文件另类方法
- 另类图片复制方法
- 浮动清理另类方法
- js 刷新 另类 方法
- js另类压缩方法
- erlang另类方法调用
- Webservice 的设计和模式
- 解决了space中的问题
- 也说蛤蟆与专家
- JPA QL 中使用外连接遇到的问题和解决方式
- Sunshine Stone
- 服务端接口定义另类方法
- WebService的基本概念
- JUnit 4教程入门
- eclipse中使用不同jdk版本的项目设置
- JS图片循环播放
- 推荐一篇ajax的文章
- ASP.NET中文乱码问题解决
- 一直拖拉欠没做完的事情
- 项目经理的工具箱---走出软件作坊:三五个人十来条枪 如何成为开发正规军(三)