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

北城以北 提交于 2019-12-10 10:44:49

问题


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


来源:https://stackoverflow.com/questions/27673012/whats-the-precise-differences-between-the-following-three-lines-in-a-mvc-contro

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!