Component scope="request"

Hello,

I had a question regarding the scope=“request” annotation that I have not found documentation for, although admittedly I could be misunderstanding it’s purpose.

I applied the annotation to a object that is called within an interceptor, and expected to see the populated object available in the request scope after the interceptor executed, but am finding nothing.

My understanding of scope=“request” was that an instance of the object would be available in the request scope. Can anyone point to some usage examples that might help me understand the scopes better?

Thank you,

Derek

It means that the “scope” of the object will the request, but it doesn’t necessarily mean that the object will be available as a top-level key of the actual request scope. I’d have to look to see where it’s actually placed (it could be inside a struct) but if you’re trying to directly access the object in the native scope, you’re “doing it wrong” :slight_smile:

To lay a ground rule, any time you want an instance of an object, you ask WireBox for it. WireBox is the gateway. And if you want to get the same object back every time you ask for a given request, then you set the scope="request" like you did and then…

myObj = getInstance( 'name' );
sameAsBefore = getInstance( 'name' );
yepStillTheSame = getInstance( 'name' );

But on the next request, it will be a fresh instance.

Before hitting send, I did take a quick look at the /coldbox/system/ioc/scopes/RequestScope.cfc and it DOES seem to just stick the object directly in the request scope using the key name wirebox:mappingName so if it’s not there, you’re either looking for the wrong key (dump the entire request scope to look) or the annotation isn’t getting processed (WireBox caches object metadata so ensure you’ve reinitted the app) or you’re somehow bypssing WireBox when creating the object.

Thank you for the response. I tested your case with my object and of course it worked correctly :smile: .

I think the issue I was having was an instance of scope widening injection. I injected wirebox into my singleton object and called my request scoped object via getInstance like you suggested and am now getting the behavior that I expected. Would this be an instance where using a provider would make sense?

Thanks again,

Derek

Yes, you are exactly correct. If you place a hard reference to your request-scoped object inside a singleton-scoped object, you will “widen the scope” of the object since the singleton will keep it in memory.

You are also correct that a provider would solve this as well since it injects an empty wrapper CFC (the provider) that does not contain a hard reference, but instead asks Wirebox for it every time internally. Which is basically the same as calling getInstance() every time, but does have bit less boilerplate for you.