RE: [coldbox:11217] wirebox scope question

I see. I’m not sure how to answer that other than that’s not how services generally are expected to work. Don’t think of the service as an object that services a single user, but something more abstract that contains NO DATA specific to a given user session. If you are going to store the logged in user in your user Service, then you certainly don’t want your user service to be a singleton- you would need a separate instance of the user service for every user. I wouldn’t recommend that though.

If you are simply looking for a place to store the logged in user, put them into session storage. If a method in the user service needs access to the logged in user, then pass in the user as an argument to that method, or have the method retreive the logged in user (in a locally scoped variable) as it needs it from session storage. Most apps would only have a single user service that lives in the application scope and it doesn’t hold any persisted references to any users directly.

If you have data/logic in your user service that stores information specific to a given user, then that data/logic probably should be in the user bean, not the user service. You seem to have blurred the lines between the user bean and service and made them both session-specific— but at that point why do you have both of them?

This is how I usually break it down in my head:
user bean: This holds ALL data directly describing a user and all methods representing actions a user can take which generally only require information directly contained within the user bean.
user service: This CFC has no “instance” data, but handles actions that involve a given user bean in combination with other objects or services in the site. i.e. logging in/out a user, creating a new user, pulling a list of users that meet certain criteria.

Note however that last paragraph is starting to border on my OO preference and I’m not saying everyone sees eye to eye on that. To go a little further, I don’t actually even have a user service. I have a contact manager service which services ALL the objects related to a contact including companies, addresses, communications etc. I personally feel that having a service for every bean leads to anemic services. My contact manager service is where I put logic that cross-cuts multiple objects related to a contact. (but that service never directly stored any of those objects)

Anyway, sorry to get off so far on theories and my personal preferences, but the crux of what you need to get out of this is that your issues are directly related to the fact that your user service is set up to be a singleton, but it is storing data related to a specific user. You either need to abandon the singleton behavior, or make your service session/agnostic.

Does that help?

~Brad

Brad,

I get it. Having that struct and bean defined in the service and having the service defined as a singleton is causing my issue. I will move that logic into the handler using local variable and write the data to the session (which it is already is but where it is doing it is causing a conflict) and all should be good.

This part of the code I inherited and it’s the only part that uses this. The rest of the time I pass in what I need to the method. I apperciate you confirming this. It’s what I thought but wanted to confirm before I undertook the ask of rewriting it.

Thanks for your time.

Jonathan