简体   繁体   English

序列化c#使用开关来序列化自述文件

[英]Serializing c# use switch to serialize a Readme

I developed an application which offers regression testing functionality to other developers within my organization internally, and the main way that they interface through the front-end of application is via hard-copded strings that correspond to a switch case structure in the code like such: 我开发了一个应用程序,它在内部向组织内的其他开发人员提供回归测试功能,并且他们通过应用程序前端接口的主要方式是通过硬编码字符串,这些字符串对应于代码中的switch case结构,如:

           case "BUILDTEST":
                //Build the test with the supplied designator as 'arg_designator'
                returnVal = BuildDesignator();
                break;

            case "CHANGEROLE":
                //Changes the user's role
                returnVal = ChangeRole();
                break;

            case "CHECKOUTDATA":
                //Check out the data you wish to test
                returnVal = CheckoutRoute();
                break;

What is the best way to serialize the names and comments below the case to generate a some sort of readme file every build? 序列化案例下面的名称和注释以在每次构建时生成某种自述文件的最佳方法是什么? In this example I would like to generate a file that looks like: 在这个例子中,我想生成一个看起来像这样的文件:

FUNCTIONALITY

BUILDTEST: Build the test with the supplied designator as 'arg_designator'

CHANGEROLE: Changes the user's role

CHECKOUTDATA: Check out the data you wish to test

Thank you for any assistance offered 感谢您提供的任何帮助

If you don't mind a bit of restructuring, here is a better solution than serializing the contents of the switch statement. 如果您不介意进行一些重组,这里有一个比序列化switch语句内容更好的解决方案。 I highly doubt you want to run regex find/replace operations over that source or mess around with adding files to help with deriving the switch contents. 我非常怀疑你想在该源上运行正则表达式查找/替换操作,或者添加文件来帮助导出交换机内容。

First, make sure each of your build action classes inherits from a base class. 首先,确保每个构建操作类都继承自基类。 While you do that, give each build action class a description to make things easier: 在执行此操作时,请为每个构建操作类提供描述以使事情更容易:

public abstract class BuildAction
{
    public string Description { get; set; }

    protected BuildAction(string description)
    {
        Description = description;
    }

    public abstract void PerformBuildAction();
}

public class BuildTest : BuildAction
{
    public BuildTest() : base("Build the test with the supplied designator as 'arg_designator'") { }

    public override void PerformBuildAction() 
    {
        // do stuff here
    }
}

public class ChangeRole : BuildAction
{
    public ChangeRole() : base("Change the user's role") { }

    public override void PerformBuildAction()
    {
        // do stuff here
    }
}

public class CheckoutData : BuildAction
{
    public CheckoutData : base("Check out the data you wish to test") { }

    public override void PerformBuildAction()
    {
        // do stuff here
    }
}

After that you can get rid of your switch statement and replace it with a generic factory. 之后,您可以删除switch语句并将其替换为通用工厂。 Depending on how you make the factory, you can leverage it for your readme generation by first building a directory to iterate through for the readme and simplify the lookup for your command routing: 根据您的工厂制作方式,您可以通过首先构建一个目录来迭代自述文件并简化命令路由的查找,从而利用它来生成自述文件:

public class BuildActionFactory
{
    private static IDictionary<string, BuildAction> _buildActions =
        new Dictionary<string, BuildAction>()
        {
            {"BUILDTEST", typeof(BuildTest)},
            {"CHANGEROLE", typeof(ChangeRole)},
            {"CHECKOUTDATA", typeof(CheckoutData)}
        };

    public static BuildAction CreateBuildAction(string directive)
    {
        return _buildActions.ContainsKey(directive) ?
            Activator.CreateInstance(_buildActions[directive]) :
            null;
    }

    public static string BuildReadme()
    {
        return string.Join(Environment.NewLine + Environment.NewLine,
            new [] {"FUNCTIONALITY"}.Union(_buildActions.Select(pair => pair.Key + ": " + pair.Value.Description));
    }
}

If you make your factory like that, your switch statement can be replaced by a simple call to BuildActionFactory.CreateBuildAction("BUILDTEST/CHANGEROLE/CHECKOUTDATA/etc whatever you got from the user").PerformBuildAction() and then your readme contents can be generated by BuildActionFactory.BuildReadme() . 如果您使用这样的工厂,可以通过简单调用BuildActionFactory.CreateBuildAction("BUILDTEST/CHANGEROLE/CHECKOUTDATA/etc whatever you got from the user").PerformBuildAction()来替换您的switch语句BuildActionFactory.CreateBuildAction("BUILDTEST/CHANGEROLE/CHECKOUTDATA/etc whatever you got from the user").PerformBuildAction()然后您的自述文件内容可以是由BuildActionFactory.BuildReadme()生成。 Of course, if you want the readme built when the solution builds, you would need to add a post build task that calls BuildActionFactory.BuildReadme() from a wrapper console app project but that's simpler than refactoring to read in a configuration file you can generate a readme from. 当然,如果您希望在构建解决方案时构建自述文件,则需要添加一个从BuildActionFactory.BuildReadme()器控制台应用程序项目调用BuildActionFactory.BuildReadme()构建后任务,但这比重构读取您可以生成的配置文件更简单来自的自述文件。

Disclaimer: all of the above code was written in Sublime. 免责声明:以上所有代码均以Sublime编写。 None of it has been compiled or tested but it should give you an idea of what to do if you take this approach. 它们都没有经过编译或测试,但它应该让您知道如果采用这种方法该怎么做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM