I need an advice on piece of functionality that I am ought to implement. The scenario is that we haven an HttpHandler which servers to intercept file uploads. In the handler, I need to persist a large dictionary of strings inside the memory. The dictionary might be as large as 100 entries. I am wondering whether it is safe to store that in a static variable, so that it is not initialized every time instance of the handler is created (there will be a lot of instance for sure). In general, what is the approach in such scenarios. Is it a generally better idea to use static fields, to persist data that will not be changed?
Share
100 items in a dictionary isn’t really very big – in fact, that is barely getting into the size where hashing is faster than linear search. If it will never change once initialized, then static may work – personally I try to have some other abstraction between
staticand instance – for example a “context” or “configuration” class that I can pass into all the instances that need it. Then I can have multiple parallel configurations (if I need), but all the related instances can share a context/configuration – so no duplication.