LightWire: A Little Less Lame

So, I love the concept of LightWire - a lightweight DI engine to provide the basic functionality of ColdSpring for those who prefer programmatic config files or like to put more responsibilities on their business objects and less on the service layer.

I love the fact that I solved the basic problem in under 200 lines of code, but the first cut required you to add two methods to a base class and to tweak your init() methods for all of your objects. It worked as a proof of concept but honestly it was a little lame.

Base classes have their place, but not as a requirement for a DI framework as it makes your application way too knowledgeable about and dependent on the framework, and having to tweak your init() methods completely breaks the benefit of a DI engine that the objects it instantiates don’t need to know anything about it.

So, I’ve fixed these dubious design decisions and I’m liking where it is going.

Mixing in the Base
I need every object I create to have a method that allows objects to be injected into its variables scope (as I don’t want to have to stick them into the THIS scope which would not be very shy). Instead of having an AddObject method in a base class, I have added an AddObjectMixin class in LightWire.cfc. When I create an object I just set:

<cfscript>
Local.ReturnObject.AddObject = variables.AddObjectMixin;
</cfscript>

to pass the method in. Simple, effective, and works like a charm. It is a very simple method.

<cffunction name="AddObjectMixin" returntype="void" access="public" output="false" hint="I add the passed object to the variables scope within this object.">
   <cfargument name="ObjectName" type="string" required="yes" hint="I am the name of the object to add.">
   <cfargument name="Object" type="any" required="yes" hint="I am the object to add.">
   <cfset variables[ObjectName] = Object>
</cffunction>

Dumping Dynamic Init()s
In my first implementation of LightWire I just used a base init class that would dynamically set all of the properties provided by LightWire. It is actually an interesting approach as it means you don’t need to put cfarguments in your init() methods. Want to add an extra parameter to your object? Just add it to your LightWire config file and you don’t need to repeat yourself by adding a cfargument to the init file.

However, the nominal benefits are completely outweighed by the fact that without a list of cfarguments in a methods init file you have no idea of the “contract” required to initialize the class making it much more likely you’ll pass only a subset of the required properties and get quirky runtime errors rather than a nice clean “argument missing” kind of error. It would also require the changing of all init() methods for existing class files just to make use of LightWire which is not a very practical design choice as it would also be making every class dependent on a DI engine with a dynamic init() method which doesn’t make a lot of sense.

So, I’ve simply removed that feature which means I need to go back in and add init methods and cfarguments into each of my class files, but I know it’s the right thing to do. Tested and working.

For anyone who is interested in the (really quite bad) idea of a “generic” base init method that will just take an argument collection and drop all of the elements into the variables scope so you don’t need to write all of your cfarguments in your init files, here is the (trivial) code to do it. It relies on a single struct being passed in, although with a little thought it could probably be rewritten to work directly with the arguments scope as a struct. Let me clarify this is code I have just REMOVED from LightWire. Just wanted a record of it somewhere in case I ever needed to do something funky with it.

<cffunction name="init" returntype="any" access="public" output="false" hint="I am the base init method required for LightWire to set all of the configured properties for each class.">
   <cfargument name="ArgumentStruct" type="struct" required="no">
   <cfscript>
      If (IsDefined("arguments.ArgumentStruct"))
         {StructAppend(variables,arguments.ArgumentStruct);};
   </cfscript>
   <cfreturn This>
</cffunction>

What’s Next?
Well, I’m going to need AOP, so I’m going to create a simple AOP implementation some time soon. I really don’t like XML config files (personal preference – I get why they are popular). That said, I WILL add an XML converter that will allow LightWire to speak a subset of ColdSpring so you’ll be able to port a project more easily between the two engines. In practice it may be a while before I add this as I have no personal use for it. If anyone wants a lightweight DI engine WITH XML config files, feel free to add the code and I’ll drop it into the project. If nobody else does this, I WILL add the feature as I need to learn how to work with XML files in CF – it seems pretty straightforward.

Comments
ccc's Gravatar [url=://www.topowerleveling.com]world of warcraft power leveling[/url] [url=://www.topowerleveling.com]wow power leveling[/url] [url=://www.topowerleveling.com]power leveling[/url] [url=://www.gowowpowerleveling.com]wow gold[/url] [url=://www.ac021.com]????[/url] [url=://www.paper-cup-machine.com]paper cup machine[/url] [url=://www.liandaauto.com/main.asp]Mass air flow[/url] [url=://www.pabx365.com]?????[/url] [url=://www.mystery.net.cn/index/index.php]????[/url] [url=://www.mystery.net.cn]????[/url] [url=://www.mystery.net.cn]???[/url] [url=://www.zhongke-china.com/product.asp]automatic rigid box line[/url] [url=://www.zhongke-china.com/product.asp]paper converting and wrapping machines[/url] [url=://www.zhongke-china.com/product.asp]automatic box makers[/url] [url=://www.zhongke-china.com/about.asp]rigid set-up gift box[/url],[url=://www.zhongke-china.com/about.asp]paperboard converting equipment[/url]


[url=://www.lszwjx.com]????[/url] [url=://www.lszwjx.com]?????[/url] [url=://www.lszwjx.com]????[/url] [url=://www.lszwjx.com/about.htm]?????[/url] [url=://www.lszwjx.com/about.htm]?????[/url] [url=://www.lszwjx.com/news.htm]????[/url] [url=://www.lszwjx.com/news.htm]????[/url] [url=://www.lszwjx.com/product.htm]????[/url] [url=://www.lszwjx.com/product2.htm]?????[/url] [url=://www.lszwjx.com/plist493.htm]?????[/url] [url=://www.lszwjx.com/product2.htm]?????[/url] [url=://www.lszwjx.com/plist490.htm]?????[/url] [url=://www.lszwjx.com/product2.htm]?????[/url] [url=://www.lszwjx.com/plist495.htm]?????[/url] [url=://www.ruian2machine.cn]???[/url] [url=://www.ruian2machine.cn]?????[/url] [url=://www.ruian2machine.cn]?????[/url] [url=://www.plastic-thermoforming-machine.com/product.htm]thermoforming Equipment[/url] [url=://www.packagemachinery.cn]bag making machine[/url] [url=://www.todesign.com.cn]????[/url] [url=://www.todesign.com.cn]????[/url] [url=://www.tzonegroup.cn]??[/url] [url=://www.tzonegroup.cn/about.asp]??????[/url] [url=://www.tzonegroup.cn/products.asp]???[/url] [url=://www.rajayj.cn]???[/url] [url=://www.rajayj.cn]?????[/url] [url=://www.ashuashi.com.cn]????[/url]


[url=://www.gowowpowerleveling.com]power leveling[/url] [url=://www.gowowpowerleveling.com]wow power leveling[/url] [url=://www.zhongke-china.com]paper box[/url] [url=://www.zhongke-china.com/about.asp]paper box equipment[/url] [url=://www.plastic-thermoforming-machine.com]thermoforming Equipment[/url] [url=://www.plastic-thermoforming-machine.com]Plastic Machinery[/url] [url=://www.plastic-thermoforming-machine.com/about.htm]Plastic Machine[/url] [url=://www.wzbtjx.cn]??[/url] [url=://www.wzbtjx.cn]????[/url] [url=://www.wzbtjx.cn]???[/url]
# Posted By ccc | 7/21/09 2:12 AM
links of london's Gravatar Thank you very much!
# Posted By links of london | 7/27/09 12:03 AM
ed hardy's Gravatar Thanks for your information!
# Posted By ed hardy | 8/5/09 12:13 AM
Guojuicycoutureout's Gravatar ://www.outletfactory-coach.net coach factory outlet
://www.outletcoach20l2.net coach outlet online
://www.coachoutlet20l2.com coach outlet
://www.coachs2011.com coach outlet online
://www.juicycoutureoutlet2012.net juicy couture outlet online
://www.outletcoach20l2.net Coach outlet store online
://www.coachoutlet20l2.com Coach outlet online
://www.coachs2011.com coach outlet store online
# Posted By Guojuicycoutureout | 11/27/11 10:24 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.5.006. | Protected by Akismet | Blog with WordPress