Browse files

DRY up typed UserSession access

  • Loading branch information...
1 parent 989833a commit bb0335ef7baac92aeac176d919452689e51df349 @mythz mythz committed Sep 21, 2012
View
38 src/ServiceStack.FluentValidation.Mvc3/Mvc/ServiceStackController.cs
@@ -27,12 +27,6 @@ public override IAuthSession AuthSession
{
get { return UserSession; }
}
-
- public override void ClearSession()
- {
- userSession = null;
- this.Cache.Remove(SessionKey);
- }
}
@@ -65,16 +59,16 @@ public virtual ActionResult AuthorizationErrorResult
/// <summary>
/// Typed UserSession
/// </summary>
- private object userSession;
+ protected object userSession;
protected TUserSession SessionAs<TUserSession>()
{
- if (userSession != null) return (TUserSession)userSession;
- if (SessionKey != null)
- userSession = Cache.Get<TUserSession>(SessionKey);
- else
- SessionFeature.CreateSessionIds();
- var unAuthorizedSession = typeof(TUserSession).CreateInstance();
- return (TUserSession)(userSession ?? (userSession = unAuthorizedSession));
+ return (TUserSession)(userSession ?? (userSession = Cache.SessionAs<TUserSession>()));
+ }
+
+ public virtual void ClearSession()
+ {
+ userSession = null;
+ Cache.ClearSession();
}
/// <summary>
@@ -91,16 +85,7 @@ public new ISession Session
public virtual IAuthSession AuthSession
{
- get { return null; }
- }
-
- protected string SessionKey
- {
- get
- {
- var sessionId = SessionFeature.GetSessionId();
- return sessionId == null ? null : SessionFeature.GetSessionKey(sessionId);
- }
+ get { return (IAuthSession)userSession; }
}
protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
@@ -113,11 +98,6 @@ protected override JsonResult Json(object data, string contentType, Encoding con
};
}
- public virtual void ClearSession()
- {
- this.Cache.Remove(SessionKey);
- }
-
public virtual ActionResult InvokeDefaultAction(HttpContextBase httpContext)
{
try
View
26 src/ServiceStack.ServiceInterface/ServiceBase.cs
@@ -130,11 +130,7 @@ public ISession Session
if (SessionFactory == null)
SessionFactory = new SessionFactory(this.GetCacheClient());
- return session ?? (session =
- SessionFactory.GetOrCreateSession(
- RequestContext.Get<IHttpRequest>(),
- RequestContext.Get<IHttpResponse>()
- ));
+ return session ?? (session = SessionFactory.GetOrCreateSession(Request, Response));
}
}
@@ -144,25 +140,7 @@ public ISession Session
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));
- }
-
- /// <summary>
- /// The UserAgent's SessionKey
- /// </summary>
- protected string SessionKey
- {
- get
- {
- var sessionId = SessionFeature.GetSessionId();
- return sessionId == null ? null : SessionFeature.GetSessionKey(sessionId);
- }
+ return (TUserSession)(userSession ?? (userSession = this.GetCacheClient().SessionAs<TUserSession>()));
}
/// <summary>
View
27 src/ServiceStack.ServiceInterface/SessionExtensions.cs
@@ -3,9 +3,11 @@
using System.Linq;
using System.Net;
using System.Web;
+using ServiceStack.CacheAccess;
using ServiceStack.Common;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface.Auth;
+using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints;
using ServiceStack.WebHost.Endpoints.Extensions;
@@ -156,5 +158,30 @@ public static IHttpResponse ToResponse(this HttpListenerResponse listenerHttpRes
{
return new HttpListenerResponseWrapper(listenerHttpRes);
}
+
+ public static string SessionKey
+ {
+ get
+ {
+ var sessionId = SessionFeature.GetSessionId();
+ return sessionId == null ? null : SessionFeature.GetSessionKey(sessionId);
+ }
+ }
+
+ public static TUserSession SessionAs<TUserSession>(this ICacheClient cache)
+ {
+ if (SessionKey != null)
+ return cache.Get<TUserSession>(SessionKey);
+
+ SessionFeature.CreateSessionIds();
+ var unAuthorizedSession = typeof(TUserSession).CreateInstance();
+ return (TUserSession)unAuthorizedSession;
+ }
+
+ public static void ClearSession(this ICacheClient cache)
+ {
+ cache.Remove(SessionKey);
+ }
+
}
}

0 comments on commit bb0335e

Please sign in to comment.