我是否需要取消订阅(手动订阅)asp.net 中的事件?

发布于 2024-07-10 15:04:19 字数 341 浏览 10 评论 0原文

有关订阅/取消订阅事件的相同最佳实践规则是否适用于 ASP.NET?

我知道这似乎是一个愚蠢的问题,但当我仔细想想,我从未真正见过任何代码,人们首先订阅页面上的事件,然后在网络请求中取消订阅。

示例1: 在页面上的 Page_Load 方法中,我订阅了 ListView 上的更新事件。 我是否应该稍后取消订阅该事件,例如在 OnPreRenderComplete 方法中?

示例2: 在被动视图模式中,视图(页面控件/用户控件)将在需要演示者执行任何操作时引发事件。 那么presenter需要订阅视图上的事件,但是否还需要再次取消订阅事件呢?

最好的问候,埃吉尔。

Do the same best practis rules regarding subscribing/unsubscribing to events apply in asp.net?

I know it might seem like a silly question, but when I think about it, I have never really seen any code where people first subscribe to an event on a page and then unsubscribe later on in the web request.

Example 1:
On a page, in the Page_Load method, I subscribe to a updating event on a ListView. Should I unsubscribe from that event later on, for example in the OnPreRenderComplete method?

Example 2:
In the passive view patter, a view (Page control/Usercontrol) will raise an event whenever it needs the presenter to do anything. So the presenter needs to subscribe to events on the view, but do it also need to unsubscribe from the events again?

Best regards, Egil.

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

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

发布评论

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

评论(2

蛮可爱 2024-07-17 15:04:19

当请求完成时,页面实例及其所有组件将“超出范围”,例如,它们有资格进行GC。 因此,您的 ListView 及其上的页面/用户控件将超出范围。 您不需要取消订阅(例如,除非您订阅属于某种单例的事件,该单例在每次请求后都存活并使用页面的方法之一作为事件处理程序)。

同样的事情对于演示者也是有效的(同样,只要该演示者仅在一页中使用并且之后超出范围)。

The page instance and all of its components will "go out of scope" when request completes, e.g. they become eligible for GC. So your ListView will go out of scope along with the Page/user controls on it. You don't need to unsubscribe (unless you subscribe to an event that belongs to some kind of singleton that survives every request and use one of the methods of the page as the event handler, for example).

The same thing is valid for the presenter (again as long as this presenter is used solely with one page and goes out of scope after that).

快乐很简单 2024-07-17 15:04:19

一般来说,不会。 页面卸载时应该自动转储事件。 应该是。 我之前遇到过一个错误(在 .NET 1.1 中),但情况并非如此。

我不会费心取消订阅,除非我注意到页面有问题(例如,从调用堆栈中的幻影调用一个方法 20 次:这通常是某些内容未正确取消订阅的标志)。

Generally, no. Events are supposed to be dumped automatically when the page unloads. SUPPOSED to be. I've run into a bug before (in .NET 1.1) where that wasn't the case.

I won't bother unsubscribing, unless I notice a problem with the page (like, a method being called 20 times from a phantom in the call stack: that's usually a sign of something not being unsubscribed properly).

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