简体   繁体   English

在类级别使用私有共享对象/变量有害吗?

[英]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. 在类后面的页面中,我使用私有和共享的对象和变量(列表或仅客户端或simplay int id)临时保存来自数据库或类库的数据。 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). 第二:请也评论在BLL中使用此类变量(以保存来自DAL /数据库的数据)。 In this example every time new object of BLL class will be made. 在此示例中,每次都会创建BLL类的新对象。

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);
    }

    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 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. 如果要为每个页面创建新实例,并且诸如GetClients()之类的功能很繁重,则可能要考虑不在每个页面/会话中运行它,而是使用某种缓存机制。

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. 另一方面,如果只有这个类系统范围的一个实例,则需要同步,因此一个线程不会更改数据,而另一个线程则可以读取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM