[coldbox-3.5.1] Injection Happy

Ok…I think I might just be taking this too far. I get a little excited when I learn about different ways of doing things or thinking about doing things :slight_smile:

I have a session variable that is a simple model object…bean really, that I want to store some information in. I have thought that I’d map the object via wirebox mapping so that I can easily inject it. The problem I’m having is understanding this:

property name=“founduser” inject=“id:currentUser”;

This is defined in an interceptor (for example). Since the mapping for currentUser is scoped to session…what is component.founduser actually doing…is it a reference to the session variable. A copy of the mapped variable? Do I even really want to do the injection for non-transient variables?

In this interceptor, the mapped currentUser object has not been touched at the time the interceptor is first triggered…within the interceptor, the values are set in the object (in this case the property founduser). I expect now that currentUser would have the values it needs, but outside the interceptor, it does not seem to…but I bet the property founduser does.

So, my guess is I’m using injection wrong? Or overusing it? Perhaps misunderstanding it’s use? I mean, if I want a composite object, injecting non-transient models (services, beans, blah blah) is great, helpful, but something in an less “fancy” way I’ve been doing for a very long time. I guess I need to better understanding how injection works outside that particular box.

Mike

Injecting an object with a shorter lifespan (session in your case) into something with a longer lifespan or persistent object (the interceptor in your example) causes what is known as scope widening. Read about it and how to stop this from happening in the wirebox docs.

http://wiki.coldbox.org/wiki/WireBox.cfm#Scope_Widening_Injection

HTH,

Curt Gratz

Computer Know How

Injecting an object with a shorter lifespan (session in your case) into something with a longer lifespan or persistent object (the interceptor in your example) causes what is known as scope widening. Read about it and how to stop this from happening in the wirebox docs.

http://wiki.coldbox.org/wiki/WireBox.cfm#Scope_Widening_Injection

HTH,

Curt Gratz

Computer Know How

Instead of injecting the actual currentUser object that’s in the session scope, you can use a Facade object that’s also a singleton and defined in your Wirebox mapping that will get you the current user object when needed on the object that’s being injected.

I have a working example of this if you are interested.

I think I get it phil…but I would love to see an example of how you did that / what you are talking about.

Mike

Brad and Curt thank you so much.

So, I played around a little bit more and ended up with this interceptor (an adaptation of the multidomainses from forgebox):

component extends=“coldbox.system.Interceptor” {
property name=“hostservice” inject=“id:hostservice”;
property name=“foundhost” inject=“id:currentDomain”;

public void function preProcess(event, interceptData) {
var rc = event.getCollection();
var prc = event.getCollection(private=true);
var temp = hostservice.getByHost(rc.cgiscope.gethttphost());

prc.validationResults = validateModel( temp );
if( prc.validationResults.hasErrors() ){
getPlugin(“MessageBox”).error( prc.validationResults.getAllErrors() );
}
else{
foundhost.setaccount_id(temp.getaccount_id());
foundhost.setaccount_host(temp.getaccount_host());
foundhost.setaccount_created(temp.getaccount_created());
arguments.event.setSESBaseURL(“http://” & rc.cgiscope.gethttphost() & “/index.cfm”);
}
};
}

The most important thing I learned was that component.foundhost is a reference to the mapped wirebox variable. That makes this easier to understand (for me anyway).

The only thing I don’t like but can’t seem to work around is have to do the property sets manually to foundhost (by proxy to currentDomain). hostservice.getByHost(rc.cgiscope.gethttphost()) returns an identical DomainHost object (same class as currentDomain), but if I return that right to foundhost, currentDomain never gets the values:

component extends=“coldbox.system.Interceptor” {
property name=“hostservice” inject=“id:hostservice”;
property name=“foundhost” inject=“id:currentDomain”;

public void function preProcess(event, interceptData) {
var rc = event.getCollection();
var prc = event.getCollection(private=true);
/var temp = hostservice.getByHost(rc.cgiscope.gethttphost());/
foundhost = hostservice.getByHost(rc.cgiscope.gethttphost());
prc.validationResults = validateModel( foundhost );
if( prc.validationResults.hasErrors() ){
getPlugin(“MessageBox”).error( prc.validationResults.getAllErrors() );
}
else{
foundhost.setaccount_id(foundhost.getaccount_id());
foundhost.setaccount_host(foundhost.getaccount_host());
foundhost.setaccount_created(foundhost.getaccount_created());
arguments.event.setSESBaseURL(“http://” & rc.cgiscope.gethttphost() & “/index.cfm”);
}
};
}

I’m ok doing it to a temporary variable and doing the sets but I’d like an easier way. I’ve been learning about the populator functions but ended up extending my generic (non orm) models with a generalized base class that uses a set of memento methods.

Good idea, bad idea…better ideas? Up for any feedback or advice.

Mike