执行强类型 ASP.NET MVC 会话的更好方法
我正在开发一个 ASP.NET MVC 项目,想要使用强类型会话对象。我实现了以下Controller派生类来公开这个对象:
public class StrongController<_T> : Controller
where _T : new()
{
public _T SessionObject
{
get
{
if (Session[typeof(_T).FullName] == null)
{
_T newsession = new _T();
Session[typeof(_T).FullName] = newsession;
return newsession;
}
else
return (_T)Session[typeof(_T).FullName];
}
}
}
这允许我为每个控制器定义一个会话对象,这符合控制器隔离的概念。有没有更好/更“正确”的方法,也许是微软官方支持的方法?
I am developing an ASP.NET MVC project and want to use strongly-typed session objects. I have implemented the following Controller-derived class to expose this object:
public class StrongController<_T> : Controller
where _T : new()
{
public _T SessionObject
{
get
{
if (Session[typeof(_T).FullName] == null)
{
_T newsession = new _T();
Session[typeof(_T).FullName] = newsession;
return newsession;
}
else
return (_T)Session[typeof(_T).FullName];
}
}
}
This allows me to define a session object for each controller, which is in line with the concept of controller isolation. Is there a better/more "correct" way, perhaps something that is officially supported by Microsoft?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这样其他对象将无法访问该对象(例如 ActionFilter)。我这样做:
然后,我可以将 IUserDataStorage 注入控制器的构造函数中,或者在 ActionFilter 内使用 ServiceLocator.Current.GetInstance(typeof(IUserDataStorage)) 。
当然,对于所有控制器都需要这个的情况(例如ICurrentUser),您可能需要使用属性注入。
This way other objects won't have access to this object (e.g. ActionFilter). I do it like this:
Then, I can either inject IUserDataStorage into controller's constructor, or use ServiceLocator.Current.GetInstance(typeof(IUserDataStorage<T>)) inside ActionFilter.
Of course for cases when all controllers need this (e.g. ICurrentUser) you may want to use property injection instead.
这可能更适合你想要的。我只想创建一个可以访问您的会话的扩展方法。扩展方法的额外好处是您不再需要从控制器继承,或者必须注入实际上不需要的依赖项。
This might be better for what you want. I would just create an extension method that can access your session. The added benefit to the extension method is that you no longer have to inherit from a controller, or have to inject a dependency that really isn't necessary to begin with.
http://codingsmith.co。 za/a-better-way-of-working-with-httpcontext-session-in-mvc/ (抱歉我博客上的颜色是围绕主题进行设计的,只是还没有修复它)
我想出了那是几年前的事了,而且效果很好。我猜和其他人一样的基本想法,为什么微软不只是将其作为标准来实现,这让我困惑。
http://codingsmith.co.za/a-better-way-of-working-with-httpcontext-session-in-mvc/ (apologies for the colours on my blog was tooling around with themes and just havent fixed it yet)
i came up with that a few years ago and it works fine. same basic idea as everyone else i guess, why microsoft dont just implement this as standard eludes me.
我通常将其用作会话密钥,然后根据需要显式添加对象。这样做的原因是这是一种干净的方法,而且我发现您希望将会话中的对象数量保持在最低限度。
这种特殊的方法将表单身份验证和用户会话整合到一个位置,以便您可以添加对象并忘记它。可以说它很冗长,但它确实防止了任何重复,并且会话中不应该有太多对象。
以下内容可以存在于核心库中或您想要的任何位置。
然后在项目根目录下的以下命名空间中实现它(我通常将其放在 mvc 项目的代码文件夹中)
:
在会话中显式声明您想要的内容的一大优点是您可以在 mvc 应用程序中的任何位置(包括视图)绝对引用它。只需引用它:
同样比其他方法稍微不那么通用,但确实非常清楚发生了什么,没有其他操纵或依赖项注入,并且对请求上下文 100% 安全。
另一方面,字典方法很酷,但最终你仍然会得到到处都是字符串来引用东西。您可以使用枚举或其他东西来配置它,但我更喜欢上述方法的强类型和设置和忘记。
I generally use this for a session key and then explicitly add objects as needed. The reason for this is it's a clean way to do it and I find that you want to keep the number of objects in session to a minimum.
This particular approach brings together forms authentication and user session into one place so you can add objects and forget about it. The argument could be made that it is a big verbose, but it does prevent any double up and you shouldn't have too many objects in session.
The following can exist in a core library or wherever you want.
Then implement this in the following class namespaced to the root of your project (I usually put it in a code folder on mvc projects):
FINALLY
The big advantage of explicitly declaring what you want in session is that you can reference this absolutely anywhere in your mvc application including the views. Just reference it with:
Again a little less generic than other approaches, but really really clear what's going on, no other rigging or dependancy injection and 100% safe to the request context.
On another note, the dicionary approaches are cool, but you still end up with strings all over the place to reference stuff. You could rig it with enums or something, but I prefer the strong typing and set and forget of the above approach.
是的,在这个问题被提出多年后,还有其他方法可以做到这一点......但万一其他人出现寻找将上述方法结合成一个有吸引力的一站式服务的东西(至少是一个吸引我的团队的方法)我...)这就是我们使用的。
然后在控制器中,您可以以更强类型的方式使用会话变量来完成您的任务。
希望这对某人有帮助!
Yes, it's years after this question was asked and there are other ways to do this... but in case anyone else shows up looking for something that combines the approaches above into an appealing one stop shop (at least one that appealed to my team and I...) Here's what we use.
Then in your controllers you can do your thing with your session variables in a more strongly typed way.
Hope this helps someone!