Last post May 28, 2010 07:09 AM by toas1
May 28, 2010 02:54 AM|Yawer Iqbal|LINK
Thanks for your attention and sparing precious time, I need your help please.
In page behind classes I am using a private and shared object (list<client> or just client) 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
1st: Can this approach harm any way ? I couldnt analyse it but a thought was using such shared variables may replace data in it ?
2nd: Please comment also on using such variables in BLL (to hold data coming from database) where new object of BLL class will be made everytime.
Here is sample code for this:
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);
public Client GetClientDetailForConfirmOrder(int UserId)
May 28, 2010 04:55 AM|HeartattacK|LINK
As long as they're not static, it's not a problem. You are however wasting some constructor calls. For example:
List<Client> clientList = new List<Client>();
you could've just done:
List<Client> clientList = null;
since you're assigning a List<Client> to clientList in GetClient(), your newed up instance would be unreferenced and simply garbage collected when clientList is assigned in GetClient().
May 28, 2010 06:19 AM|Yawer Iqbal|LINK
I am very thankful to have answer from you. Thanks again.
After your replay a point is here to discuss.
Please consider using such way in page behind cs file (page's code file). As per my understanding we dont make an object of page behind class and it is not session dependant (please guide if I am misunderstanding).
In this case there will only one instance of object say clint. Now if in a single time multiple people access a page which uses this object will not values be lost or over witten ? Just as with the use of static members in static class ?
Suppose my page has List<Client> Clients =new List<Client> on class level.
On page load getting all client function, search client funtion and new clients funtion are accessing it in a single page. Consider multiple people accessing this page. Will a class levle shared member a problem ? It is ok or I should use seprate members
thanks once again.
May 28, 2010 07:02 AM|HeartattacK|LINK
An instance of page is created to serve each request. If you have the property specified as static, it's shared among all instances of the class (and thus, shared among all requests). If it's not static, then it's a different property for each instance (and
thus, each user).
May 28, 2010 07:09 AM|toas1|LINK
A simplified version: Each request are internally handled by IIS using a pool of HttpApplication objects and each incomming request are handled by an instance fetched from this pool. If you have many reqests arriving at the same time they will all be handled
by thier own HttpApplication object assigned to the request. The HttpApplication tries to locate a compiled version off the page to be handled, if no compiled version is found it will compile the page on the fly and create an instance off it and use it to
process the request. So, just as HeartattacK wrote there wont be any problems if you are not using static variables.