Getting Started With LightWire

To get started with LightWire download the LightWire.cfc and the sample LightWireConfig.cfm from the SVN repository. You can place either file anywhere you want. I personally put the cfc in .com.lib (my library directory) and the config file in a "config" directory both of which are below my web root for a given project, but you can put the files anywhere you want.

Somewhere in your Application.cfc (in the onApplicationStart method) you’ll want to set the path to the LightWire config file path and then createobject LightWire. I currently just create it in application scope. Below is the config code I use which happens to use an application.CFMapping for the application specific mapping.

<cfset Local.ConfigFilePath = "/#application.CFMapping#/config/LightWireConfig.cfm">
<cfset Application.LightWire = CreateObject("component","#application.CFMapping#.com.lib.LightWire").Init(Local.ConfigFilePath)>

You will want to customize the config file. See my recent posting on the syntax for that.

That is just about it. If you want to create an object (transient or singleton), just call LightWire and it will return the fully loaded object. Below are a couple of sample requests. If you want a singleton, use getSingleton(). If you want a transient, getTransient(). All you need to pass in is the name of the class you want to instantiate.

<!--- Create PageService --->
<cfset Request.PageService = Request.LightWire.getSingleton("PageService")>

<!--- Create PageObject --->
<cfset Request.PageObject = Request.LightWire.getTransient("PageObject")>

I could have created a single get() method as it would be easy to tell from the config file whether it was a singleton or a transient, but I decided I’d rather have a really explicit interface which would make it very obvious in my code what I was doing. Please also bear in mind that most objects will just be direct injected, so a lot of things like PageService and PageDAO won’t ever be explicitly called – they’ll just be injected into the appropriate controller cfcs so in practice you only need to call LightWire occasionally (pretty much wherever you'd have called ColdSpring plus wherever you'd have called your custom bean factories if you need DI into the beans).

If you are new to DI/IoC, start off by downloading ColdSpring. It has better documentation, more support and many more people using it. LightWire is for people who have explored ColdSpring and have decided they’d like an approach more optimized for DI into transient objects and/or because they like programmatic config files.

Any comments/thoughts much appreciated!

Comments
Nick Tong's Gravatar Hi Peter, Maybe a short explanation of what a singleton and transient object might help people understand the code above a little more - just a thought?
# Posted By Nick Tong | 2/6/07 1:25 PM
Peter Bell's Gravatar Hi Nick,

Great cooment, and good point - thanks. There are some cfc's that you only need to create once. In effect they act as function libraries which may also contain some "global" variables (although hidden within the cfc). Examples of this would be a UserService for interactive with users (when you want to get a list of users, for instance), or a UserDAO for holding the SQL to get users from a database. These objects can just be created once and effectively stored within the application scope (although that is hidden and handled by Lightwire). These are "singleton" objects because you only need a single object for each of these.

Transient objects are those which live for a shorter period of time and which you may well create multiple times. For instance, if you were to get a list of users from the database for a particular user request, you might want to encapsulate that recordset in an object. That would be a transient object as its only job is to take the user list from the model and to pass it to the view. At the end of the page request the object is thrown away and then if you want a list of users down the line, you'll go back to the UserService and it'll give you a new user colleciton with the appropriate recordset.

So, a singleton is typically created once for the life of an application whereas transients are created and disposed of much more frequently - often on a per request basis.
# Posted By Peter Bell | 2/6/07 1:36 PM
Aaron Roberson's Gravatar Peter,

I am enjoying LightWire very much!

I am wondering though, should I scope all of my objects in the application or should I just call them in my controller when I need them? If it makes a difference, I am using Lazy Loading.
# Posted By Aaron Roberson | 2/26/07 7:33 PM
Peter Bell's Gravatar Hi Aaron,

Glad you're enjoying! Of those two approaches, just call the objects from LightWire. Put l/w into app scope and if you want UserService just write: UserService = application.LightWire.getSingleton("UserService").

However, here is something to think about. Why don't you just use LightWire to create your controllers and then inject the service methods into the controllers using LightWire so you don't need to call LightWire at all in the controller?! That's how I'm doing things in LightBase and it seems to be working out pretty well.
# Posted By Peter Bell | 2/27/07 10:17 AM
nick tong's Gravatar Hi Peter, this sounds interesting, do you have an example?? (I feel a blog post coming ;))
# Posted By nick tong | 2/27/07 10:25 AM
Aaron Roberson's Gravatar Blog Post... encore!

First off, I'm not even sure how to use a controller as a cfc. I usually just have a .cfm page with a prefix and the name of the calling page and do all of my traffic control there.
# Posted By Aaron Roberson | 2/27/07 10:30 AM
Peter Bell's Gravatar Hi Nick,

Lets say you want to run the Cart controller. You'd say Cart = application.LightWire.get("Cart"). In your config file, you'd inject (say) product, cart and user services to the Cart controller (or whatever it needed). Then in the cart controller if you wanted to get product info you'd call ProductService.getbyID("CartProductID") and if you wanted to save your cart you'd do something like CartService.save(Cart). Does that make any sense?
# Posted By Peter Bell | 2/27/07 10:36 AM
Peter Bell's Gravatar Aaron, Understood. Then stick with what is working for now and just call the cfc's using application.LightWire.getSingleton(ServiceMethodName) - moving your controllers to cfcs as well is the next step, but no compelling reason to jump in and do that today!
# Posted By Peter Bell | 2/27/07 10:37 AM
nick tong's Gravatar Perfectly, i just need to put it into practice to make it gel.
# Posted By nick tong | 2/27/07 11:14 AM
Aaron Roberson's Gravatar The controller CFC would be a transient, right?

I've wanted to go to using CFCs for my controllers for a long while, I just can't find any resources on learning to do so. I probably know enough already to do it, I just haven't really tried it yet and seeing some code would probably help get me over the edge.
# Posted By Aaron Roberson | 2/27/07 4:50 PM
Peter Bell's Gravatar I'll try to post somesample LightBase code as this is the approach I take there.

You *could* make the controllers transient, but I don't keep any state in my controllers, so I treat them as singletons so I don't need to re-init them every time.
# Posted By Peter Bell | 2/27/07 5:09 PM
Aaron Roberson's Gravatar Great! That would be much appreciated!
# Posted By Aaron Roberson | 2/27/07 5:19 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.5.006.