Element autoProperty

Element autoProperty is used to configure the auto-generated implementation of a property in interface specified in element autoService.

An example of autoProperty elements is provided in Autogenerated Services. In this section some specifics of this element will be provided.

The Format of autoProperty

Property name and return type in autoProperty

  • Property name is specified using the attribute name.

  • Property return value type is specified using the attribute returnType and optional attribute assembly, or alternatively using attribute returnTypeRef to reference a type defined in some /iocConfiguration/typeDefinitions/typeDefinition element.

    Note

    Even though we only need the property name, to identify the property, the return type makes the configuration more readable. Also, the return type serves as an additional way to identify the property, if a property with similar name exists in multiple extended interfaces.

Specifying the returned value

The property auto-generated by IoC.Configuration will return the value specified in child of autoProperty element.

The child of autoProperty element should be a value initializer element, such as collection, int32, constructedValue, injectedObject, etc.

Note

Refer to Value Initialization Elements for more details on value initializer elements.

Example:

1
2
3
4
5
<autoService interface="IoC.Configuration.Tests.AutoService.Services.IActionValidatorFactory">
    <autoProperty name="PublicProjectId" returnType="System.Guid" >
        <object type="System.Guid" value="95E352DD-5C79-49D0-BD51-D62153570B61"/>
    </autoProperty>
</autoService>

Resolving conflicts by using declaringInterface in autoProperty

If the auto-implemented property with the specified name, and return type is not in auto-implemented interface, but is present in multiple parent interfaces, IoC.Configuration will report an error, since it will not know which property the configuration refers to.

In such rare cases an attribute declaringInterface can be used to specify explicitly the parent interface, where the property is declared.

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
29
30
31
32
33
34
35
<!--
IoC.Configuration.Tests.AutoService.Services.IMemberAmbiguityDemo demonstrates
cases when there are multiple occurrences of auto-generated methods and properties
with same signatures and return types in base interfaces of
IoC.Configuration.Tests.AutoService.Services.IMemberAmbiguityDemo.
-->
<autoService interface="IoC.Configuration.Tests.AutoService.Services.IMemberAmbiguityDemo">
    <!--
    Both IMemberAmbiguityDemo_Parent1 and IMemberAmbiguityDemo_Parent2 have properties called DefaultDbConnection
    with the same return types. We can auto-implement this property for each of these interfaces by using
    declaringInterface attribute in autoProperty element to explicitly specify the interface that own
    the property (declaringInterface can be used in autoMethod as well as demonstrated above)
    -->
    <!--Auto-implementation of IMemberAmbiguityDemo_Parent1.DefaultDbConnection-->
    <autoProperty name="DefaultDbConnection"
                  returnType="SharedServices.Interfaces.IDbConnection"
                  declaringInterface="IoC.Configuration.Tests.AutoService.Services.IMemberAmbiguityDemo_Parent1">
        <constructedValue type="SharedServices.Implementations.SqliteDbConnection">
            <parameters>
                <string name="filePath" value="c:\IMemberAmbiguityDemo_Parent1_Db.sqlite"/>
            </parameters>
        </constructedValue>
    </autoProperty>

    <!--Auto-implementation of IMemberAmbiguityDemo_Parent2.DefaultDbConnection-->
    <autoProperty name="DefaultDbConnection"
                  returnType="SharedServices.Interfaces.IDbConnection"
                  declaringInterface="IoC.Configuration.Tests.AutoService.Services.IMemberAmbiguityDemo_Parent2">
        <constructedValue type="SharedServices.Implementations.SqliteDbConnection">
            <parameters>
                <string name="filePath" value="c:\IMemberAmbiguityDemo_Parent2_Db.sqlite"/>
            </parameters>
        </constructedValue>
    </autoProperty>
</autoService>