Reputation: 7216
is this bad practice?
using System.Web.Mvc;
namespace WebInventaris.Utils.Session
{
public static class SessionControllerExtensions
{
public static void SetSessionVar<T>(this Controller controller, string field, T value)
{
controller.Session[field] = value;
}
public static T GetSessionVar<T>(this Controller controller, string field)
{
return (T) controller.Session[field];
}
}
}
I'm trying to separate the Session from my controllers, but I'm not getting a good feeling about these methods.
Upvotes: 2
Views: 1068
Reputation: 1038940
There's nothing wrong with using the session directly from your controllers (that's one of the reasons why it has been exposed as a property of the Controller class). And I don't kind of see the benefit of replacing:
Session["foo"] = "bar";
with a call to an extension method:
this.SetSessionVar("foo", "bar");
Where it is probably a bit more interesting is your generic getter but still it is not type safe as you are casting inside this method which could have also been done in the controller action. So to conclude I wouldn't say that it is a bad practice but I don't see any benefit of such a class and it also makes your code less readable by other developers who need to understand that this is an extension method and that behind the scenes it simply uses the session.
Upvotes: 5