Reputation: 42185
I'm doing an ASP.NET MVC application and some of my Action Methods and other Extension Methods need access to User data. The code I'm using to get the user is:
this.currentUser = (CurrentUser)HttpContext.Session["CurrentUser"];
//and in the extension methods it's:
CurrentUser user = (CurrentUser)HttpContext.Current.Session["CurrentUser"];
This same line is scattered among a lot of my Action Methods in a lot of my Controllers. The problem is this makes it difficult to test, and it doesn't appear to be very 'elegant'.
can anyone suggest a good SOLID approach to this solution?
Thanks
Dave
Upvotes: 2
Views: 3774
Reputation: 31842
You shouldn't store user in Session. Session can be easily lost when application is restarted by modification in web.config or by reaching memory limit. This will log out user in random moments.
There is no reason not to use session for different purposes (for example to store items in basket). You can do it like that:
First we define interface:
public interface ISessionWrapper
{
int SomeInteger { get; set; }
}
Then we make HttpContext implementation:
public class HttpContextSessionWrapper : ISessionWrapper
{
private T GetFromSession<T>(string key)
{
return (T) HttpContext.Current.Session[key];
}
private void SetInSession(string key, object value)
{
HttpContext.Current.Session[key] = value;
}
public int SomeInteger
{
get { return GetFromSession<int>("SomeInteger"); }
set { SetInSession("SomeInteger", value); }
}
}
Then we define our base controller:
public class BaseController : Controller
{
public ISessionWrapper SessionWrapper { get; set; }
public BaseController()
{
SessionWrapper = new HttpContextSessionWrapper();
}
}
Finally:
public ActionResult SomeAction(int myNum)
{
SessionWrapper.SomeInteger
}
This will make testing easy, because you can replace ISessionWrapper with mock in controller tests.
Upvotes: 6
Reputation: 6316
Yes. Don't use sessions (there are several reasons why not).
Asp.Net has a very good mechanism called Forms Authentication for authenticationg and accessing user data.
I have answered a similar question which might help.
Upvotes: 2