Settings

The configuration file has two elements related to settings: /iocConfiguration/settingsRequestor and /iocConfiguration/settings

Note

See Plugins for details on settings in plugins.

Here is an example of settings in XML configuration file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
    <iocConfiguration>

        <settingsRequestor type="SharedServices.FakeSettingsRequestor"
                            assembly="shared_services">
        </settingsRequestor>

        <settings>
            <int32 name="SynchronizerFrequencyInMilliseconds" value="5000" />
            <double name="MaxCharge" value="155.7" />
            <string name="DisplayValue" value="Some display value" />
        </settings>
    </iocConfiguration>
  • Element settings lists all the settings using elements byte, int16, int32, int64, double, boolean, datetime, string, object. The values of settings are de-serialized using serializers provided in element parameterSerializers (reference section Parameter Serializers).

  • Element settingsRequestor is optional and if present, is used to force the user to include settings using the type specified in attributes type and assembly. The specified type should implement an interface IoC.Configuration.ISettingsRequestor, which specifies a collection of required settings that should be present in settings element.

    Note

    The type specified in type attribute in settingsRequestor element is fully integrated into a dependency injection framework. In other words, constructor parameters will be injected using bindings specified in dependencyInjection element.

Interface IoC.Configuration.ISettingsRequestor

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
    namespace IoC.Configuration
    {
        public interface ISettingsRequestor
        {
            /// <summary>
            /// Gets the collection of settings, that should be
            /// present in configuration file.
            /// </summary>
            [NotNull, ItemNotNull]
            IEnumerable<SettingInfo> RequiredSettings { get; }
        }
    }

Accessing Setting Values in Code

To access the setting values in code, inject the type IoC.Configuration.ISettings as a constructor parameter, and use the methods bool GetSettingValue<T>(string name, T defaultValue, out T value) or T GetSettingValueOrThrow<T>(string name) in IoC.Configuration.ISettings.

Here is an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    public class TestInjectedSettings
    {
        public TestInjectedSettings(ISettings settings)
        {
            Assert.IsTrue(settings.GetSettingValue<double>("MaxCharge", 5.3,
                                            out var maxChargeSettingValue));
            Assert.AreEqual(155.7, maxChargeSettingValue);

            Assert.IsFalse(settings.GetSettingValue<int>("MaxCharge", 5,
                                            out var settingValueNotFound_InvalidType));
            Assert.AreEqual(5, settingValueNotFound_InvalidType);

            Assert.IsFalse(settings.GetSettingValue<int>("MaxChargeInvalid", 7,
                                            out var nonExistentSettingValue));
            Assert.AreEqual(7, nonExistentSettingValue);

            try
            {
                // This call will throw an exception, since there is no setting of double
                // type with name "MaxChargeInvalid".
                settings.GetSettingValueOrThrow<double>("MaxChargeInvalid");
                Assert.Fail("An exception should have been thrown.");
            }
            catch
            {
            }
        }
    }

Note

Binding for a type TestInjectedSettings should be registered either in module class or in XML configuration file. Below is an example of registering binding for TestInjectedSettings in an IoC.Configuration module.

public class TestDiModule : IoC.Configuration.DiContainer.ModuleAbstr
{
    protected override void AddServiceRegistrations()
    {
        this.Bind<TestInjectedSettings>().ToSelf();
    }
}