ASP.NET 观察者模式

发布于 2024-07-10 10:33:49 字数 151 浏览 12 评论 0原文

我正在开发一个项目,其中中心类(主题)将包含大量数据。 将有一个 aspx 页面使用 .net ajax 显示此数据。 当主题从任何页面更新时,我希望所有打开的页面都更新。 我将为此使用观察者模式。 问题是,我是否将实际的 aspx 页面设为观察者,还是有更好的方法来做到这一点?

I'm working on a project where a central class (the subject), will contain alot of data. There will be an aspx page that displays this data, using .net ajax. When the subject is updated from any page, I want all the open pages to update. I will be using the observer pattern for this. THe question is, do I make the actual aspx page the observer, or is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

习惯成性 2024-07-17 10:33:49

在这种情况下,观察者模式没有意义。

Web(和 ASP.NET)通过请求(例如,来自 Web 浏览器)和响应(例如,来自 Web 服务器)的方式工作。

观察者模式要求主体(例如网络服务器)维护对其观察者(例如网络浏览器)的引用并向他们推送通知更新。 显然,这不适用于 Web 浏览器 - Web 服务器场景。

我同意 Sergio 的观点,最好的方法可能是让您的客户端(网络浏览器)定期检查资源(网络服务器)的更新。 最巧妙的方法是 AJAX 调用。

Gmail 就是一个很好的例子,服务器收到新消息后很快就会出现在收件箱中,而无需用户发出明确的请求。 这是通过 AJAX 调用来完成的,该调用会定期检查 Gmail 服务器是否有新邮件。

更新(2012.03.01):-

最近的发展使得将数据从 Web 服务器推送到客户端变得更加现实,例如 信号R。

An observer pattern doesn't make sense in this case.

The web (and ASP.NET) works by way of requests (e.g. from a web browser) and responses (e.g. from a web server).

An observer pattern requires that the subject (e.g. a web server) maintains references to it's observers (e.g. web browsers) and pushes notification updates to them. Clearly this is not applicable to a web browser-web server scenario.

I agree with Sergio that the best method is probably to make your clients (web browsers) periodically check for updates from the resource (web server). The neatest method of doing this would be an AJAX call.

A good example is Gmail, new messages appear in the inbox soon after they are received on the server without the user having to make an explicit request. This is done by AJAX calls which periodically check the Gmail server for new messages.

UPDATE (2012.03.01):-

Recent developments have allowed the pushing of data from web server to client to become a more realistic possibility, e.g. SignalR.

ゃ懵逼小萝莉 2024-07-17 10:33:49

塞尔吉奥是对的,在某种程度上你也是对的。 通过 Asp.Net AJAX 实现此目的的最简单方法是使用 AJAX 计时器控件,但您当然不希望在所有页面上重复该控件。 更好的方法是将该功能包装到母版页中,甚至创建一个派生自 Page 的基页。

查看有关计时器控件的 MSDN 页面,还有一个不错的视频教程

作为设计的旁注,我不一定会将实际的观察者代码放在代码后面。 相反,您希望将这些事件转发到某个底层模型。 这个想法是你的代码隐藏只负责 UI 相关的东西和实现这一点的逻辑。 本质上是 MVP/MVP 中的 V

另外,考虑到您正在谈论没有实际状态的单个 HTTP 响应/请求周期,我不确定“实际”观察者模式在 Web 上下文中的应用效果如何。

Sergio is right, and so are you to an extent. The easiest way to implement this through Asp.Net AJAX is using an AJAX timer control, but you certainly don't want to duplicate that across all your pages. A better approach would be to wrap that functionality up into a Master page, or even create a base page that derives from Page.

Check out the MSDN page on the Timer Control, and a decent video tutorial as well.

As a side note on design, I wouldn't necessarily put the actual observer code inside your code behind however. Rather, you want to forward those events on to some underlying model. The idea is that your Code Behind is responsible only for UI related stuff and the logic to make that happen. Essentially the V in MVP/MVP.

Also, I am not sure how well the "actual" observer pattern applies in a web context considering you are talking about a single HTTP Reponse/Request cycle with no real state.

べ繥欢鉨o。 2024-07-17 10:33:49

我不熟悉观察者模式,但执行此操作的唯一方法是在页面加载上放置一个 JavaScript 无限循环,每隔 X 秒调用服务器上的 AJAX 方法来检查更新。

I'm not familiar with the observer pattern, but the only way you have to do this is to place a javascript endless loop on page load that every X seconds invokes an AJAX method on the server to check for updates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文