2

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>();
Darren
  • 9,014
  • 2
  • 39
  • 50
  • If you want to see the *precise* difference look at the docs again, [it's in there](https://github.com/ServiceStack/ServiceStack/blob/a194da0221862e14a1ce27e3f8b216257538c899/src/ServiceStack/ServiceStackProvider.cs)! – ChiefTwoPencils Dec 28 '14 at 01:48
  • Thanks, but that's not documentation, it's source code (not the final class either). If it is not obvious within a few minutes then it is worth asking a question to have it documented. – Darren Dec 28 '14 at 02:56
  • I won't bother arguing whether code represents a form of documentation or not; seems pretty obvious to me. I will say it's in there again, in what you strictly refer to as documentation. I didn't link to it because you don't get more "precise" than the code. As far as the link goes I apparently navigated away, can't edit now - apologies. – ChiefTwoPencils Dec 28 '14 at 03:21

1 Answers1

0

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>();
mythz
  • 141,670
  • 29
  • 246
  • 390
  • For people reading, the above means I will generally use var session = GetSession(false); in a controller because I don't want to unnecessarily go to the cache when if it's already in the HTTP context. Of course it will likely make zero difference. – Darren Dec 28 '14 at 06:49