0

We have a multi-company capable site which requires unique business logic for each company. We are using constructor dependency injection in our controllers, but would need to swap the unity container being used based upon a user's company. I was thinking that you could examine the user's cookie before setting the container for the current HttpContext. Is this even possible?

2
  • Do you mean that you don't run the websites of different companies in different environments? It is actually the same "instance" of the website for every visitor? Commented Jun 4, 2012 at 11:08
  • That is the intent. We could setup different front end sites for each company, but were hoping to integrate them all under the same front end so that the "super admins" can log into one tool and change company settings for whichever company they choose. Commented Jun 4, 2012 at 11:27

1 Answer 1

1

It's very doable. What I'd do is set up a "master" container, and then a child container for each company. That way you have default configuration in one place, and then you can customer per company easily without having to reconfigure every time. Save the child containers in some easily indexed way (a dictionary of company -> container, perhaps).

Then, write an HttpModule implementation that runs early in the pipeline to figure out which company the request is for. Use that to figure out the appropriate container to use. And from there you're pretty much set.

I would be worried as a customer of your system that you're not isolating my data sufficiently; wouldn't want to leak information across customers and get sued.

Sign up to request clarification or add additional context in comments.

4 Comments

I have to admit that what you call 'customer worries' gives me the creeps. A question about the technical part of the solution: You initialize the container (parent and child) in an HttpModule. How do you make it accessible to an ASP.NET Web Forms (not MVC) application? Where do you store it across requests? I understood that initializing the container (I'm referring to Unity here but I think its the same for other containers) is expensive so you would try to minimize the performance impact and initialize it only once.
I'd store the containers themselves in application state, since they're global to the app. Or you could just use global variables. The HttpModule would locate the correct container, and store it in a well known slot in HttpContent.Current.Items. The container would only actually be used out of HttpContext.Items; that way you're guaranteed to be using the correct container for this request.
@Chris. Are you suggesting to spin up the container per request? If you implement something like assembly scanning for discovery of your types to add to container, this could be pretty costly per request as opposed to application_start for example.
No, not at all. I'd create all the per-company child containers at startup and cache them in app level state. The HttpModule would be responsible for picking the correct one to use based on the request. It doesn't create the containers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.