从业务对象访问控制器上下文/ TempData
我正在尝试构建一个可以交换的会话/临时数据提供程序。默认提供程序将在 asp.net mvc 之上工作,并且需要从业务对象类访问 .net mvc TempData。我知道临时数据可以通过控制器上下文获得,但我似乎无法找到它是否通过 HttpContext 或其他东西公开。我真的不想将控制器上下文作为参数传递,因为这会稀释我的接口定义,因为只有基于 ASP.NET 的会话提供程序需要这个,其他(使用 NoSQL DB 等)不关心控制器上下文。
为了进一步澄清,在此处添加更多代码。我的 ISession 界面如下所示。当此代码投入生产时,会话/临时数据预计将使用 NoSql db 工作。但我也希望有另一个在 asp.net mvc session/tempdata 之上工作的实现,用于我的开发测试等。
公共接口 ISession
{
T GetTempData<T>(string key);
void PutTempData<T>(string key, T value);
T GetSessiondata<T>(string key);
void PutSessiondata<T>(string key, T value);
}
I am trying to build a session/tempdata provider that can be swapped. The default provider will work on top of asp.net mvc and it needed to access the .net mvc TempData from the business object class. I know the tempdata is available through the controller context, but i cant seem to find if that is exposed through HttpContext or something. I dont really want to pass the Controller context as an argument as that would dilute my interface definition since only asp.net based session provider needs this, other (using NoSQL DB etc) doesn't care about Controller Context.
To clarify further, adding little more code here. my ISession interface look like this. and when this code goes to production, the session/tempdata is expected to work using NoSql db. But i also like to have another implementation that works on top of asp.net mvc session/tempdata for my dev testing etc.
public interface ISession
{
T GetTempData<T>(string key);
void PutTempData<T>(string key, T value);
T GetSessiondata<T>(string key);
void PutSessiondata<T>(string key, T value);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道你到底想做什么,但 TempDataDictionary 实现了
IDictionary
,因此您可以让业务对象将其作为参数或使用构造函数注入。然后,您可以让控制器将 TempData 作为参数传递给业务对象。通过使用字典接口,您的业务对象不再依赖于 ASP.NET MVC。I don't know exactly what you are trying to do but TempDataDictionary implements
IDictionary<string, object>
so you could have your business objects take it as parameter or use constructor injection. Then you could have your controller pass the TempData as parameter to the business object. By using the dictionary interface your business objects no longer depend on ASP.NET MVC.