使这个代码片段变得更好

发布于 2024-11-01 19:53:37 字数 813 浏览 0 评论 0 原文

我有一个包含以下代码的程序:

foreach (string section in DataAccessLayer.AcceptedSections)
{
    switch (section)
    {
        case "Section1":
            Console.WriteLine("Section 1");
            break;
        case "Section2":
            Console.WriteLine("Section 2");
            break;
        case "Section3":
            Console.WriteLine("Section 3");
            break;
        default:
            Console.WriteLine("Default section");
            break;
    }                    
}

我是否可以执行此代码的操作,而无需在案例中再次提供该部分的字符串? DataAccessLayer.AcceptedSections 是动态的,我不想必须在我的代码中添加另一个部分案例,每次有新部分出现时重建和重新部署。今天是星期五,我的思维不太好。

例如: 当第 4 节添加到数据库时,我不想添加以下代码:

case "Section4":
    Console.WriteLine("Section 4");
     break;

I have a program that has the following code:

foreach (string section in DataAccessLayer.AcceptedSections)
{
    switch (section)
    {
        case "Section1":
            Console.WriteLine("Section 1");
            break;
        case "Section2":
            Console.WriteLine("Section 2");
            break;
        case "Section3":
            Console.WriteLine("Section 3");
            break;
        default:
            Console.WriteLine("Default section");
            break;
    }                    
}

Is there anyway i can do what this code does without providing the section's string again within the case? The DataAccessLayer.AcceptedSections is dynamic and i don't want to have to add another section case to my code, rebuild and redeploy every time a new section comes on board. It's Friday and my mind is not working very well.

For Example:
I don't want to add the following code when Section 4 gets added to the database:

case "Section4":
    Console.WriteLine("Section 4");
     break;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

命比纸薄 2024-11-08 19:53:37

如果字符串始终是“SectionN”,您可以直接处理它:

if (section.StartsWith("Section"))
    Console.WriteLine(section.Insert(7, " "));
else
    Console.WriteLine("Default Section");

If the string is always "SectionN", you could just handle it directly:

if (section.StartsWith("Section"))
    Console.WriteLine(section.Insert(7, " "));
else
    Console.WriteLine("Default Section");
向日葵 2024-11-08 19:53:37

有一个由 section 键控的 Dictionary>。这将完全取代 switch 语句。

调用相应的动作:

foreach (string section in DataAccessLayer.AcceptedSections)
{
    myActionsDictionary[section]();
}

Have a Dictionary<string,Action<T>> that is keyed by section. This will completely replace the switch statement.

Invoke the corresponding action:

foreach (string section in DataAccessLayer.AcceptedSections)
{
    myActionsDictionary[section]();
}
暗恋未遂 2024-11-08 19:53:37

如果这都是数据驱动的,我建议您从数据库中返回一些其他显示值以及标识符字符串

Table AcceptedSections

Name = "Section1"
DisplayName = "Section 1"

然后您可以只返回 DisplayName


如果它是不是您必须像现在一样处理这个问题,或者您可以创建一个带有用于显示的属性的枚举:

public enum AcceptedSections
{
    [Description("Default Section")]
    Default,
    [Description("Section 1")]
    Section1,
    [Description("Section 2")]
    Section2,
    [Description("Section 3")]
    Section3,
    [Description("Section 4")]
    Section4
}
// writing this made me kind woozy... what a terrible enum

这将允许您编写如下内容:

foreach (AcceptedSections section in AcceptedSections.GetValues())
{
    Console.WriteLine(section.GetDescription());
}

其中 GetDescription() 是返回枚举上的自定义属性的简单方法

If this is all data-driven I suggest you just return some other display value from the database along with that identifier string

Table AcceptedSections

Name = "Section1"
DisplayName = "Section 1"

Then you can just just return the DisplayName


If it is not you'll have to handle this like you're doing now or you could create an enum with an attribute for display:

public enum AcceptedSections
{
    [Description("Default Section")]
    Default,
    [Description("Section 1")]
    Section1,
    [Description("Section 2")]
    Section2,
    [Description("Section 3")]
    Section3,
    [Description("Section 4")]
    Section4
}
// writing this made me kind woozy... what a terrible enum

which will allow you to write something like this:

foreach (AcceptedSections section in AcceptedSections.GetValues())
{
    Console.WriteLine(section.GetDescription());
}

where GetDescription() is a simple method that returns that custom attribute on the enum

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文