3
I have a question about methods and static classes, given the code below:
static List<MaterialRCM> mr = new List<MaterialRCM>();
[Authorize]
public void AddMaterial(int Qtd, int Id)
{
mr.Add(new MaterialRCM(Id, Qtd));
}
Now, how are static classes and methods not types by reference, if two users use the same functionality runs the risk of having data from both users in the list? To be clearer, this method is called via Ajax, each time the user adds a material this method is called and adds the Material Id and quantity in the list, and if two users are requesting materials?
Well, how do you identify in this method which user?
– Leonel Sanches da Silva
Well... there is no identification, but I could put the user ID too, then there would be no problem because I would have the ID of the user who requested the right material? But then, the list would contain the objects of the two users?
– Alan Almeida
@I think the question is: static classes and methods are global for the entire application, or only exist by request in the MVC?
– bfavaretto
@bfavaretto are global for the entire application. The ideal for concurrent access is to put a
.lock()
on top of the object to be accessed.– Leonel Sanches da Silva
Yes users shared the same information, and beware this is bad practice!
– Maria
@user3670112 is easy for you to notice such behavior, have two browsers installed on your computer, open the first add items in that list. Then open the other one, you will notice that it will reflect to the other browser, so be careful with this not effective method
– Maria