providedValue element

The providedValue element is used to inject values that are resolved at runtime through custom logic rather than being hardcoded in the XML or resolved directly from the DI container. This is particularly useful for injecting external dependencies like loggers, session data, or environment-specific values.

Note

Refer to IoCConfiguration_providedValue.xml for more examples on providedValue element as well as related unit tests in ProvidedValue tests for more examples.

How it Works

When the configuration parser encounters a providedValue element, it attempts to resolve the value using a list of IValueProvider implementations passed during the initialization of the FileBasedConfiguration.

Setup in C#

To use providedValue, you must initialize the ValueProviders property in IoC.Configuration.DiContainerBuilder.FileBased.FileBasedConfigurationParameters.

// Example setup for ValueProviders
var configurationParameters = new FileBasedConfigurationParameters(
    configurationFileContentsProvider,
    entryAssemblyFolder,
    loadedAssemblies)
{
    // Initialize the list of value providers
    ValueProviders = new List<IValueProvider>
    {
        new MyCustomValueProvider(logger)
    }
};

// Use configurationParameters to build the DiContainer

The IValueProvider interface requires implementing TryResolveValue, which receives information about the target (type, name, and whether it’s a parameter, property, or setting) to provide the appropriate object.

XML Configuration Examples

The providedValue element can be used for module parameters, service constructor arguments, properties, and settings.

1. Injection into Modules

You can pass externally provided values into DI modules. This is useful for passing a pre-configured logger to a module that needs to register it.

<module type="Modules.IoC.DiModule3">
    <parameters>
        <!-- The value for 'diModule3_param1' will be resolved by a ValueProvider -->
        <providedValue name="diModule3_param1" type="System.Int32" />
    </parameters>
</module>

2. Injection into Services and Properties

It can be used within <services> to inject dependencies into constructors or via property injection.

<service type="IoC.Configuration.Tests.ProvidedValue.TestClasses.ITestInterface1">
    <implementation type="IoC.Configuration.Tests.ProvidedValue.TestClasses.TestInterface1_Impl1">
        <parameters>
            <providedValue name="logger" type="OROptimizer.Diagnostics.Log.ILog" />
        </parameters>
        <injectedProperties>
            <providedValue name="LoggerInjectedIntoProperty" type="OROptimizer.Diagnostics.Log.ILog" />
        </injectedProperties>
    </implementation>
</service>

3. Usage in Settings

providedValue can also define application settings that are resolved at runtime.

<settings>
    <providedValue name="Logger" type="OROptimizer.Diagnostics.Log.ILog" />
</settings>

4. Plugin Support

Plugins can also leverage provided values for their own initialization and settings.

 1<pluginSetup plugin="Plugin1">
 2    <!--The type in pluginImplementation should be non-abstract class
 3        that implements IoC.Configuration.IPlugin and which has a public constructor-->
 4    <pluginImplementation type="TestPluginAssembly1.Implementations.Plugin1">
 5        <parameters>
 6            <providedValue name="param1" type="System.Int64"/>
 7        </parameters>
 8        <injectedProperties>
 9            <providedValue name="Property2" type="System.Int64"/>
10        </injectedProperties>
11    </pluginImplementation>
12    <settings>
13        <providedValue name="Int32Setting1" type="System.Int32"/>
14        <int64 name="Int64Setting1" value="25" />
15    </settings>
16    <dependencyInjection>
17        <modules>
18            <module type="ModulesForPlugin1.Autofac.AutofacModule1" >
19                <parameters>
20                    <providedValue name="param1" type="System.Int32"/>
21                </parameters>
22            </module>
23            <module type="ModulesForPlugin1.IoC.DiModule1" >
24                <parameters>
25                    <providedValue name="param1" type="System.Int32"/>
26                </parameters>
27            </module>
28        </modules>
29
30        <services>
31            <service type="TestPluginAssembly1.Interfaces.IDoor">
32                <implementation scope="singleton" type="TestPluginAssembly1.Implementations.Door">
33                    <parameters>
34                        <providedValue name="color" type="System.Int32"/>
35                    </parameters>
36                    <injectedProperties>
37                        <providedValue name="Height" type="System.Double" />
38                    </injectedProperties>
39                </implementation>
40            </service>
41        </services>
42    </dependencyInjection>
43</pluginSetup>