public virtual void TestDynamicProperty()
        {
            MemoryDictionaryConfigurationSource source = CreateSource();

            source.SetPropertyValue("exist", "ok");

            IConfigurationManager           manager        = CreateManager(source);
            PropertyConfig <String, String> propertyConfig = ConfigurationProperties.NewConfigBuilder <String, String>()
                                                             .SetKey("exist").SetDefaultValue("default").Build();
            IProperty <String, String> property = manager.GetProperty(propertyConfig);

            Console.WriteLine("property: " + property + "\n");
            Assert.Equal("ok", property.Value);

            source.SetPropertyValue("exist", "ok2");
            Thread.Sleep(10);
            Console.WriteLine("property: " + property + "\n");
            Assert.Equal("ok2", property.Value);

            source.SetPropertyValue("exist", "ok3");
            Thread.Sleep(10);
            Console.WriteLine("property: " + property + "\n");
            Assert.Equal("ok3", property.Value);

            source.SetPropertyValue("exist", "ok4");
            Thread.Sleep(10);
            Console.WriteLine("property: " + property + "\n");
            Assert.Equal("ok4", property.Value);
        }
        protected virtual IConfigurationManager CreateKeyCachedManager(MemoryDictionaryConfigurationSource source)
        {
            CascadedConfigurationSourceConfig <ConfigurationSourceConfig> sourceConfig = StringPropertySources.NewCascadedSourceConfigBuilder <ConfigurationSourceConfig>()
                                                                                         .SetName("cascaded-memory-map").SetKeySeparator(".").AddCascadedFactor("part1")
                                                                                         .AddCascadedFactor("part2").SetSource(source).Build();
            CascadedConfigurationSource <ConfigurationSourceConfig> cascadedSource = new KeyCachedCascadedConfigurationSource <ConfigurationSourceConfig>(sourceConfig);
            TaskExecutor taskExecutor = new TaskExecutor();
            ConfigurationManagerConfig managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("test")
                                                       .AddSource(1, cascadedSource).SetTaskExecutor(taskExecutor.Run).Build();

            Console.WriteLine("manager config: " + managerConfig + "\n");
            return(ConfigurationManagers.NewManager(managerConfig));
        }
示例#3
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);
        }