细微之处才能显示水平
1.Contains
最近上了个新的产品,但是不希望所有的客户都能使用,于是在配置文件里面做了一个过滤,看起来很简单一个功能,只要判断当前登录的客户是否包含在这个配置的字符串里面就可以了,假设在config文件里面有一个DFCorporation节点,值是'testa|testb'这样的值,代码如下,看上去没有什么错误吗?
string corporationIDs = ConfigurationManager.AppSettings["DFCorporation"]; if (wsFlt.ProductSource == 4 && (!string.IsNullOrEmpty(wsFlt.DirectFlightChannel) && wsFlt.DirectFlightChannel.ToUpper() == "ZH-WS")) { if (!string.IsNullOrEmpty(corporationIDs) && corporationIDs.Length > 0 && !corporationIDs.Contains(corporationid)) { return false; } }
其实是又问题的,关键在corporationIDs.Contains(corporationid)这句,如果有一个客户代码是'test',很明显会验证通过,没有扮演过滤的作用,因为'testa|testb'是包含'test'的,于是做了下面的改进。
//公司,产品过滤 string corporationIDs = ConfigurationManager.AppSettings["DFCorporation"]; string[] corpArray = corporationIDs.Split(new char[] { '|' }); if (wsFlt.ProductSource == 4 && !string.IsNullOrEmpty(corporationid) && (!string.IsNullOrEmpty(wsFlt.DirectFlightChannel) && wsFlt.DirectFlightChannel.ToUpper() == "ZH-WS")) { bool isSupportZHWS = false; for (int i = 0; i < corpArray.Length; i++) { if (corpArray[i].ToLower().Equals(corporationid.ToLower())) { isSupportZHWS = true; break; } } if (!isSupportZHWS) { return isSupportZHWS; } }
还可以这样写,利用List<string>的Contains方法
string directAirlineList = ConfigurationManager.AppSettings["SupportDirectAirlineList"]; if (!string.IsNullOrEmpty(directAirlineList)) supportDirectAirlineList = new List<string>(directAirlineList.ToUpper().Split('|')); //直连航班过滤 if (wsFlt.ProductSource == 4 && (!this.supportDirect || !this.supportDirectAirlineList.Contains(wsFlt.AirlineDibitCode.ToUpper().Trim()))) {return false; }
2.语句上下文
注意上一句对下一句的影响,就像下棋一样要能看到后面几步,这个需要一定的功力。看下面的代码,如果wsFlt.DirectFlightChannel="MU-WS" ,第一句会得到flt.DirectFlightChannel="MU-WS",这个时候还没有错,再执行下面一句会得到flt.DirectFlightChannel="MU",执行第三句会得到flt.DirectFlightChannel="",很显然这是错误的。
// 是直连航班应该都有DirectFlightChannel if (!string.IsNullOrEmpty(wsFlt.DirectFlightChannel)) { flt.DirectFlightChannel = wsFlt.DirectFlightChannel.Trim().ToUpper() == "MU-WS" ? wsFlt.DirectFlightChannel : wsFlt.DirectFlightChannel.Substring(0, 2); flt.DirectFlightChannel = wsFlt.DirectFlightChannel.Trim().ToUpper() == "ZH-WS" ? wsFlt.DirectFlightChannel : wsFlt.DirectFlightChannel.Substring(0, 2);
flt.DirectFlightChannel = wsFlt.DirectFlightChannel.Trim().ToUpper() == "ZH-WS" ? wsFlt.DirectFlightChannel : wsFlt.DirectFlightChannel.Substring(0, 2); }
正确的写法如下,这个才是正解。
switch (wsFlt.DirectFlightChannel.ToUpper().Trim()) { case "MU-WS": case "ZH-WS": case "HO-WS": flt.DirectFlightChannel = wsFlt.DirectFlightChannel; break; default: flt.DirectFlightChannel = wsFlt.DirectFlightChannel.Substring(0, 2); break; }
如果传入的是一个字符‘T’,下面这样写是不行的
db.AddInParameter(dbCommand, "@IsNeedPrint", DbType.Byte, o_OrdersEntity.IsNeedPrint);
应该这样
db.AddInParameter(dbCommand, "@IsNeedPrint", DbType.AnsiStringFixedLength, o_OrdersEntity.IsNeedPrint);
3.避免使用数组下标
<add key="FlightAgencyOfDirectFlights_HU" value="3U-WS:520,8L-WS:539,9C-WS:551,CZ-SP:528,GS-WS:540,HO-WS:636,HU-WS:507,JD-WS:538,MU-SP:528,MU-WS:569,PN-WS:541,T8-WS:528,ZH-WS:571,CZ-WS:661" /> /// <summary> /// 获取直联政策产品的票台 /// </summary> /// <param name="DirectFlightChannel">直联政策产品渠道标识</param> /// <returns>票台</returns> public static int GetStrategyFlightAgencyNew(string DirectFlightChannel) { string flightAgencyOfDirectFlights_HU = ConfigurationFunc.GetStringFromConfigurations("FlightAgencyOfDirectFlights_HU").ToString(); int flightAgencyOf3U = int.Parse(flightAgencyOfDirectFlights_HU.Split(',')[1].Split(':')[1]); int flightAgencyOfHU = int.Parse(flightAgencyOfDirectFlights_HU.Split(',')[0].Split(':')[1]); int flightAgencyOfMU = int.Parse(flightAgencyOfDirectFlights_HU.Split(',')[2].Split(':')[1]); int flightAgencyOfCZ = int.Parse(flightAgencyOfDirectFlights_HU.Split(',')[3].Split(':')[1]); int flihgtAgencyOfJD = int.Parse(flightAgencyOfDirectFlights_HU.Split(',')[4].Split(':')[1]); int flihgtAgencyOf8L = int.Parse(flightAgencyOfDirectFlights_HU.Split(',')[5].Split(':')[1]); int flihgtAgencyOfGS = int.Parse(flightAgencyOfDirectFlights_HU.Split(',')[6].Split(':')[1]); int flihgtAgencyOfPN = int.Parse(flightAgencyOfDirectFlights_HU.Split(',')[7].Split(':')[1]); DirectAirLineEnum dae = GetDirectAirLineEnum(DirectFlightChannel); int result = 0; if (dae == DirectAirLineEnum._3U) { result = flightAgencyOf3U; } else if (dae == DirectAirLineEnum.CZ) { result = flightAgencyOfCZ; } else if (dae == DirectAirLineEnum.HU) { result = flightAgencyOfHU; } else if (dae == DirectAirLineEnum.MU) { result = flightAgencyOfMU; } else if (dae == DirectAirLineEnum.JD) { result = flihgtAgencyOfJD; } else if (dae == DirectAirLineEnum._8L) { result = flihgtAgencyOf8L; } else if (dae == DirectAirLineEnum.GS) { result = flihgtAgencyOfGS; } else if (dae == DirectAirLineEnum.PN) { result = flihgtAgencyOfPN; } else if (dae == DirectAirLineEnum._9C) { int flihgtAgencyOf9C = int.Parse(flightAgencyOfDirectFlights_HU.Split(',')[8].Split(':')[1]); result = flihgtAgencyOf9C; } else if (dae == DirectAirLineEnum.T8) { int flihgtAgencyOfT8 = int.Parse(flightAgencyOfDirectFlights_HU.Split(',')[9].Split(':')[1]); result = flihgtAgencyOfT8; } else if (dae == DirectAirLineEnum.MUWS) { int flihgtAgencyOfMUWS = int.Parse(flightAgencyOfDirectFlights_HU.Split(',')[10].Split(':')[1]); result = flihgtAgencyOfMUWS; } //else if (dae == DirectAirLineEnum.ZH) //{ // int flihgtAgencyOfZH = int.Parse(flightAgencyOfDirectFlights_HU.Split(',')[11].Split(':')[1]); // result = flihgtAgencyOfZH; //} else if (dae == DirectAirLineEnum.ZHWS) { int flihgtAgencyOfZH = int.Parse(flightAgencyOfDirectFlights_HU.Split(',')[11].Split(':')[1]); result = flihgtAgencyOfZH; } else if (dae == DirectAirLineEnum.HOWS) { result = int.Parse(flightAgencyOfDirectFlights_HU.Split(',')[12].Split(':')[1]); } return result; }
换一种写法,使用字典来避免核对数组下标的麻烦
/// <summary> /// 获取直联政策产品的票台 /// </summary> /// <param name="DirectFlightChannel">直联政策产品渠道标识</param> /// <returns>票台</returns> public static int GetStrategyFlightAgencyNew(string DirectFlightChannel) { int result = 0; string flightAgencyOfDirectFlights = ConfigurationFunc.GetStringFromConfigurations("FlightAgencyOfDirectFlights_HU").ToString(); Dictionary<string, int> dicDirectFlightAgency = new Dictionary<string, int>(); if (!string.IsNullOrEmpty(flightAgencyOfDirectFlights)) { string[] listAirlineAgencys = flightAgencyOfDirectFlights.Split(','); foreach (string airlineAgency in listAirlineAgencys) { string[] listAirlineAgency = airlineAgency.Split(':'); if (airlineAgency.Contains(":") && listAirlineAgency.Length > 1) { int? agency = ConvertFunc.ConvertObjectToInt32OrNull(listAirlineAgency[1]); if (agency.HasValue) { dicDirectFlightAgency.Add(listAirlineAgency[0].ToUpper().Trim(), agency.Value); } } } if (dicDirectFlightAgency.ContainsKey(DirectFlightChannel)) { result = dicDirectFlightAgency[DirectFlightChannel]; } } return result; }
作者:Tyler Ning
出处:http://www.cnblogs.com/tylerdonet/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,请微信联系冬天里的一把火