根据操作对字符串进行分组的最佳方法

时间:2017-09-15 16:32:01

标签: c# refactoring

“最佳方式”我的意思是,也许,没有很多If,干净的代码。

我有一个作为参数接收的函数(string currentVersion,string action) 它应该返回一个字符串versionToBe =“”;

for action =“installOldVersion”

------------- if“currentVersion”----------------:------------- OldversionToInstall --------------

  • “windows10(pro)”,“windows10(pro)(education)”:“windows81(pro)”
  • “windows10(enterprise)”​​,“windows10(enterpise)(lstb)”:“windows81(enterprise)”​​
  • “windows7(home)”,“windows7(home)(basic)”,“windows7(basic)”,“windows7”:“windowsVista(starter)”
  • “windowsXP(starter)”,“windowsXP(starter)(home)”,“windowsXP(home)”,“windowsXP”:“windows2000(professional)”
  • “windowsNT(workstation)”,“windowsNT”:“windows95(sp1)”

for action =“installNewVersion”

------------- if“currentVersion”----------------:------------- NewVersionToInstall --------------

  • “windows81(pro)”,“windows81(pro)(education)”:“windows10(pro)”
  • “windows81(enterprise)”​​,“windows81(enterprise)(education)”:“windows10(enterprise)”​​
  • “windowsVista(starter)”,“windowsVista(starter)(package)”,“windowsVista(package)”,“windowsVista”:“windows7(home)”
  • “windowsVista(starter)”,“windowsVista(starter)(praok)”,“windowsVista(praok)”,“windowsVista”:“windowsXP(starter)”
  • “windows95(sp1)”,“windows95(sp1)(versionE)”,“windows95”:“windowsNT(workstation)”

因此,例如,每次字符串名称如下:“windows10(pro)”或“windows10(pro)(education)”时,它应返回:“windows81(pro)”。

我知道这可以通过以下方式完成:

if (version.Equals("windows10(pro)") || version.Equals("windows10(pro)(education)"))
            {
                versionToBe = "windows81(pro)";
            }

和其余的相同,并且总计10个If语句。

但如果有更好的方法,我想知道。

另一个限制或其他需要考虑的事项:

  • 如果操作是“installOldVersion”,则versionToBe是OldversionToInstall,             如果操作是“installNewVersion”,则versionTobe将是NewVersionToInstall。

3 个答案:

答案 0 :(得分:0)

您可以使用CurrentVersion,旧版本和新版本创建对象列表,然后从列表中提取所需的对象。

示例指令类定义

public class VersionInformation 
{
    public string CurrentVersion {get; set;}
    public string NewVersion {get; set;}
    public string OldVersion {get; set;}
}

然后在你的程序中,有一个列表,硬编码或从文件加载或你想要的任何数据存储,并按如下方式进行版本检查:

private List<VersionInformation> _versionInformation = //Load your list from wherever;

public void DoVersionCheck(string version)
{
    var currentversionInfo = _versionInformation.Single(x=> x.CurrentVersion == version);

    //Do Whatever you want with the upgrades and downgrades here based on whatever action you are doing
}

答案 1 :(得分:0)

设置自己的字典并执行查找。

作为读者的练习:

  • 如果需要,您可以从某些配置或其他...甚至从数据库中驱动字典内容。
  • 您可能希望将字典设置为静态并仅将其初始化一次。
  • 当没有字典条目时,您需要一些处理 - 您没有在问题中指定默认值。

    字典,字符串&gt; ActionMatrix = new Dictionary,string&gt;();

    ActionMatrix.Add(Tuple.Create(“windows10(pro)”,“installOldVersion”),“windows81(pro)”); ActionMatrix.Add(Tuple.Create(“windows10(pro)(education)”,“installOldVersion”),“windows81(pro)”); ActionMatrix.Add(Tuple.Create(“windows10(enterprise)”​​,“installOldVersion”),“windows81(enterprise)”​​); ActionMatrix.Add(Tuple.Create(“windows10(enterpise)(lstb)”,“installOldVersion”),“windows81(enterprise)”​​);

    //等

    ActionMatrix.Add(Tuple.Create(“windows81(pro)”,“installNewVersion”),“windows10(pro)”); ActionMatrix.Add(Tuple.Create(“windows81(pro)(education)”,“installNewVersion”),“windows10(pro)”); ActionMatrix.Add(Tuple.Create(“windows81(enterprise)”​​,“installNewVersion”),“windows10(enterprise)”​​); ActionMatrix.Add(Tuple.Create(“windows10(enterpise)(education)”,“installNewVersion”),“windows10(enterprise)”​​);

    //等

    public string VersionToBe(string currentVersion,string action) {     return ActionMatrix [Tuple.Create(currentVersion,action)]; }

答案 2 :(得分:0)

使用它自己的列表的简单对象应该可以做到这一点,并且在视觉上更好地遵循。

    public class VersionData
    {
        private static List<VersionData> VersionDatas { get; set; } = new List<VersionData>()
        {
            new VersionData( "OldversionToInstall", new [] {"windows10(pro)", "windows10(pro)(education)" }.ToList(), "windows81(pro)" ),
            new VersionData( "OldversionToInstall", new [] {"windows10(enterprise)", "windows10(enterpise)(lstb)" }.ToList(), "windows81(enterprise)" )
        };

        public string Action { get; set; } = "";
        public List<string> CurrentVersions { get; set; } = new List<string>();
        public string Version { get; set; } = "";

        public VersionData(string action, List<string> currentVersions, string version)
        {
            Action = action;
            CurrentVersions = currentVersions;
            Version = version;
        }

        public static string GetVersion(string action, string currentVersion)
        {
            return VersionDatas.FirstOrDefault(o => o.Action == action && o.CurrentVersions.Any(x => x == currentVersion)).Version;
        }
    }

并称之为:&#39; s简单如下:

var oldVersion = VersionData.GetVersion("OldversionToInstall", "windows10(enterpise)(lstb)");