【问题标题】:Using App.Config to derive which class/variables to use使用 App.Config 派生要使用的类/变量
【发布时间】:2019-10-14 17:28:53
【问题描述】:

我正在构建一个 selenium 框架,它需要在不同的客户之间表现出不同的行为,例如2 位客户的网页会略有不同

我正在尝试从 app.config appsettings 驱动这个,例如

<add key="Customer" value="ABC" /> 

然后我有一个客户类:

public static class Customer
    {
        public static string CustomerName = ConfigurationManager.AppSettings["Customer"];
        public static string custAddress = {CustomerName}.custAddress ; //this is obviously wrong - how do i do this?...
    }

public static class ABC
    {
        custAddress = "customer abc address";

    }

public static class DEF
    {
        custAddress = "customer def address";

    }

然后我想将其称为获取值的最简单方法,例如

var address = Customer.custAddress;

...将根据 app.config 设置进行设置。

我是一个 c# 新手,因此对缺乏清晰性表示歉意 - 我一直在试图弄清楚如何将客户字符串转换为与其同名的类。我认为接口也可能是一种选择,但要与复杂性作斗争。有没有更好的办法?

【问题讨论】:

  • 听起来您希望依赖注入与某种策略或工厂模式捆绑在一起。也许考虑使行为独立于特定客户定制也是合理的?否则,您最终会得到针对特定客户的无休止的特殊实现,所有这些都需要进行测试等。

标签: c# selenium-webdriver interface app-config


【解决方案1】:

我建议您为每个客户设置一组设置,地址就是AppSettings["Address"]

这样,当添加第三位客户时,您无需修改​​代码。如果您保留class ABC,那么进行设置就没有多大意义了。

【讨论】:

  • 谢谢 - 你能解释一下吗?如何区分 2 个不同客户的地址 - 我猜这意味着我的 app.config 中有多个 AppSettings["Address"],每个客户一个。
  • 你会有两个配置文件。
  • 感谢您的指导 - 我已经实施了下面的解决方案,它运行良好,这与我的技能水平差不多。非常感谢。 mitasoft.wordpress.com/2011/09/28/multipleappconfig
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-19
  • 2018-01-20
  • 1970-01-01
  • 2021-09-13
  • 1970-01-01
  • 2011-07-16
  • 1970-01-01
相关资源
最近更新 更多