constructedValue element

The constructedValue element is used to construct a value in configuration file.

The constructedValue element can be used recursively in another constructedValue to specify the constructor parameter or injected property values.

Note

Refer to IoCConfiguration_constructedValue.xml for more examples on constructedValue element.

Below are some examples of using constructedValue element.

Example 1: Using constructedValue element to define a setting of type IoC.Configuration.Tests.ConstructedValue.Services.AppInfo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<settings>
    <object name="App1" typeRef="IAppInfo" value="1, App 1"/>

    <!--AppInfo is declared in element typeDefinitions, and references a type
        IoC.Configuration.Tests.ConstructedValue.Services.AppInfo. -->
    <constructedValue name="App2" typeRef="AppInfo" >
        <parameters>
          <int32 name="Id" value="2"/>
        </parameters>
        <injectedProperties>
          <string name="Description" value="App 2"/>
        </injectedProperties>
    </constructedValue>
</settings>

Example 2: Using constructedValue element to define a service implementation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<services>
    <service typeRef="IAppInfo">
        <valueImplementation scope="singleton">
            <!--Demo of constructedValue to provide an implementation
                for a service under valueImplementation element.-->
            <constructedValue typeRef="AppInfo">
                <parameters>
                  <int32 name="id" value="8"/>
                </parameters>
                <injectedProperties>
                  <string name="Description" value="App 8"/>
                </injectedProperties>
            </constructedValue>
        </valueImplementation>
    </service>
</services>

Example 3: Using constructedValue element to specify a value returned in autoProperty element

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<autoGeneratedServices>
    <autoService interface="IoC.Configuration.Tests.ConstructedValue.Services.IAppInfoFactory">
        <autoProperty name="DefaultAppInfo" returnTypeRef="IAppInfo">
            <constructedValue typeRef="AppInfo" >
                <parameters>
                    <int32 name="id" value="11"/>
                </parameters>
                <injectedProperties>
                    <string name="Description" value="App 11"/>
                </injectedProperties>
            </constructedValue>
        </autoProperty>
    </autoService>
</autoGeneratedServices>

Example 4: Using constructedValue element to specify a value returned in autoMethod element

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<autoGeneratedServices>
    <autoService interface="IoC.Configuration.Tests.ConstructedValue.Services.IAppInfoFactory">
        <autoMethod name="CreateAppInfo" returnTypeRef="IAppInfo">
            <default>
                <constructedValue  typeRef="AppInfo" >
                    <parameters>
                        <int32 name="id" value="12"/>
                    </parameters>
                    <injectedProperties>
                        <string name="Description" value="App 12"/>
                    </injectedProperties>
                </constructedValue>
            </default>
        </autoMethod>
    </autoService>
</autoGeneratedServices>

Example 5: Using constructedValue element as a parameter value in another constructedValue element

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<settings>
    <constructedValue
            name="DecoratedAppInfo"
            type="IoC.Configuration.Tests.ConstructedValue.Services.AppInfoDecorator">
        <parameters>
            <constructedValue
                    name="appInfo"
                    type="IoC.Configuration.Tests.ConstructedValue.Services.AppInfoDecorator">
                <parameters>
                    <constructedValue name="appInfo" typeRef="AppInfo">
                        <parameters>
                            <int32 name="id" value="25"/>
                        </parameters>
                        <injectedProperties>
                            <string name="Description" value="App 25"/>
                        </injectedProperties>
                    </constructedValue>
                </parameters>
            </constructedValue>
        </parameters>
    </constructedValue>
</settings>