如何在switch语句中优化if语句

时间:2019-11-23 21:16:36

标签: c# if-statement interface switch-statement

我想知道是否有可能在下面优化此代码,从而不必在每个case语句中都包含“ if”语句?减少/最小化代码...

FYI-if语句在传递生产接口(例如ARMProduction.WebServiceAWI)和生产对象(例如新的ARMProduction.User())之间切换 而且它们来自不同的接口,所以我认为我无法创建一个接口并通过它。

switch(claimParams.ServiceName) {
case "ARM":
    if (_environment.Production)
       claimResult = await WebService<ARMProduction.WebServiceAWI>.GetClaim(claimParams, _environment.ARMUrl, _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new ARMProduction.User());     
    else
       claimResult = await WebService<ARMDevelopment.WebServiceAWI>.GetClaim(claimParams, _environment.ARMUrl, _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new ARMDevelopment.User());
    break;
case "BW":
    if (_environment.Production)
       claimResult = await WebService<BWProduction.WebServiceBW>.GetClaim(claimParams, _environment.BWUrl, _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new BWProduction.User());     
    else
       claimResult = await WebService<BWDevelopment.WebServiceBW>.GetClaim(claimParams, _environment.BWUrl, _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new BWDevelopment.User());
    break;
case "CS":
    if (_environment.Production)
       claimResult = await WebService<CSProduction.WebServiceCS>.GetClaim(claimParams, _environment.CSUrl, _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new CSProduction.User());     
    else
       claimResult = await WebService<CSDevelopment.WebServiceCS>.GetClaim(claimParams, _environment.CSUrl, _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new CSDevelopment.User());
    break;
}

3 个答案:

答案 0 :(得分:1)

不,不是。

我认为“优化”此方法(如您所说)的最佳方法是使其尽可能可读。它已经很难阅读,并且错误可以在这样的代码区域找到家。

所以,不,我不建议您尝试花哨并使用条件运算符或任何会使代码的可读性低于现有条件的操作。

就个人而言,如果将if语句从switch语句中删除,我会更喜欢它。这段代码应该是等效的,但我认为它的可读性更高:

if (_environment.Production)
{
    switch(claimParams.ServiceName)
    {
        case "ARM":
            claimResult = await WebService<ARMProduction.WebServiceAWI>.GetClaim(claimParams, _environment.ARMUrl, _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new ARMProduction.User());     
            break;
        case "BW":
            claimResult = await WebService<BWProduction.WebServiceBW>.GetClaim(claimParams, _environment.BWUrl, _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new BWProduction.User());     
            break;
        case "CS":
            claimResult = await WebService<CSProduction.WebServiceCS>.GetClaim(claimParams, _environment.CSUrl, _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new CSProduction.User());     
            break;
    }
}
else
{
    switch(claimParams.ServiceName)
    {
        case "ARM":
            claimResult = await WebService<ARMDevelopment.WebServiceAWI>.GetClaim(claimParams, _environment.ARMUrl, _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new ARMDevelopment.User());
            break;
        case "BW":
            claimResult = await WebService<BWDevelopment.WebServiceBW>.GetClaim(claimParams, _environment.BWUrl, _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new BWDevelopment.User());
            break;
        case "CS":
            claimResult = await WebService<CSDevelopment.WebServiceCS>.GetClaim(claimParams, _environment.CSUrl, _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new CSDevelopment.User());
            break;
    }
}

答案 1 :(得分:1)

从C#7开始,您可以在when语句(https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch#-the-case-statement-and-the-when-clause)中使用switch子句

这看起来像:

switch (claimParams.ServiceName)
{
    case "ARM" when _environment.Production:
        claimResult = await WebService<ARMProduction.WebServiceAWI>.GetClaim(claimParams, _environment.ARMUrl, _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new ARMProduction.User());
        break;
    case "ARM" when !_environment.Production:
        claimResult = await WebService<ARMDevelopment.WebServiceAWI>.GetClaim(claimParams, _environment.ARMUrl, _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new ARMDevelopment.User());
        break;
    case "BW" when _environment.Production:
        claimResult = await WebService<BWProduction.WebServiceBW>.GetClaim(claimParams, _environment.BWUrl, _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new BWProduction.User());
        break;
    case "BW" when !_environment.Production:
        claimResult = await WebService<BWDevelopment.WebServiceBW>.GetClaim(claimParams, _environment.BWUrl, _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new BWDevelopment.User());
        break;
    case "CS" when _environment.Production:
        claimResult = await WebService<CSProduction.WebServiceCS>.GetClaim(claimParams, _environment.CSUrl, _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new CSProduction.User());
        break;
    case "CS" when !_environment.Production:
        claimResult = await WebService<CSDevelopment.WebServiceCS>.GetClaim(claimParams, _environment.CSUrl, _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new CSDevelopment.User());
        break;
}

答案 2 :(得分:0)

如果您真的想减少代码(如@Fabio所述),则可以使用Dictionary。可以使用ServiceName和Production标志将其设置为组合键。

类似的东西:

var claims = new Dictionary<(string, bool), Func<Task<ClaimResult>>>();
claims.Add(("ARM", true), () => WebService<ARMProduction.WebServiceAWI>.GetClaim(claimParams, _environment.ARMUrl, 
        _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new ARMProduction.User()));
claims.Add(("ARM", false), () => WebService<ARMDevelopment.WebServiceAWI>.GetClaim(claimParams, _environment.ARMUrl, 
        _environment.TrustOnlineUsername, _environment.TrustOnlinePassword, new ARMDevelopment.User()));
var claimResult = await claims[(claimParams.ServiceName, _environment.Production)].Invoke();