예제 #1
0
        public void SettingGlobalValueChangeNotification()
        {
            string testSettingKey               = nameof(SettingGlobalValueChangeNotification);
            string initialValue                 = "initialValue";
            string notificationNewValue         = "";
            AmbientSetting <string> testSetting = new AmbientSetting <string>(testSettingKey, "", s => { notificationNewValue = s; return(s); }, initialValue);

            Assert.AreEqual(initialValue, testSetting.Value);
            string secondValue = "change1";

            Assert.AreEqual(initialValue, notificationNewValue);
            IAmbientSettingsSet        settingsReader  = _SettingsSet.Global;
            IMutableAmbientSettingsSet settingsMutator = settingsReader as IMutableAmbientSettingsSet;

            if (settingsMutator != null)
            {
                settingsMutator.ChangeSetting(testSettingKey, secondValue);
                Assert.AreEqual(secondValue, testSetting.Value);
                Assert.AreEqual(secondValue, notificationNewValue);
                // now change it to the same value it already has (this should not do anything)
                notificationNewValue = "";
                bool changed = settingsMutator.ChangeSetting(testSettingKey, secondValue);
                Assert.AreEqual(secondValue, testSetting.Value);
                Assert.AreEqual("", notificationNewValue, changed.ToString());
            }
        }
예제 #2
0
        public void AmbientServicesBasic()
        {
            ITest test = _Test.Global;

            Assert.IsNotNull(test);
            Assert.AreEqual(_Test.GlobalReference.Service, _Test.Global);
            IAmbientLogger logger = _Logger.Global;

            Assert.IsNotNull(logger);
            AmbientLogger <TestAmbientService> serviceBrokerLogger = new AmbientLogger <TestAmbientService>(logger);
            IAmbientProgressService            progressTracker     = _ProgressService.Global;

            Assert.IsNotNull(progressTracker);
            IAmbientSettingsSet settings = _SettingsSet.Global;

            Assert.IsNotNull(settings);
            IJunk junk = _Junk.Global;

            Assert.IsNull(junk);


            ITest compareTest = test;

            Assert.IsNotNull(test);

            int   changed     = 0;
            ITest updatedTest = test;

            Assert.IsNotNull(updatedTest);
            EventHandler <EventArgs> globalChanged = (o, e) => { updatedTest = _Test.Global; ++changed; };

            _Test.GlobalChanged += globalChanged;

            _Test.Global = null;
            Assert.AreEqual(1, changed);
            Assert.IsNull(updatedTest);
            compareTest = null;

            ITest disabledTest = _Test.Global;

            Assert.IsNull(disabledTest);

            _Test.Global = test;
            Assert.AreEqual(2, changed);
            compareTest = test;

            ITest reenabledTest = _Test.Global;

            Assert.IsNotNull(reenabledTest);

            _Test.GlobalChanged -= globalChanged;
        }
예제 #3
0
 public AmbientSettingsOverride(Dictionary <string, string> overrideSettings, string name, IAmbientSettingsSet fallback = null, AmbientService <IAmbientSettingsSet> settings = null)
 {
     _overrideRawSettings   = new ConcurrentDictionary <string, string>(overrideSettings);
     _overrideTypedSettings = new ConcurrentDictionary <string, object>();
     foreach (string key in overrideSettings.Keys)
     {
         IAmbientSettingInfo ps = SettingsRegistry.DefaultRegistry.TryGetSetting(key);
         if (ps != null)
         {
             _overrideTypedSettings[key] = ps.Convert(this, overrideSettings[key]);
         }
     }
     _name                  = name;
     _fallbackSettings      = fallback ?? settings?.Local;
     _weakSettingRegistered = new LazyUnsubscribeWeakEventListenerProxy <AmbientSettingsOverride, object, IAmbientSettingInfo>(
         this, NewSettingRegistered, wvc => SettingsRegistry.DefaultRegistry.SettingRegistered -= wvc.WeakEventHandler);
     SettingsRegistry.DefaultRegistry.SettingRegistered += _weakSettingRegistered.WeakEventHandler;
 }
예제 #4
0
        /// <summary>
        /// Converts the specified value for the specified settings set.
        /// </summary>
        /// <param name="settingsSet">The <see cref="IAmbientSettingsSet"/> for the implementation doing the conversion.</param>
        /// <param name="value">The string value for the setting.</param>
        /// <returns>A typed value created from the string value.</returns>
        public object Convert(IAmbientSettingsSet settingsSet, string value)
        {
            T ret;

            try
            {
                ret = _convert(value);
            }
#pragma warning disable CA1031 // this is a "do your best" kind of function, so we really do want to cactch all exceptions here
            catch
#pragma warning restore CA1031
            {
                ret = _defaultValue;
            }
            // is this settings set the one for this setting or is it the global settings set?
            if (settingsSet == _SettingsSet.Global)
            {
                System.Threading.Interlocked.Exchange(ref _globalSetAndValue, new SettingsSetSettingValue <T>(ret, settingsSet));
            }
            return(ret !);
        }
예제 #5
0
        protected (T, string) GetValueAndSet(IAmbientSettingsSet set)
        {
            object?value = set.GetTypedValue(_settingInfo.Key);

            return((value == null) ? (_settingInfo.DefaultValue, DefaultSettingsSet.Instance.SetName) : ((T)value, set.SetName));
        }
예제 #6
0
        protected T GetValueFromSet(IAmbientSettingsSet set)
        {
            object?value = set.GetTypedValue(_settingInfo.Key);

            return((value == null) ? _settingInfo.DefaultValue : (T)value);
        }
예제 #7
0
 public SettingsSetSettingValue(T value, IAmbientSettingsSet settingsSet)
 {
     this.Value       = value;
     this.SettingsSet = settingsSet;
 }
예제 #8
0
 public object Convert(IAmbientSettingsSet settingsSet, string value)
 {
     return(DefaultValueString);
 }