Rip's Domain

CFWheels and implementing a Service Layer

Posted in Uncategorized by rip747 on June 2, 2012

A couple of weeks ago Doug Boude (rhymes with loud) authored a post about providing a service layer in CFWheels. I’m not going to go into what a service layer is or the use of it. If you want to know that, check out his post here.

Personally I never saw I use for this before. In keeping with the MVC tradition of doing thing, I always felt that these `services` could live in the model layer. However I absolutely love when people make me fall on my sword and I can clearly say that a service layer is definitely something that is needed in CFWheels. I still wouldn’t go so far as using it with a DI like WireBox, but who knows if I’ll change my mind about that later in life as well.

Following Doug’s post, I was able to implement a service layer with ease. The issue I had with his approach though was that it made testing very awkward. Having the initialization of the service layer living in the controller init method, made it a little hard to test, not impossible mind you, but a little harder then it should have been.

After giving it some thought, I figured out a way to not only make it so that the services initialized when CFWheels booted, but also was able to make the service() method global so that you can easily test against it.

If you take a look at this gist I created you’ll see the changes I made. Basically all I did was move his initServices() method into the events/onapplicationstart.cfm file and move the service() method into the events/functions.cfm file. The big change I made though was to the initServices() method so that it would automatically initialize any new services that you add to the services directory.

By putting these methods into their new homes, the services are loading once when CFWheels boots as apposed to every time a controller is created and also you’re able to access the service() method from anywhere in your application which make it easy to call in tests.

Tagged with:

4 Responses

Subscribe to comments with RSS.

  1. spaghettichef said, on November 25, 2012 at 9:46 pm

    I’ve read his article and have been meaning to give it a try. Thanks for documenting these improvements!

  2. spaghettichef said, on November 30, 2012 at 8:08 pm

    Hey Tony, In trying this, I’m getting the following error…
    The method init was not found in component D:\wwwroot\services\Service.cfc.

    Did you add an init to the Service.cfc as well?

  3. rip747 said, on November 30, 2012 at 9:20 pm

    yes. but all it does is return the `this` scope.

  4. spaghettichef said, on November 30, 2012 at 9:45 pm

    Thanks, I did figure that out… but worth a mention here anyway🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: