For example, all of my DAO objects require a DSN to be passed into the
constructor. If I pass in the DSN using the LightWire config property
ConstructorProperties.dsn then I can just do the following in my
controller:
someDAO = application.lightwire.getSingleton("someDAO");
instead of
someDAO = application.lightwire.getSingletom("someDAO").init(application.dsn);
However (and this is my real question), If I leave off the constructor
invocation then my code looks like it doesn't require any values to be
initialized and is not very self documenting. Is that a problem?
What do YOU do in your apps?
You should not call the init() method in your code as LightWire takes care
of the initialization and gives you a single place to put and manage all of
your constructor properties (like DSNs).
One of the main roles of LightWire (or any DI engine) is to take care of
configuration properties, so if someone is looking at a code base using DI,
they will understand that they need to look at the config file for the DI
engine to see the init properties for the objects as when you are asking for
our singleton, you are NOT initializing it, you're just asking LightWire to
do whatever it needs to do to give it to you - fully formed.
Best Wishes,
Peter
My tought was that if I pulled LightWire out I would have to go back
through the entire application and add .init(#application.dsn#) to
every Gateway and DAO object invocation. That would be a real
headache, not that I would ever go back to not using a DI/ioC
framework.
-Aaron
That is true and something I was talking about a long time ago. If you
implement DI, you *are* dependent on DI because you *aren't* passing all of
the init info you need.
The good news is that you are not dependent on a specific implementation of
DI, so you could replace LW with your own custom DI engine without any
changes to the code other than the initial LightWire.getSingleton() call and
any getTransient calls which by their very nature are factory aware.
However, if it more than adding .init() to object invocations . . . If you
are using DI, you don't HAVE any object invocations within your code.
If you used to have:
Variables.UserService = createobject(whatever).init(whatever)
Userist = UserService.getAll()
Now you just have:
Userist = UserService.getAll()
The invocation disappears completely. The DI engine handles it.
Best Wishes,
Peter
That is exactly how I had it before.
> Now you just have:
> Userist = UserService.getAll()
>
> The invocation disappears completely. The DI engine handles it.
Not exactly. Now I have:
userService = application.lightwire.getSingleton("userService");
qUsers = userService.getAll()
The invocation is still there, just passed off to LightWire's
getSingleton method which invokes the object if it doesn't already
exist. Is there another way of doing it?
Yes there is another way. Inject UserService into your controller (or
whatvere is calling your user service). Not everyone does this, but if you
do, EVERYTHING gets wired up through your DI engine - makes life very nice
indeed!
Best Wishes,
Peter
=>=>Wishes Peter Bell would write a blog post on using CFCs for
controllers and some links to some good resources regarding it<=<=