Пример #1
0
        private static void InitConfig()
        {
            // create a non-dynamic K/V (string/string) configuration source
            PropertiesFileConfigurationSourceConfig sourceConfig = StringPropertySources
                                                                   .NewPropertiesFileSourceConfigBuilder().SetName("app-properties-file").SetFileName("app.properties")
                                                                   .Build();
            IConfigurationSource source = StringPropertySources.NewPropertiesFileSource(sourceConfig);

            // create a configuration manager with single source
            ConfigurationManagerConfig managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("little-app")
                                                       .AddSource(1, source).Build();
            IConfigurationManager manager = ConfigurationManagers.NewManager(managerConfig);

            // create a StringProperties facade tool
            _properties = new StringProperties(manager);

            // default to null
            _appId = _properties.GetStringProperty("app.id");

            // default to "unknown"
            _appName = _properties.GetStringProperty("app.name", "unknown");

            // default to empty list
            _userList = _properties.GetListProperty("user.list", new List <string>());

            // default to empty map
            _userData = _properties.GetDictionaryProperty("user.data", new Dictionary <string, string>());

            // default to 1000, if value in app._properties is invalid, ignore the invalid value
            _sleepTime = _properties.GetIntProperty("sleep.time", 1000, v => v < 0 ? null : v);

            // custom type property
            _myCustomData = _properties.GetProperty("my-custom-type-property", MyCustomType.Converter);
        }
Пример #2
0
        private static void InitConfig()
        {
            // create a non-dynamic K/V (string/string) configuration source
            PropertiesFileConfigurationSourceConfig sourceConfig = StringPropertySources
                                                                   .NewPropertiesFileSourceConfigBuilder().SetName("app-properties-file").SetFileName("app.properties")
                                                                   .Build();
            IConfigurationSource source = StringPropertySources.NewPropertiesFileSource(sourceConfig);

            // crate a dynamic K/V (string/string) configuration source
            _systemPropertiesSource = StringPropertySources.NewMemoryDictionarySource("system-property");

            // create a configuration manager with 2 sources, system property has higher priority then app.properties
            ConfigurationManagerConfig managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("little-app")
                                                       .AddSource(1, source).AddSource(2, _systemPropertiesSource).Build();
            IConfigurationManager manager = ConfigurationManagers.NewManager(managerConfig);

            // Add a log listener
            manager.OnChange += (o, e) =>
                                Console.WriteLine("\nproperty {0} changed, old value: {1}, new value: {2}, changeTime: {3}",
                                                  e.Property, e.OldValue, e.NewValue, e.ChangeTime);

            // create a StringProperties facade tool
            _properties = new StringProperties(manager);

            // default to null
            _appId = _properties.GetStringProperty("app.id");

            // default to "unknown"
            _appName = _properties.GetStringProperty("app.name", "unknown");
            // Add change listener to app.name
            _appName.OnChange += (o, e) => Console.WriteLine("\napp.name changed, maybe we need do something");

            // default to empty list
            _userList = _properties.GetListProperty("user.list", new List <string>());

            // default to empty map
            _userData = _properties.GetDictionaryProperty("user.data", new Dictionary <string, string>());

            // default to 1000, if value in app._properties is invalid, ignore the invalid value
            _sleepTime = _properties.GetIntProperty("sleep.time", 1000, v => v < 0 ? null : v);

            // custom type property
            _myCustomData = _properties.GetProperty("my-custom-type-property", MyCustomType.Converter);
        }