Click here to Skip to main content
15,919,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have an web api application. I've created a static class with some static properties in that application which is storing few cookies value. Now my question is suppose if 4 users are using concurrently this api (by using 4 tabs in same browser) then will user1 data can get replaced by user 2/3/4?
If yes then what is the preferred way to store those data. I'm using Autofac DI.
Thank

What I have tried:

I tried to create global variable and creating object of that class each time
Posted
Updated 30-Dec-19 20:50pm

1 solution

Static data is just that: static. There is one copy for the whole application (in a similar way to Global Variables in other languages).

If your static data contains user specific information, it will fail because it can't contain different information depending on which user is "active" within the app.

Ignore computers for a moment, and let's talk about cars.
All cars have a colour - but which colour it is depends on which specific car you are talking about. My car is black; your car is red; this car is green; that car is blue. Colour is an instance property of the Car class because you need to have a specific instance of a Car in order to ask the question "what colour is it?" - you can't say "what colour is a car?" because it's meaningless without saying which car you mean.
But cars have static properties as well: you can ask "how many wheels has a car?" because all cars have four wheels. (If it had two, it would be a motorbike, not a car!)

As soon as you start proliferating static data, the chances are your design is wrong: a well designed app will often have little or no static data at all - perhaps a connection string to a DB, maybe a URL to a website. But if you start storing information about a specific user in static data, then that is almost certainly wrong and will cause you problems.

I'd go back to your design, and work out why you think this data has to be static - and if that reason is "so this static method can access it" then you need to look at "why is this method static at all?" rather than "how can I get instance data into a static method?".
static methods should only ever work with data that is not instance related, or that can be disassociated from any specific instance and passed to the method via parameters.
 
Share this answer
 
Comments
User-8621695 31-Dec-19 2:59am    
@OriginalGriff Thank you so much for clearing my doubt about static classes and which data we should store in static way.
Now my doubt is if we have to store cookies data which will be different for each user and can be updated somewhere in mid of running application what should I use?
OriginalGriff 31-Dec-19 3:01am    
Cookies? They are stored on the client, not in your app!
User-8621695 31-Dec-19 3:03am    
yes correct but there are few data which we are storing in cookies need to store in our application level as well.
OriginalGriff 31-Dec-19 3:12am    
No, you don't. You fetch them from the client into local variables when you need them, you don't store them in your app, because you can't even guarantee that the app will be running in two minutes time.
Each time the client makes a post to the server, it's as if the app was run for the first time - you can't rely on anything existing from previous posts unless they are stored in the Session - and that expires after 20 minutes under normal circumstances as well!
You should certainly never, ever using static data in a website - you have no idea how many different clients will "share" it - could be just one, could be dozens!

I think you need to take a long hard look at your design, you don't seem to understand how websites and the codebehind work at all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900