一遍又一遍地从 AppSettings 读取整数

发布于 2024-09-18 06:24:54 字数 707 浏览 3 评论 0原文

我经常做的一些事情是从 AppSettings 中读取整数。最好的方法是什么?

而不是每次都这样做:

int page_size; 
if (int.TryParse( ConfigurationManager.AppSettings["PAGE_SIZE"], out page_size){

}

我在我的 Helpers 类中考虑这样的方法:

int GetSettingInt(string key) { 
  int i;
  return int.TryParse(ConfigurationManager.AppSettings[key], out i) ? i : -1;
}

但这只是为了节省一些击键。

理想情况下,我希望将它们全部放入某种可以使用智能感知的结构中,这样我就不会出现运行时错误,但我不知道如何解决这个问题......或者如果这可能的话。

从 Web.Config 的 AppSettings 部分获取和读取整数的最佳实践方法是什么?

还有一件事...

将其设置为只读不是一个好主意吗?

readonly int pageSize = Helpers.GetSettingInt("PAGE_SIZE") 似乎不起作用。

Some I do quite a lot of is read integers from AppSettings. What's the best way to do this?

Rather than do this every time:

int page_size; 
if (int.TryParse( ConfigurationManager.AppSettings["PAGE_SIZE"], out page_size){

}

I'm thinking a method in my Helpers class like this:

int GetSettingInt(string key) { 
  int i;
  return int.TryParse(ConfigurationManager.AppSettings[key], out i) ? i : -1;
}

but this is just to save some keystrokes.

Ideally, I'd love to put them all into some kind of structure that I could use intellisense with so I don't end up with run-time errors, but I don't know how I'd approach this... or if this is even possible.

What's a best practices way of getting and reading integers from the AppSettings section of the Web.Config?

ONE MORE THING...

wouldn't it be a good idea to set this as readonly?

readonly int pageSize = Helpers.GetSettingInt("PAGE_SIZE") doesn't seem to work.

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

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

发布评论

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

评论(3

找个人就嫁了吧 2024-09-25 06:24:54

我找到了我的问题的答案。一开始它会涉及额外的工作,但最终会减少错误。

它可以在 Scott Allen 的博客 OdeToCode 中找到,这是我的实现:

创建一个名为 的静态类Config

public static class Config {

   public static int PageSize {
       get { return int.Parse(ConfigurationManager.AppSettings["PAGE_SIZE"]); }
   }
   public static int HighlightedProductId {
     get { 
      return int.Parse(ConfigurationManager.AppSettings["HIGHLIGHT_PID"]); 
     }
   }
}

这样做的优点有三个:

  • 智能感知
  • 一个断点 (DRY)
  • 由于我只编写一次配置字符串,因此我执行常规的 int.Parse。

如果有人更改 AppSetting Key,它就会损坏,但我可以处理这个问题,因为这些值不会更改,并且性能比 TryParse 更好,并且可以在一个位置修复。

解决办法就是这么简单...我不知道为什么我之前没有想到。像这样调用这些值:

Config.PageSize

Config.HighlightedProductId

耶!

I've found an answer to my problem. It involves extra work at first, but in the end, it will reduce errors.

It is found at Scott Allen's blog OdeToCode and here's my implementation:

Create a static class called Config

public static class Config {

   public static int PageSize {
       get { return int.Parse(ConfigurationManager.AppSettings["PAGE_SIZE"]); }
   }
   public static int HighlightedProductId {
     get { 
      return int.Parse(ConfigurationManager.AppSettings["HIGHLIGHT_PID"]); 
     }
   }
}

Advantage of doing this are three-fold:

  • Intellisense
  • One breakpoint (DRY)
  • Since I only am writing the Config String ONCE, I do a regular int.Parse.

If someone changes the AppSetting Key, it will break, but I can handle that, as those values aren't changed and the performance is better than a TryParse and it can be fixed in one location.

The solution is so simple... I don't know why I didn't think of it before. Call the values like so:

Config.PageSize

Config.HighlightedProductId

Yay!

累赘 2024-09-25 06:24:54

我知道这个问题很多年前就被问过,但也许这个答案对某人有用。目前,如果您已在类构造函数中收到 IConfiguration 引用,则最好的方法是使用 GetValue("appsettings-key-goes-here")< /代码>:

public class MyClass
{
    private readonly IConfiguration _configuration;

    public MyClass(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public void MyMethod()
    {
        int value = _configuration.GetValue<int>("appsettings-key-goes-here");
    }
}

I know that this question was asked many years ago, but maybe this answer could be useful for someone. Currently, if you're already receiving an IConfiguration reference in your class constructor, the best way to do it is using GetValue<int>("appsettings-key-goes-here"):

public class MyClass
{
    private readonly IConfiguration _configuration;

    public MyClass(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public void MyMethod()
    {
        int value = _configuration.GetValue<int>("appsettings-key-goes-here");
    }
}
橘味果▽酱 2024-09-25 06:24:54

查看 T4Config。我将使用正确数据类型中的值的延迟加载来生成应用程序设置和 Web/应用程序配置的连接字符串部分的接口和具体实现。它使用一个简单的 T4 模板来自动为您生成内容。

Take a look at T4Config. I will generate an interface and concrete implementation of your appsettings and connectionstringsections of you web/app config using Lazyloading of the values in the proper data types. It uses a simple T4 template to auto generate things for you.

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