首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在AppConfig.json中存储对象的最佳方法

在AppConfig.json中存储对象的最佳方法
EN

Stack Overflow用户
提问于 2022-03-19 14:40:14
回答 2查看 116关注 0票数 2

我的AppConfig.json:

代码语言:javascript
复制
{
     "MyTimeZone: "CET",
     "RegularString" : "SomeValue",
     "AnArray" : ["1","2"]
}

我的POCO课程:

代码语言:javascript
复制
public class Settings
{
     public TimeZoneInfo MyTimeZone { get; set; }
     public string RegularString { get; set; }
     public IList<string> AnArray { get; set; }
}

Registry.cs:

代码语言:javascript
复制
var configuration = GetConfiguration("AppSettings.json");
services.Configure<Settings>(configuration.GetSection("Settings"));

当然,这不会将"CET“绑定到有效的TimeZoneInfo对象。现在的问题是,在我的应用程序(一个web应用程序)中,将字符串转换为TimeZoneInfo的最佳位置是什么?是否有一种在不创建自定义转换器的情况下根据特定规则自动将字符串配置值转换为对象的方法?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-19 16:07:47

参考使用DI服务配置选项

代码语言:javascript
复制
services.AddOptions<Settings>()
    .Configure<IConfiguration>((setting, configuration) => {
        var section = config.GetSection("Settings");
        //This will populate the other properties that can bind by default
        section.Bind(setting);

        //this will extract the remaining value and set it mnually
        string value = section.GetValue<string>("MyTimeZone");
        TimeZoneInfo info = TimeZoneInfo.FindSystemTimeZoneById(value);
        setting.MyTimeZone = info;
    });

可以通过DI直接从配置中提取复杂的设置值,并用于创建时区并将其应用于设置。

票数 2
EN

Stack Overflow用户

发布于 2022-03-20 04:12:21

这只是我个人的观点,但我更希望MyTimeZone是一个json对象,而不是一个字符串。请考虑以下几点:

代码语言:javascript
复制
"Settings": {
    "MyTimeZone": {
      "ConfigureTimeZoneById":  "CET"
    },
    "RegularString": "SomeValue",
    "AnArray": [ "1", "2" ]
  }

MyTimeZone.ConfigureTimeZoneById不是实际数据对象的一部分。它只是将对象绑定到配置的代理。这是TimeZone类的样子:

代码语言:javascript
复制
public class TimeZone
    {
        private string configureTimeZoneById { get; set; }
        public string ConfigureTimeZoneById
        {
            get { return configureTimeZoneById; }
            set
            {
                configureTimeZoneById = value;
                InitializeTimeZone(value);
            }
        }
        public string TimeZoneId { get; set; }
        public string OtherProperties { get; set; }

        private void InitializeTimeZone(string id)
        {
            var getTimeZone = TimeZonesDataset().FirstOrDefault(tzon => tzon.TimeZoneId.Equals(id));
            if (getTimeZone != null)
            {
                this.TimeZoneId = getTimeZone.TimeZoneId;
                this.OtherProperties = getTimeZone.OtherProperties;
            }
        }







        //dummy dataset
        private List<TimeZone> TimeZonesDataset() => new List<TimeZone> {
            new TimeZone{TimeZoneId = "CET", OtherProperties = "Dummy properties to prove point"},
            new TimeZone{TimeZoneId = "GMT", OtherProperties = default},
        };
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71539089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档