WireBox

Overview

You can access any dependencies your component may have using WireBox, ColdBox's robust dependency injection framework.

This is a great way to keep your business logic out of your components.

You can achieve this by defining a CFC property in your component and using inject.

property name="postService" inject="PostService";
// ./models/PostService.cfc
component singleton {
    function getAll() {
        return queryExecute( "select * from posts" );
    }
}
// ./wires/Posts.cfc
component extends="cbwire.models.Component" {
    property name="postService" inject="PostService";
    function allPosts() {
        return postService.getAll();
    }
}
<!--- ./wires/posts.cfm --->
<cfoutput>
    <div>
        <h1>Posts</h1>
        <cfloop array="#allPosts()#" index="post">
            <div wire:key="post-#post.id#">
                <h2>#post.title#</h2>
            </div>
        </cfloop>
    </div>
</cfoutput>

CBWIRE uses WireBox internally to load your components. Any lifecycle methods fired by WireBox are available to you.

Getting Instances

You can use getInstance() to access a dependency from within your actions.

// ./wires/Posts.cfc
component extends="cbwire.models.Component" {
    function allPosts() {
        var postService = getInstance( "PostService" );
        return postService.getAll();
    }
}

Here is the method signature for getInstance():

/**
 * Get a instance object from WireBox
 *
 * @name The mapping name or CFC path or DSL to retrieve
 * @initArguments The constructor structure of arguments to passthrough when initializing the instance
 * @dsl The DSL string to use to retrieve an instance
 *
 * @return The requested instance
 */
function getInstance( name, initArguments={}, dsl )

Documentation

Learn about the full capabilities of WireBox at https://wirebox.ortusbooks.com/.

Last updated