Dan Card

August 18, 2022

Spread the word

Share your thoughts

Recently, I did a webinar on Refactoring Legacy Code and the question came up about whether or not it was possible to use ColdBox with existing code without converting everything to a ColdBox module or making changes to the existing codebase.

In the first installation in this series, we took a tour of the various elements which make up ColdBox.

In the second installation, we looked at creating layouts, views, and routes in the main site.

In the third, we created a module and did the first integration of our existing code into our ColdBox site.

In the fourth, we continued devloping our module with a handler and passing variable back to Coldbox.

See the previous posts about setting up the sample code from Forgebox at

Method 5: Using Wirebox

As we briefly mentioned before, WireBox is the Dependency Injection engine which is used in Coldbox. The analogy used was that it was a large warehouse of components, values and other items which your code can use. Some of these items are available automatically because they are pre-configured in ColdBox but you can also define your own items to be called. In this method, we are going to define our people.cfc and accounting.cfc in WireBox and then will be able to reference them in a much easier way.

Step 1: Configure Wirebox

In the /coldboxsite/config folder, open the Wirebox.cfc file. In the configure method, after the wirebox: {}, you’ll see a comment saying //map bindings below. As you might have guessed, we’re going to map our items below this.

Add these two lines:



You will need to ?fwreinit=1 to reload WireBox.

Step 2: Add a property in the handler myco.cfc

Just inside the component, add property="people" inject="people";

The inject keyword tells ColdBox that when the handler is created, to obtain a reference to whatever WireBox has defined as "people" and inject it into the variables scope of the component.

Step 3: Duplicate the method passingVariables and call it wirebox1.

Make these modifications in it:

public function wirebox1( event, rc, prc ){
    event.setPrivateValue( "people", people.allClients() );
    event.setPrivateValue( "balances", wirebox.getinstance("accounting").allAccounts() );
    event.setView( "home/passingVariables" );

In the first line, we are using the injected component called people which WireBox knows references the com.myco.people.accounts component.

In the second line, we are calling wirebox directly to "get an instance" of accounting.

These are two ways of doing the same thing. WireBox can manifest many types of items including components, Java elements, web services, RSS feeds, or any type of data as transient, singletons or many other configurations.

We can keep the same view for this method since we aren’t making any significant changes in the view.

Step 4: Create route("/wirebox1","myCo.wirebox1")

You will need to ?fwreinit=1 to reload ColdBox to reload the routes.

Method 6: Moving Your Logic into a module

In this method we’re going to move some logic from our existing codebase into our module.

Step 1

Copy the Accounts.cfc and the Accounting.cfc and place them into the /models folder of our myco module.

Step 2

Create a route to /wirebox2 by putting route( "/wirebox2", "myCo.wirebox2" ); in the configure() method in /config/Router.cfc in our module.

Step 3

In our handler myco.cfc file, we need to add two properties at the top of the file

property name="accounts" inject="accounts@myco";

property name="accounting" inject="accounting@myco";

Notice the two ways this is different from the previous method.

Firstly, we didn't have to configure WireBox by manually creating a mapping. Since accounts.cfc and accouting.cfc were in our /models folder and our ModuleConfig.cfc file had this.autowire=true, ColdBox created a mapping for us.

Secondly, notice the @myco as part of the mapping. This is to make it clear we want the accounts model from the myco module. This is a great way that our code can access other modules, whether they are our own or by someone else. It doesn't matter if the other module is in a custom location, in the modules or the module_app folder or whatever. Our code just asks WireBox which makes it much more portable.

Step 4

Create the function wirebox2() which looks like this:

public function wirebox2( event, rc, prc ){

   event.setPrivateValue( "people", accounts.allClients() );
   event.setPrivateValue( "balances", accounting.allAccounts() );
   event.setView( "home/passingVariables" );

This is nearly identical to the wirebox1() method. We're still accessing a component which has been injected by WireBox but in this case, the models were autowired by ColdBox and our cfcs are actually in a ColdBox module.


Now that we've gotten into using WireBox, there are a couple of things that might be coming apparant. We can now ask WireBox for code from almost anywhere. We can map to our existing codebase as well as other modules. We might want to examine our current codebase and see if it can be cleanly separated "vertically" meaning separating the code based on it's business function such as login, payments, searching, archiving or whatever. Each site might be different in this respect. A "vertical" concern might be easily encapsulated as a Coldbox module with its own views, models, layouts and routes.

One way that this framework can make it easier on the developers during a time a transition is being able to change the code to which a mapping refers. For example, let's say that we are not going to move the "scary blob of code" in accounting for quite a while. We can create a mapping in WireBox of SBOC which refers to com.myco.account.doNotEverTouchThisEver. Our devs just inject SBOC in their code and move on with life. Later when we actually to move the scary blob of code to a coldbox module of it's own, we can simply have SBOC now refer to the new module and the devs who didn't work on it will never know the difference.


Hopefully this has been an interesting and helpful series. We've dived into some of the details about ColdBox and also investigated WireBox to a small extent. This is hopefully enough to get you started but there is a great deal more to both of these packages and I'd encourage you to check out the content on them or attend one of the trainings such as Coldbox: Zero To Hero / Super Hero / MegaHero.

Did you miss the June Webinar - Getting started with the Legacy Migration with Dan Card

We will look at the process of converting legacy .cfm based sites into a more modern coding design which has less overall code, is easier to maintain and manage, mistakes and errors can more readily and speedily identified and fixed, and is easier to read.


Did you miss: Legacy Migration Follow Up: Using Coldbox with an Existing Code Base with Dan Card

July 29th 2022: Time 11:00 AM Central Time ( US and Canada )

Dan Card will present a follow-up to his June webinar: Getting started with the Legacy Migration. Dan received some good questions, so July's Webinar: Legacy Migration Follow Up: Using Coldbox with an Existing Code Base with Dan Card. If you have a more traditional/legacy codebase and want to modernize with ColdBox but don't know where to start, this webinar is just for you!


Find out more about Dan Card's workshop at Into the Box - Legacy Code Conversion to the Modern World

This one-day workshop will focus on converting legacy .cfm based sites into a more modern coding design that has less overall code, is easier to maintain and manage, mistakes and errors can be more readily and speedily identified and fixed, and is easier to read.

Add Your Comment

Recent Entries

Ortus Solutions Soars Back to CFCamp Munich as Platinum Sponsor and Keynote Presenter!

Ortus Solutions Soars Back to CFCamp Munich as Platinum Sponsor and Keynote Presenter!

We're thrilled to announce that Ortus Solutions is returning to CFCamp Munich for another year! We're incredibly proud to be a continuing sponsor of this fantastic event and can't wait to connect with the ColdFusion community once again.

Taking Center Stage with the Day 1 Keynote

Get ready for some deep dives into the world of BoxLang! Ortus Solutions will be kicking off CFCamp with a dynamic keynote address on day 1. This is your chance to gain valua...

Cristobal Escobar
Cristobal Escobar
May 28, 2024
Into the Box 2025 - Blind Tickets are Out!

Into the Box 2025 - Blind Tickets are Out!

Ortus Solutions is thrilled to announce the release of blind tickets for Into the Box 2025, the premier event for web developers. This year's conference promises to be a game-changer, especially with the unveiling of our groundbreaking new product, BoxLang. **Our team is working hard to get all modern CFML developers amazing tools and software features to improve their productivity. **

Maria Jose Herrera
Maria Jose Herrera
May 27, 2024
Into the Box - Keynote Recap Day 1

Into the Box - Keynote Recap Day 1

Day 1 of Into the Box kicked off with an electrifying keynote session that set the tone for an incredible conference. The excitement was palpable as developers, tech enthusiasts, and industry leaders gathered to explore the latest innovations and trends in web development.

Maria Jose Herrera
Maria Jose Herrera
May 24, 2024