Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# eTuple。从那时起,你有几个选择,我想提出一个开关_C#_.net_Design Patterns - Fatal编程技术网

C# eTuple。从那时起,你有几个选择,我想提出一个开关

C# eTuple。从那时起,你有几个选择,我想提出一个开关,c#,.net,design-patterns,C#,.net,Design Patterns,我认为最好先检查枚举,以防将来它会增长,然后检查布尔值。像这样: enum Animal { Fish, Dog, Zebra } string GetLabel(bool isAwesome, Animal animal) { switch (animal) { case Animal.Fish: return isAwesome ? "awesome fish" : "not so awesome fish";

我认为最好先检查枚举,以防将来它会增长,然后检查布尔值。像这样:

enum Animal
{
    Fish,
    Dog,
    Zebra
}

string GetLabel(bool isAwesome, Animal animal)
{
    switch (animal)
    {
        case Animal.Fish:
            return isAwesome ? "awesome fish" : "not so awesome fish";
        case Animal.Dog:
            return isAwesome ? "super awesome dog" : "awesome dog";
        case Animal.Zebra:
            return isAwesome ? "awesome zebra" : "normal zebra";
        default:
            return "is this even an animal?";
    }
}

这并不是说这比另一个答案中提出的嵌套
字典更有效。

字典是摆脱
if
switch
语句的好方法,但我不太喜欢Sam Axe提出的嵌套字典解决方案。我的解决方案只使用一个
字典

我创建了一个简单的类,该类将用作
字典
的键,覆盖
GetHashCode
Equals
方法:

internal class Key
{
    public readonly bool IsNetworkDeployed { get; }
    public readonly InventoryTypeEnum InventoryType { get; }

    public Key(InventoryTypeEnum inventoryType, bool isNetworkDeployed=false)
    {
        IsNetworkDeployed = isNetworkDeployed;
        InventoryType = inventoryType;
    }

    protected bool Equals(Key other)
    {
        return IsNetworkDeployed == other.IsNetworkDeployed && 
               InventoryType == other.InventoryType;
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != this.GetType()) return false;

        return Equals((Key) obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            return (IsNetworkDeployed.GetHashCode() * 397) ^ (int) InventoryType;
        }
    }
}
正如Sam所建议的,还有一个用来保存路径的类:

public class FilePaths
{
    public string InputPath { get; set; } = string.Empty;
    public string OutputPath { get; set; } = string.Empty;
}
初始化
词典

private readonly Dictionary<Key, FilePaths> _pathsDictionary = new Dictionary<Key, FilePaths>
{
    {
        new Key(InventoryTypeEnum.Licences, isNetworkDeployed: true),
        new FilePaths {
            InputPath = $@"{ApplicationDeployment.CurrentDeployment.DataDirectory}\{INPUTFILELICENSE}",
            OutputPath = $@"{ApplicationDeployment.CurrentDeployment.DataDirectory}\{LICENSEFILE}"
        }
    },
    {
        new Key(InventoryTypeEnum.Prices, isNetworkDeployed: true), 
        new FilePaths {
            InputPath = $@"{ApplicationDeployment.CurrentDeployment.DataDirectory}\{INPUTFILE}",
            OutputPath = $@"{ApplicationDeployment.CurrentDeployment.DataDirectory}\{PRICEFILE}"
        }
    },
    {
        new Key(InventoryTypeEnum.Licences, isNetworkDeployed: false), 
        new FilePaths {
            InputPath = INPUTFILELICENSE,
            OutputPath = LICENSEFILE
        }
    },
    {
        new Key(InventoryTypeEnum.Prices, isNetworkDeployed: false), 
        new FilePaths {
            InputPath = INPUTFILE,
            OutputPath = PRICEFILE
        }
    }
};

也许是问这个问题的好地方谢谢,我不知道那个网站,我会去看看。使用枚举的主要替代方法是数组、列表或其他集合(枚举比分组编译时整数常量多一点)。像字典这样的东西可能会更好用,因为你可以轻松地进行键查找以检查输入是否有效。你可能只需要将这些值存储在webconfig中,然后使用不同的构建配置来处理部署开关我认为最好使用
ValueTuple
作为键,不要使用你建议的嵌套
字典
。谢谢你的建议,我会仔细研究并测试一下。
字典
最多只能有两个条目:一个键为
true
,另一个键为
false
。如何添加四个值?您必须删除一个额外的true/false,然后删除/重新定位一些大括号,以便将所有值放入嵌套字典中。否则这将抛出一个异常Ibelieve@SamAxe为什么要让他们执行所有这些if语句,使用构建配置appsettings.json或使用web来处理这些语句不是更有意义吗。config@pinkfloydx33pi你完全正确。初始化器在这里不起作用。这就是我在没有测试的情况下写作的结果。你可以把初始化移到一个方法上。谢谢你的建议,这里性能不是问题,更多的可维护性和可读性,所以这是一个很好的建议。我喜欢这个。这是一个多一点的设置,但我认为它提供了一个非常好的界面。
public class FilePaths
{
    public string InputPath { get; set; } = string.Empty;
    public string OutputPath { get; set; } = string.Empty;
}
private readonly Dictionary<Key, FilePaths> _pathsDictionary = new Dictionary<Key, FilePaths>
{
    {
        new Key(InventoryTypeEnum.Licences, isNetworkDeployed: true),
        new FilePaths {
            InputPath = $@"{ApplicationDeployment.CurrentDeployment.DataDirectory}\{INPUTFILELICENSE}",
            OutputPath = $@"{ApplicationDeployment.CurrentDeployment.DataDirectory}\{LICENSEFILE}"
        }
    },
    {
        new Key(InventoryTypeEnum.Prices, isNetworkDeployed: true), 
        new FilePaths {
            InputPath = $@"{ApplicationDeployment.CurrentDeployment.DataDirectory}\{INPUTFILE}",
            OutputPath = $@"{ApplicationDeployment.CurrentDeployment.DataDirectory}\{PRICEFILE}"
        }
    },
    {
        new Key(InventoryTypeEnum.Licences, isNetworkDeployed: false), 
        new FilePaths {
            InputPath = INPUTFILELICENSE,
            OutputPath = LICENSEFILE
        }
    },
    {
        new Key(InventoryTypeEnum.Prices, isNetworkDeployed: false), 
        new FilePaths {
            InputPath = INPUTFILE,
            OutputPath = PRICEFILE
        }
    }
};
public string GetFromInventoryTableOnServer(InventoryTypeEnum type)
{
    var key = new Key(type, ApplicationDeployment.IsNetworkDeployed);
    FilePaths paths = _pathsDictionary[key];

    // remaining code here ...
}