Permalink
Browse files

Add helpful base class to fetch a typed session

  • Loading branch information...
1 parent c504b65 commit 55d15b3050cab43e8164965eb28bdac5fbfa292a @mythz mythz committed Aug 26, 2012
Showing with 26 additions and 0 deletions.
  1. +26 −0 src/ServiceStack.ServiceInterface/ServiceBase.cs
@@ -114,6 +114,9 @@ protected object AfterEachRequest(TRequest requestDto, object response)
return response.IsErrorResponse() ? response : OnAfterExecute(response); //only call OnAfterExecute if no exception occured
}
+ /// <summary>
+ /// Session Bag
+ /// </summary>
private ISession session;
public ISession Session
{
@@ -131,6 +134,29 @@ public ISession Session
}
/// <summary>
+ /// Typed UserSession
+ /// </summary>
+ private object userSession;
+ protected TUserSession SessionAs<TUserSession>()
+ {
+ if (userSession != null) return (TUserSession) userSession;
+ if (SessionKey != null)
+ userSession = this.GetCacheClient().Get<TUserSession>(SessionKey);
+ else
+ SessionFeature.CreateSessionIds();
+ var unAuthorizedSession = typeof(TUserSession).CreateInstance();
+ return (TUserSession) (userSession ?? (userSession = unAuthorizedSession));
+ }
+
+ protected string SessionKey
+ {
+ get
+ {
+ var sessionId = SessionFeature.GetSessionId();
+ return sessionId == null ? null : SessionFeature.GetSessionKey(sessionId);
+ }
+ }
+ /// <summary>
/// Resolve an alternate Web Service from ServiceStack's IOC container.
/// </summary>
/// <typeparam name="T"></typeparam>

0 comments on commit 55d15b3

Please sign in to comment.