S
shapper
Hello,
I am trying to create a common "SessionProvider" that a SessionService
use.
I came up with the following Session provider:
// SessionProvider
public class SessionProvider<Child> where Child :
SessionProvider<Child>, new() {
private static String Key {
get { return typeof(SessionProvider<Child>).FullName; }
} // Key
private static Child Value {
get { return (Child)HttpContext.Current.Session[Key]; }
set { HttpContext.Current.Session[Key] = value; }
}
public static Child Current {
get {
var instance = Value;
if (instance == null)
lock (typeof(Child)) {
instance = Value;
if (instance == null)
Value = instance = new Child();
}
return instance;
}
} // Current
} // SessionProvider
Then I have for each project the following:
public interface ISessionService {
DateTime Appointment { get; set; }
} // ISessionService
public class SessionService : SessionProvider<SessionService>,
ISessionService {
public DateTime Appointment { get; set; }
} // SessionService
What do you think?
Then I inject the Session Service having the following on my Structure
Map configuration:
For<ISessionService>().HttpContextScoped().Use(() => new
SessionService());
Maybe I should create an interface for SessionProvider instead of
SessionService?
How should I do this?
Now to get a session value or save it I use the following on my
controller.
sessionService.Current.Appointment = DateTime.UtcNow;
DateTime when = sessionService.Current.Appointment;
The naming SessionService and SessionProvider are just what I came up
with ...
Any suggestion about the naming?
Thank You,
Miguel
I am trying to create a common "SessionProvider" that a SessionService
use.
I came up with the following Session provider:
// SessionProvider
public class SessionProvider<Child> where Child :
SessionProvider<Child>, new() {
private static String Key {
get { return typeof(SessionProvider<Child>).FullName; }
} // Key
private static Child Value {
get { return (Child)HttpContext.Current.Session[Key]; }
set { HttpContext.Current.Session[Key] = value; }
}
public static Child Current {
get {
var instance = Value;
if (instance == null)
lock (typeof(Child)) {
instance = Value;
if (instance == null)
Value = instance = new Child();
}
return instance;
}
} // Current
} // SessionProvider
Then I have for each project the following:
public interface ISessionService {
DateTime Appointment { get; set; }
} // ISessionService
public class SessionService : SessionProvider<SessionService>,
ISessionService {
public DateTime Appointment { get; set; }
} // SessionService
What do you think?
Then I inject the Session Service having the following on my Structure
Map configuration:
For<ISessionService>().HttpContextScoped().Use(() => new
SessionService());
Maybe I should create an interface for SessionProvider instead of
SessionService?
How should I do this?
Now to get a session value or save it I use the following on my
controller.
sessionService.Current.Appointment = DateTime.UtcNow;
DateTime when = sessionService.Current.Appointment;
The naming SessionService and SessionProvider are just what I came up
with ...
Any suggestion about the naming?
Thank You,
Miguel