What's the precise differences between the fol

2019-05-21 10:03发布

问题:

What's the precise differences between the following three lines in a MVC controller inheriting from ServiceStackController?

(I cannot find the difference explained in any documentation)

//A - (default: reload = true)
var session = GetSession(); 

//B
var session = GetSession(false);

//C
var session = SessionAs<IAuthSession>();

回答1:

GetSession is better named GetOrCreateSession as it will either Get a Typed Session or create a new one if it doesn't exist. It also stores the instance of the Session in HTTP Request Context where if reload:false will return the local instance when it exists:

IAuthSession session = GetSession(reload:false);
IAuthSession session = GetSession(reload:true);

If reload:true it will always retrieve the Session from the underlying ICacheClient.

SessionAs<T> always gets the Session from the ICacheClient and returns an empty instance if it doesn't exist. It also returns a typed version of your Custom AuthUserSession:

CustomUserSession session = SessionAs<CustomUserSession>();