Managing service containers in JavaScript

Service containers and processor endpoint configurations can be defined not only in the native Spring.NET XML notation, but also as JSON objects right in the scripts. The JSON notation resembles the Spring.NET XML, so if you already familiar with the XML containers you will find it easy to learn the JSON version.

To define a container in JSON, specify an object instead of XML file name in the 'from' argument of a createOrUpdate* function.

createOrUpdateServiceContainer({
    name: 'MyServiceContainer',
    configuration: {
        ... // Here will be the container definition.
    }
});


createOrUpdateProcessor({
    name: 'MyProcessor',
    configuration: {
        ... // Here will be the container definition.
    }
});


The attributes of the top-level object are the container's object definitions within the root {<objects>} element of the XML container. The attribute names become ids.

{
    obj1: { ... },
    obj2: { ... }
}


<objects>
    <object id="obj1" type="...">
        ...
    </object>
    <object id="obj2" type="...">
        ...
    </object>
</objects>


To specify a type of an object, use $type attribute.

{
    obj1: { 
        $type: 'AnObjectType.Full.Name, MyAssembly'
    }
}


<objects>
    <object id="obj1" type="AnObjectType.Full.Name, MyAssembly">
        ...
    </object>
</objects>


The attributes 'factory-method', 'factory-object' and 'prototype' can be defined by using the corresponding JSON attributes $factoryMethod, $factoryObject and $prototype.

{
    obj1: { 
        $type: 'AnObjectType.Full.Name, MyAssembly',
        $factoryObject: 'obj1Factory',
        $factoryMethod: 'Create'
    },

    obj1Factory: {
        ...
    }
}



Properties are specified as JSON attributes.

{
    obj1: { 
        $type: 'AnObjectType.Full.Name, MyAssembly',

        IntProperty: 1,
        BoolProperty: true,
        StringProperty: 'string',
        NullProperty: null
    }
}


<objects>
    <object id="obj1" type="AnObjectType.Full.Name, MyAssembly">
        <property name="IntProperty" value="1" />
        <property name="BoolProperty" value="true" />
        <property name="StringProperty" value="string" />
        <property name="NullProperty">
            <null />
        </property>
    </object>
</objects>


To define a reference property, use an object with $ref attribute.

{
    obj1: { 
        $type: 'AnObjectType.Full.Name, MyAssembly',
        RefProperty: { $ref: 'obj2' }
    },
    obj2: { ... }
}


<objects>
    <object id="obj1" type="AnObjectType.Full.Name, MyAssembly">
        <property name="RefProperty" ref="obj2" />
    </object>
    <object id="obj2" type="...">
        ...
    </object>
</objects>


Objects may be defined in-place as the values of properties.

{
    obj1: { 
        $type: 'AnObjectType.Full.Name, MyAssembly',

        ObjProperty: {
            $type: 'AnObjectType.Full.Name, MyAssembly'
        }
    }
}


<objects>
    <object id="obj1" type="AnObjectType.Full.Name, MyAssembly">
        <property name="ObjProperty">
            <object type="AnObjectType.Full.Name, MyAssembly">
                ...
            </object>
        </property>
    </object>
</objects>


To specify constructor arguments, use $constructor attribute. The value of the attribute may be either an array or an object.

{
    obj1: { 
        $type: 'AnObjectType.Full.Name, MyAssembly',
        $constructor: {
            arg1: 1,
            arg2: 'string',
            arg3: true
        }
    },
    obj2: { 
        $type: 'AnObjectType.Full.Name, MyAssembly',
        $constructor: [1, 'string', true]
    }
}


<objects>
    <object id="obj1" type="AnObjectType.Full.Name, MyAssembly">
        <constructor-arg name="arg1" value="1" />
        <constructor-arg name="arg2" value="string" />
        <constructor-arg name="arg3" value="true" />
    </object>
    <object id="obj2" type="AnObjectType.Full.Name, MyAssembly">
        <constructor-arg value="1" />
        <constructor-arg value="string" />
        <constructor-arg value="true" />
    </object>
</objects>


You can specify IList and IDictionary properties by using JavaScript arrays and objects (a JavaScript object without $type attribute considered a dictionary).

{
    obj1: {
        $type: 'AnObjectType.Full.Name, MyAssembly',
        ListProperty: [
            1,
            'String',
            { $ref: 'obj2' }
        ],
        DictionaryProperty: {
            'one': 1,
            'str': 'String',
            'obj2': { $ref: 'obj2' }
        }
    },

    obj2: { ... }
}


<objects>
    <object id="obj1" type="AnObjectType.Full.Name, MyAssembly">
        <property name="ListProperty">
            <list>
                <value>1</value>
                <value>String</value>
                <ref>obj2</ref>
            </list>
        </property>
        <property name="ListProperty">
            <dictionary>
                <entry key="one"><value>1</value></entry>
                <entry key="str"><value>String</value></entry>
                <entry key="obj2"><ref>obj2</ref></entry>
            </dictionary>
        </property>
    </object>

    <object id="obj2" type="AnObjectType.Full.Name, MyAssembly">
        ...
    </object>
</objects>    

Last edited Dec 23, 2013 at 12:07 PM by dbratus, version 3

Comments

No comments yet.