在 MVC 域模型库中使用会话变量 [英] Using Session Vars in a MVC Domain Model library

查看:56
本文介绍了在 MVC 域模型库中使用会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 IQueryable 函数.在该函数中,我需要将数据存储和检索到 Session;你们能指出我正确的方向吗?

I have a IQueryable function. In that function, I need to store and retrieve data to Session; can you guys point me in the right direction.

我查看了 HttpSessionStatBase,其中的会话通常是从 HttpContext.Current 获取的,但这在库中似乎是不可能的.我错过了什么吗?

I've looked at the HttpSessionStatBase where the session is usually taken from HttpContext.Current but this doesnt seem possible to do in the library. Am I missing something?

提前致谢.

推荐答案

我会避免依赖于静态 HttpContext.我的首选策略是从控制器中的会话中提取信息,并将其作为参数(或设置为属性)传递到数据访问层/存储库上.如果您觉得必须直接使用 Session,那么我会以相同的方式将其提供给 DAL/存储库——作为属性或参数.但是请注意,您正在增加 DAL/存储库和控制器之间的耦合.这将使在非 Web 设置中重用变得更加困难,即,如果您需要从 Windows 服务使用它,则必须创建一个假会话来与 DAL/存储库交互,或者例如,控制台应用程序.

I would avoid having a dependency on the static HttpContext. My preferred strategy would be to extract the information from the session in the controller and pass it as parameters (or set as properties) on your data access layer/repository. If you feel that you must use the Session directly, then I would provide it to the DAL/repository in the same manner -- as a property or as a parameter. Note, however, that you are increasing the coupling between your DAL/repository and the controller. This will make it much more difficult to re-use in a non-web setting, i.e., you'd have to create a fake session just to interact with the DAL/repository if you ever needed to work with it from a windows service or console app, for example.

这篇关于在 MVC 域模型库中使用会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆