views:

183

answers:

1

I was thinking of using StructureMap's profiles to facilitate offering up slight differences in behavior in my web app based on the authenticated user's type. My question is, if I do something like

ObjectFactory.Profile = Session["UserType"];

is it going to be thread-safe or will simultaneous requests potentially interfere with each other when resolving things based on the profile?

A: 

The operations on the static ObjectFactory facade are all "thread safe". This means that you can safely call them on different threads without corrupting the internal state of the ObjectFactory.

However, what you are asking is whether each thread gets its own personal copy of the ObjectFactory, and the answer is no. Any change you make to ObjectFactory on any thread, will be reflected in all other threads within the AppDomain.

The Profiles feature is probably not the solution you are looking for. You probably want to use something like named instances:

ObjectFactory.GetInstance<ISomeService>( Session["UserType"] );

There are other potential solutions, depending on what you are trying to do. Consider posting a question about the problem you are trying to solve, ex: "how do I get different behavior based on the current user's UserType..."

Joshua Flanagan
Thanks Josuah, but I'm curious why Profiles is the wrong feature? What if the whole graph is different based on what I set ObjectFactory.Profile to? Would your answer be different if instead my example was to set ObjectFactory.Profile equal to something hardcoded differently in controller actions (same potential threading issue which I see is not an issue)?
jayrdub
It IS an issue. I assume you are talking about a web environment. Every web request, from all users, share a single instance of ObjectFactory. If you change the Profile in a request for one user, any concurrent requests (for other users) will also have their Profile changed, which is obviously not what you want.If you really need to change the entire graph per request, you might be able to accomplish it using the new nested container feature. Again, I'm just speculating, without knowing the problem you are trying to solve.
Joshua Flanagan
Thanks, I guess I misunderstood you orig answer, it makes sense now.
jayrdub
Would you mind pointing me to where "the new nested container feature" is discussed. I've haven't found anything by that name, and am too new to SM to really know what you are talking about.
jayrdub
btw, thanks for your suggestion, I just asked this question...http://stackoverflow.com/questions/1499442/using-structuremap-to-get-different-behaviour-depending-on-a-users-type
jayrdub