what is coldspring <map> & <list> eqv in wirebox?

261 Views Asked by At

I'm porting my Coldspring xml into Wirebox, but I'm stuck.

<map> in Coldspring can create a struct of singletons and then pass that struct into the 'bean' by constructor or setter. And <list> creates an array.

What do I write in Wirebox.cfc to do the same thing?

<bean id="Foo" class="com.foo">
    <constructor-arg name="something">
        <map>
            <entry key="apple">
                <ref bean="apple"/>
            </entry>                
            <entry key="banana">                    
                <ref bean="banana"/>
            </entry>                    
        </map>
    </constructor-arg>
</bean>

<bean id="bar" class="com.bar">
    <constructor-arg name="anArray">
        <list>
            <bean class="com.XX"/>
            <bean class="com.YY"/>
        </list>
    </constructor-arg>
</bean>
2

There are 2 best solutions below

0
On BEST ANSWER

Update: I have found a clean way to support this after reading how ColdSpring does it.

First, create a cfc with 2 methods:

component 
{
    struct function buildStruct()   {
        return arguments;
    }

    array function buildArray()     {
        var array = [];

        for (var index = 1; index <= arrayLen(arguments); index++)
            array[index] = arguments[index];

        return array;
    }
}

Then in wirebox config.cfc:

map("Factory")
        .to("com.util.wirebox.Factory")
        .asSingleton()
        .noAutowire();    

map("something")
        .toFactoryMethod(factory="Factory", method="buildStruct")
        .methodArg(name="apple", ref="apple")
        .methodArg(name="banana", ref="banana");

map("Foo").to("com.Foo").initArg(name="something", ref="something").asSingleton();

Original Answer:

Luis Majano's Answer:

// Map Binder so you can do utility methods
map("myBinder").toValue( this );
// Map the singleton maps
map("s1Map").toFactoryMethod("myBinder", "buildMap")
    .methodArg(name="mapType", value="1");


// Map A service with a singleton map
map("Service").to("path")
    .initArg(name="myMap", ref="s1Map");

He suggested me to file an ER, and here is it: http://coldbox.assembla.com/spaces/coldbox/support/tickets/1387-support-for--list--and--map--of-coldspring-xml

4
On

Let's break this down and see whats going on here.

1.) You are creating a new bean with an id of foo and its mapped to com.foo. To do this in wirebox use the map method and pass in whatever key you want to reference it by later. Next use the to method to map that key to a path
2.) You want to pass a map (structure) to the components init method. To do so just pass in a normal structure. This can be any value including other beans by using the initWith method.

<cfscript>
    map("Foo")
        .to("com.foo")
        .initWith({apple=apple,banana=banana})
</cfscript>

You can also download the Coldbox plugin for ColdFusion builder. This has a nice little utility in that will allow you to right click on a coldspring definition file and convert it to wirebox. Hope this helps.