一遍又一遍地从 AppSettings 读取整数
我经常做的一些事情是从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了我的问题的答案。一开始它会涉及额外的工作,但最终会减少错误。
它可以在 Scott Allen 的博客 OdeToCode 中找到,这是我的实现:
创建一个名为
的静态类Config
这样做的优点有三个:
如果有人更改 AppSetting Key,它就会损坏,但我可以处理这个问题,因为这些值不会更改,并且性能比
TryParse
更好,并且可以在一个位置修复。解决办法就是这么简单...我不知道为什么我之前没有想到。像这样调用这些值:
耶!
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
Advantage of doing this are three-fold:
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:
Yay!
我知道这个问题很多年前就被问过,但也许这个答案对某人有用。目前,如果您已在类构造函数中收到
IConfiguration
引用,则最好的方法是使用GetValue("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 usingGetValue<int>("appsettings-key-goes-here")
:查看 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.