ColdSpring and Dependency Injection for the beginner part 1
First off let me caveat this post by saying that I am no an OO guru, if you are looking for a more indepth look at DI, IOC, Constructors, setters/getter etc then you should really check out sites like Peter Bells site, Application Generation.
Ok, I am going to make this first post small (I know, I know boring chat first code later) and just give a little background in preparation for the next part which will be the code (I promise). This post also isn't for the total beginner to Coldspring. Really at this point you should know why you want to use Dependency Injections(DI) and roughly understand or know the benefits when it comes to a ColdSpring.xml file. If you are looking for an introduction to ColdSpring go to the docs section of the CS site, honestly they are really good and the introduction explains a lot.
So, you should know that ColdSpring is an inversion-of-control framework for CFCs. What does this mean, it handles dependencies. For example, if you have a userService that needs (depends on) an userDAO instead of having to initiate the components(beans) via CF code in the right order like:
you can just tell the ColdSpring.xml file to do this for you (and in any order)
I'm not expecting you to understand the above XML but just to comprehend what is roughly happening. I mean, if you are reading this post then you should also understand why we are doing this in the first place. It means that we don't have to worry about dependencies throughout our applications. Because of our xml configuration file we now know that when we call userService it will always have userDAO available.
So thats really it. Where am I going next then in my next post..... Well, when it comes to DI there are two methods of injecting your beans (components/CFC's)
- Constructor Injection and
- Setter Injection
From the ColdSpring site:
This tag will cause Coldspring to supply your bean with a value or object reference when it is instantiated (during a CFC's init() method), passed as an argument named via the name attribute.
<property name="propertyName" />
Similar in nature to constructor arg, however in this case ColdSpring will pass some value or object reference into your bean as an argument to a setter method, identified via the name attribute. Thus, your CFC must have a setter method name that matches the property tag's name attribute (for example if your property is named "foo" then your CFC needs a setFoo() method).
Which one too use is your choice and as with any choice there are two sides. All I am going to do in my next post is cover how to implement these from within ColdSpring and then let you choose the one that suits your project/style best. If you do want a little read on this subject then check out Peter Bell's site. It has a good post, which if you read Peter Farrell's comments as well it covers both sides of this injection coin. Constructor vs. Setter Injection: Constructor is Better.
Don't forget for more information check outcoldspringframework.org, or the mailing lists
To subscribe, send an email:
Subject: SUBSCRIBE firstname.lastname@example.org. I do know I said this was going to be a small post :o)