I'm using asp.net (c#). I have a factory that uses a dictionary of keys that are mapped to objects types. I originally used a static variable to track the dictionary but realized it would be shared among all sessions. Then I'm left with the idea of passing the object map around everywhere, which seems messy.

Should object maps or other contextual items get passed around, stored in the session, or should I just create a new instance each time I need one (in the case of non-contextual items)? I'm not using an IoC container. An example of a non-contextual item in my case would be mapping text commands that come in from the client to objects.

In general it seems there is no good way to deal with contextual data without at least having a static object that pulls the values from a session. Otherwise I am stuck with passing tons of context objects around everywhere they are needed. Surely there is a good pattern for handling contexts, object maps, etc. without using session to store everything?

  • 1
    Text -> object fits into the interpreter pattern, though it's odd that you would want/need that. Otherwise session and/or passing objects around is the expected behavior. If you're passing things 'everywhere' though, there's likely another problem in the design.
    – Telastyn
    Apr 25, 2012 at 20:15
  • For a web app, I don't see any other way... The commands are set on the client side using javascript. Hidden form fields are set with the command (dashboard, etc.), date filter (last 7 days, etc.), and then on the server I have to map the command to a command object. How else would you do that without creating a map. Otherwise you have a nasty select case... If hidden form fields contains "report1" then create report1 object and execute... Whereas I just pull the object it needs from the map using the text as the key. Apr 25, 2012 at 20:38
  • If you're using ASP.NET MVC then the form variables get pulled into your app via model binding, or you can use REST to do the mapping without passing objects around. Frankly, taking text input from clients for this sort of thing sounds like the last solution that should be considered. And even if you do that, I don't see how it's being passed around... The endpoint takes the input, validates and adapts it to some concrete method call.
    – Telastyn
    Apr 25, 2012 at 22:05
  • If you saw the project you would understand. I don't use a lot of server controls. The user might click on a (javascript chart) data point for instance, this will raise an event and from there javascript might take that set of dates and put them in hidden form fields. They will need to be translated to an appropriate object on the server based on the type of date range, etc. The same goes for commands. Even if you used a server control or binding, you'd have to look up the command object in some factory. Anyway I'm not using MVC and I do appreciate your help. Apr 26, 2012 at 15:09

1 Answer 1


static variable to track the dictionary but realized it would be shared among all sessions. Then I'm left with the idea of passing the object map around everywhere, which seems messy.

Generally : cache data, but recreate objects on each request

Storing too much in the session is a terrible mistake; static variables tend to be even worse (especially with ObjectContext and DBContext)

Creating objects is not nearly as expensive as an IO operation and therefore isn't considered a spot you'd want to try optimizing by storing objects in the session. Although it is a good idea to make your object initialization do as little processing as possible. Look into Lazy to see the preferred way of delayed initialization.

  • If i don't cache my object map for instance though... then I would need to pass it around everywhere wouldn't I? So for instance... the page loads... I call registerdatefilters... It then creates a dictionary of keys and the types that go with them. Now I have this map that has to be passed around pretty much everywhere that uses a date filter. If I don't store it or make it static, it seems I would have no other choice? Is there a better way? Apr 25, 2012 at 20:33
  • 1
    If you're using MVC you can override IControllerFactory if and have it automatically property injected using unity or ninject (not sure why you can't use DI). You should also have the retrieval from the static variable living beneath your controller in another class so your controllers don't need to know if the dictionary is taken from the cache, static variable, or recreated on each page load. This will separate concerns and make the call you need to make a lot as short as possible.
    – brian
    Apr 25, 2012 at 20:40
  • I'm not using mvc unfortunately. Maybe I should just take the time to setup unity and start using it. I'm using it currently for a winforms app I'm working on, but have never used it with a web app. Sounds like though in the meantime if I going to recreate the map on every request, I'll never to actually return it from my business layer to my presentation layer and then pass around to places that need to access/create an object in my map. Apr 25, 2012 at 20:53

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.