需求是这样的(直接拷贝了):开始开发标准电子面单接口,对接几家常见的物流公司并测试
过程略了,要下班了,大概记录一下,结果是这样的,直接上代码吧。
1: //相当于一个工厂,创建各种实例
2: public class ESingleLogic : _BaseLogic
3: {
4: ESingle _InitESingle(string carrierNo)
5: {
6: SystemParaLogic sysParalogic = new SystemParaLogic();
7:
8: if (carrierNo.Equals("EMS", StringComparison.CurrentCultureIgnoreCase))
9: {
10: //ems
11: return new ESingleEMS(sysParalogic.QuerySystemPara(""),
12: sysParalogic.QuerySystemPara(""),
13: sysParalogic.QuerySystemPara(""));
14: }
15: else if (carrierNo.Equals("JD", StringComparison.CurrentCultureIgnoreCase))
16: {
17: //京东
18: return new ESingleJD(
19: sysParalogic.QuerySystemPara(""),
20: sysParalogic.QuerySystemPara(""),
21: sysParalogic.QuerySystemPara(""));
22: }
23: else
24: {
25: return null;
26: }
27: }
28:
29: public string CreateESingle(vew_wms_ExpressList orderInfo)
30: {
31: //创建实例
32: var eSingle = _InitESingle(orderInfo.fCarrierNo);
33:
34: if (eSingle == null)
35: {
36: return null;
37: }
38:
39: var response = eSingle.CreateESingle(orderInfo);
40:
41: //记录返回内容
42: Log(response.OriginalResponseString, "ESingleResponse");
43:
44: return response.ResponseData;
45: }
46: }
一个基类:
1: public abstract class ESingle
2: {
3: /// <summary>
4: /// 接口地址(全路径)
5: /// </summary>
6: public string Url { get; set; }
7:
8: /// <summary>
9: /// 【WMS->物流公司】创建一个物流订单
10: /// </summary>
11: /// <param name="url">url地址</param>
12: /// <param name="orderInfo">快递订单信息</param>
13: /// <returns>远程服务器返回信息</returns>
14: public virtual ESingleResponse CreateESingle(vew_wms_ExpressList orderInfo)
15: {
16: throw new NotImplementedException();
17: }
18: }
一个实现类:
1: /// <summary>
2: /// EMS电子面单处理
3: /// </summary>
4: public class ESingleEMS : ESingle
5: {
6: string userName = "";
7: string userPsw = "";
8:
9: /// <summary>
10: /// 构造函数中列出自己需要的URL参数
11: /// </summary>
12: /// <param name="url"></param>
13: /// <param name="userName"></param>
14: /// <param name="userPsw"></param>
15: public ESingleEMS(string url, string userName, string userPsw)
16: {
17: this.Url = url;
18: this.userName = userName;
19: this.userPsw = userPsw;
20: }
21:
22: public override ESingleResponse CreateESingle(vew_wms_ExpressList orderInfo)
23: {
24: var request = _CreateRequestXml(orderInfo,
25: userName,
26: userPsw);
27: var response =Post(this.Url, request);
29:
32: ESingleResponse result = new ESingleResponse();return result;
46: }
47: }
设计初衷是想:对WMS来说只有一个入口,传入相同的参数,得到同样结构的结果。因此这里还引入一个统一结果类(还需要完善)
1: /// <summary>
2: /// 所有电子面单接口的返回内容都统一转化为这个实体
3: /// </summary>
4: public class ESingleResponse
5: {
6: /// <summary>
7: /// 原始的返回信息
8: /// </summary>
9: public string OriginalResponseString { get; set; }
10: /// <summary>
11: /// 执行是否成功
12: /// </summary>
13: public bool isSuccess { get; set; }
14: /// <summary>
15: /// 错误内容
16: /// </summary>
17: public string ErrMsg { get; set; }
18: /// <summary>
19: /// 请求单号
20: /// </summary>
21: public string ReqOrderNo { get; set; }
22: /// <summary>
23: /// 返回结果
24: /// </summary>
25: public string ResponseData { get; set; }
26: }
因为每家物流公司的接口所需要的参数都不相同,因此在实例化类的时候把这些参数通过构造函数填进去
//ems return new ESingleEMS(sysParalogic.QuerySystemPara(""), sysParalogic.QuerySystemPara(""), sysParalogic.QuerySystemPara(""));
给大家看下最早的设计,引以为鉴,一个是在构造函数中填充值,一个是在具体执行函数中。
public virtual ESingleResponse CreateESingle(string url, vew_wms_ExpressList orderInfo, string userName, string userPsw, Dictionary<string,string> otherPara) { throw new NotImplementedException(); }
VS
public string CreateESingle(vew_wms_ExpressList orderInfo){}
大家有什么的想法欢迎拍砖!