我可以使用ASP.NET会话[]变量外部DLL [英] Can I use ASP.NET Session[] variable in an external DLL

查看:118
本文介绍了我可以使用ASP.NET会话[]变量外部DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个项目,DLL项目里面有我所有的逻辑和数据访问的东西,并做我的形式等。

I have two projects, the DLL project which has all my logic and data access stuff, and the ASP.NET project which does my forms etc.

我有点困惑。我想如果我说System.Web命名空间参考DLL项目,我将能够引用ASP.NET页面的会话状态信息。

I am a bit confused. I thought if I added the System.Web namespace reference to the DLL project I would be able to reference the Session state information of the ASP.NET page.

我可以使用每个页面出获取会话信息,并把它传递给DLL的处理,但很想能够直接从DLL类(S)处理的事情。

I could use each page to get the session info out and pass it to the DLL for the processing but would love to be able to process things directly from the DLL class(s).

这可能吗?

我已经摆弄System.Web命名,只是似乎能够得到的会话变量的引用。

I have fiddled with the System.Web namespace and just seem able to get a reference to the Session variable.

谢谢大家。

乔恩

推荐答案

您应该能够使用HttpContext.Current.Session

You should be able to use HttpContext.Current.Session

虽然是我同意你不应该紧密结合你的业务逻辑DAL或等组件,以ASP.Net会话。有很多有效的情况下访问一个Web项目以外的HTTP上下文。

While yes I agree you should not tightly couple your Business Logic DAL or etc assemblies to ASP.Net session. There are plenty of valid cases for accessing HTTP Context outside of a web project.

Web控件是probally的一个最好的例子,可重复使用的HTTP模块等等...

Web Controls is probally one of the best examples, reusable http modules etc etc...

现在,如果你希望你的DLL从拉届东西一个选择是抽象出来的​​会话。所以,你可以像定义一个的IStorage接口,那你的图书馆将知道如何使用。然后你就可以有一个或sessionStorage的类MemoryStorage并使用IoC容器注入合适的类到你的库类。这给了你自由code是你怎么想它是codeD而不会占用你的code到会话。哦,和另外一个好处,如果处理得当可以用来不会配合你的code到会议网站无论是。

Now one option if you want to have your DLL pull the stuff from Session is to abstract out session. So you could define an interface like IStorage, that your library will know how to use. Then you can have a SessionStorage or MemoryStorage class and use IoC to inject the appropiate class into your library classes. This gives you the freedom to code it how you wanted it to be coded without tying your code to Session. Oh and one other benefit if done properly can be used to not tie your code to session in the web either.

这篇关于我可以使用ASP.NET会话[]变量外部DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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