Blog

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 https://www.forgebox.io/view/coldbox-existing-code-blog.

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:

Map("people").to("com.mycode.people.accounts");

Map("accounting").to("com.mycode.accounting.accounting");

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.

Ramifications

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.

Conclusion

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 https://cfcasts.org 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.

Recording: https://cfcasts.com/series/ortus-webinars-2022/videos/getting-started-with-the-legacy-migration-with-dan-card

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!

Recording: https://cfcasts.com/series/ortus-webinars-2022/videos/legacy-migration-follow-up:-using-coldbox-with-an-existing-code-base

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.

https://intothebox.org/#workshops-2022

Add Your Comment

Recent Entries

ColdBox 7.2.0 Released

ColdBox 7.2.0 Released

ColdBox, a widely used development platform for ColdFusion (CFML), has unveiled version 7.2. Packed with compelling new features, bug fixes, and enhancements, this release is designed to empower developers by boosting productivity, refining scheduled task capabilities, and enhancing the overall reliability and efficiency of application development. This article will delve into the key highlights of ColdBox 7.2 and elucidate how these advancements can positively impact developers in their daily coding endeavors.

Luis Majano
Luis Majano
November 20, 2023
Into the Box 2023 Series on CFCast

Into the Box 2023 Series on CFCast

Excitement is in the air as we unleash the highly anticipated ITB 2023 series exclusively for our valued CFCast subscribers – and the best part? It's FREE for CFCast members! Now is the perfect time if you haven't joined the CFCast community yet. Plus, we've got an incredible End-of-Year deal that's too good to miss

Maria Jose Herrera
Maria Jose Herrera
November 20, 2023
Ortus Deals are Finally Here!

Ortus Deals are Finally Here!

The much-anticipated Ortus End-of-the-Year Sale has arrived, and it's time to elevate your development experience! Whether you're a seasoned developer, a tech enthusiast, or someone on the lookout for top-notch projects, Ortus has something special in store for you. Brace yourself for incredible discounts across a wide array of products and services, including Ortus annual events, books, cutting-edge services, and more.

Maria Jose Herrera
Maria Jose Herrera
November 15, 2023