is using private shared objects/variables on class level harmful?
Thanks for your attention and time. I need your opinion on an basic architectural issue please.
In page behind classes I am using a private and shared object and variables (list or just client or simplay int id) to temporary hold data coming from database or class library. This object is used temporarily to catch data and than to return, pass to some function or binding a control.
1st: Can this approach harm any way ? I couldn't analyze it but a thought was using such shared variables may replace data in it when multiple users may be sending request at a time?
2nd: Please comment also on using such variables in BLL (to hold data coming from DAL/database). In this example every time new object of BLL class will be made.
Here is sample code:
public class ClientManager
{
Client objclient = new Client(); //Used in 1st and 2nd method
List<Client> clientlist = new List<Client>();// used in 3rd and 4th method
ClientRepository objclientRep = new ClientRepository();
public List<Client> GetClients()
{
return clientlist = objclientRep.GetClients();
}
public List<Client> SearchClients(string Keyword)
{
return clientlist = objclientRep.SearchClients(Keyword);
}
public Client GetaClient(int ClientId)
{
return objclient = objclientRep.GetaClient(ClientId);
开发者_JAVA百科 }
public Client GetClientDetailForConfirmOrder(int UserId)
{
return objclientRep.GetClientDetailForConfirmOrder(UserId);
}
}
I am really thankful to you for sparing time and paying kind attention.
If you are creating a new instance of this class for every page refresh, then it won't be a problem as each request will run with its own instances of all the classes.
If you are making new instance for each page and functions like GetClients() are heavy you might want to consider not running it for every page/session but use some caching mechanism instead.
On the other way around - if there is only one instance of this class system wide you need synchronization so one thread will not change data while other thread reading it.
精彩评论