Model-Glue 3 "Beans" Injection

One of my favorite features in Model-Glue, suggested and originally implemented by Sean Corfield, is autowiring. With autowiring, if you create a method named setSomeService() on a Controller, the framework will automatically search a ColdSpring bean factory for a bean named "someService" and set it into the controller.

It's super-cool, but it requires some explicit, repetitive code: writing nearly identical setters on your controller for the services it needs.

Model-Glue 3's code name is "Gesture," and it stands for declarative workflows and elimination of boilerplate. Clearly, something had to be done about all these setters that just serve as a marker for autowiring.

In Model-Glue 3, this "something" is a new attribute on the <controller /> tag called "beans":


Inside of each Model-Glue controller is a new scope called "beans" (really, it's just a struct in variables....). When the framework loads, it automagically sets any beans listed in the "beans" attribute of <controller /> into the "beans" scope, keyed by beanId, no setter required.

In other words, as soon as I reload the framework with the above <controller /> tag, I can do this in any listener function:

<cfset var contacts = beans.contactService.list() />

Fun stuff!

Related Blog Entries

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Sounds like a great idea, and I have one request. I'd prefer to have a getSomeService() available to me, rather than having to explicitly refer to beans.someService. Could you consider implementing a get() function automatically in addition to making the service available in the beans struct?
# Posted By Bob Silverberg | 1/15/08 9:42 AM
Joe, this seems like a great idea and of course, keeps the damned CFC file size down!

More configuration please! Excellent work!
# Posted By Mark Drew | 1/15/08 9:58 AM

getModelGlue().getBean("myBean") is the "classic" pre-autowiring way of doing it, and is available in all public versions of model-glue.

Is that what you're after?


Glad you like! It's the first of three or four new features that are in this vein.
# Posted By Joe Rinehart | 1/15/08 10:24 AM
Not exactly. When I am using Coldspring to inject dependencies into any cfc, whether controller or otherwise, I use a pair of methods. A setSomeBean() which Coldspring uses to inject the bean, and a getSomeBean() which I use when referring to the bean from within the cfc (rather than beans.SomeBean or getModelGlue().getBean("SomeBean")).

I realize now that what I'm looking for is to have the getSomeBean() method automatically generated in the controller, and I suppose that may not be possible (or worthwhile). I just figured that if you are removing the requirement to define the setSomeBean() function it would be great to not have to define the getSomeBean() function either, but still have access to it.

Would there be a way of doing that, or is that just pie-in-the-sky?
# Posted By Bob Silverberg | 1/15/08 10:42 AM
I like this. It seems like a nice shortcut.

# Posted By Dan Wilson | 1/15/08 11:05 AM
Great! Looking forward to seeing how you implemented this!
# Posted By Peter Bell | 1/15/08 2:55 PM

Ah, Ok, I see what you're saying now: generate the getSomeService() method when it's first called.

I thought about having a CF8-only feature that used onMissingMethod to do this, but I wanted to:

1. Keep the functionality consistent across versions
2. Not make pre-emptive use of onMissingMethod: something else may come up in the future where I'd kick myself for reserving it for autowiring purposes.
# Posted By Joe Rinehart | 1/15/08 4:05 PM
© 2020 Joe Rinehart
BlogCFC was created by Raymond Camden. This blog is running version