I’m using the ASP.NET session state server for one of my websites. I put a user object in session because it is used a lot.
Consider the following code fragment:
DoSomething(SessionUser.Me.UserID)
DoSomeMore(SessionUser.Me.UserName)
SessionUser.Me is a static property that just does something like:
return (SessionUser)Session["currentuser"];
Now because I access SessionUser.Me 2 times (DoSomething and DoSomeMore) is .NET smart enough to not do 2 roundtrips to the stateserver and deserialize 2 times?
With other words is it equivalent in performance if I just did:
var user = SessionUser.Me;
DoSomething(user.UserID)
DoSomeMore(user.UserName)
?
Session is deserialized from the state server early in the request pipeline (see HttpApplication.AcquireRequestState, and written back to the state server when execution of the page completes (see HttpApplication.ReleaseRequestState).
Thus, you shouldn’t incur a performance hit from repeated access.