ASP.Net 中静态类的生命周期 [英] Life of a Static class in ASP.Net
问题描述
在我的 UI 层中,我在会话状态中存储了一些时区信息.
In my UI layer, I store some timezone information in the Session State.
我现在需要访问服务/业务/数据层中的时区.
I now need access to the timezone in my Service/Business/Data layers.
从我的 UI 层,我传递了登录用户的 UserID,我的数据库为该用户存储了时区信息.我不想用用户 ID 传递时区.
From my UI layer, I pass down the UserID of the logged on user, and my database has the Timezone information stored for that user. I would prefer not to pass the Timezone down WITH the user ID.
时区仅用于一些时间很重要的呼叫.(有项目开始了吗,今天有没有人请假……等等).
The Timezone is only used on a few calls where time is important. (Has a project started, is a person on leave today.. etc).
我在想可能有一个静态类,它在所有层(服务/业务/数据)上都被引用,它有一个时区信息字段.我希望它是静态的,以便我可以引用它:
I m thinking of maybe having a Static class, which is referenced on all layers (Service/Business/Data), which has a field for the Timezone info. I'd like it to be static, so that I can reference it:
var CurrentDate = CommonClass.GetLocatDateTime(_userId);
那可能会返回一个 DateTime
.
然后,如果 CurrentDate == null,则使用 UserId,从数据库中为该用户获取 TimeZoneId - 因此,数据库调用只会发生一次,并且只有在之前没有发生的情况下才会发生.
And then, if CurrentDate == null, using the UserId, go get the TimeZoneId from the database for that user - so, the database call will only happen once, and only happen if it hadn't happened before.
但是,静态类什么时候诞生"和消亡"?
But, when is the static class 'born' and 'killed'?
是每个用户的会话吗?还是在 Asp.Net 应用程序运行时?当我说一个用户会话时,我的意思是,每次用户单击某些内容时,都会创建一个新会话,因此,我的静态类将被创建吗?或者静态类对所有其他会话可见"吗?我希望它仅限于当前用户.
Is it per session of a user? Or is it while the Asp.Net application is running? When I say, a user session, I mean, each time the user clicks something, is a new session created, and therefore, my static class will be created then? Or is the Static class 'visible' to all other sessions? I want it to be limited to the current user.
推荐答案
但是,静态类什么时候诞生"和消亡"?
But, when is the static class 'born' and 'killed'?
您不需要类的引用来访问静态成员.所以永远没有出生或死亡.
You don't need a reference of the class in order to access a static member. So there's never a birth or a death.
是每个用户的会话吗?
不,它对您的整个应用程序是全局的,并在所有用户之间共享.
No, it is global for your entire application and shared between all users.
我希望它仅限于当前用户.
I want it to be limited to the current user.
然后忘记静态成员并使用会话.
Then forget about static members and use the session.
这篇关于ASP.Net 中静态类的生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!