多个客户端的选择性更改通知 (C#)

发布于 2024-10-01 12:06:31 字数 404 浏览 4 评论 0原文

我需要为具有一个管理器和多个消费者/客户端的系统实现一种通知机制。管理者应该轮询数据库并在数据发生变化时触发事件。现在,如果所有客户端都对相同的数据感兴趣,那就很容易了,并且实现单个事件并将所有客户端订阅该事件就足够了。但是,客户端应该只接收其负责的数据的事件。

例如,有多个客户端添加新客户。这是通过管理器以线程安全的方式发生的。现在,创建客户的这些客户需要了解仅发生在这些客户身上的任何更改。经理每 N 秒轮询一次客户表,并获取所有发生更改的客户的列表。然后,经理需要将通知“路由”(没有更好的词)给感兴趣的客户。

这是否必须通过每个客户端必须向管理器提供的某种回调来实现?这听起来像是我需要的东西,但我不知道如何将参数传递给这个回调(这里,这些是我感兴趣的客户,当您有任何其他客户的更新时不要打扰我)

我使用 C#,.NET 2.0。谢谢!

I need to implement a notification mechanism for a system that has one manager and multiple consumers/clients. A manager should poll a database and fire an event whenever there are changes in the data. Now, it'd be easy if all clients would be interested in the same data, and it would be sufficient to implement a single event and subscribe all clients to that event. However, clients should only receive the events for the data they are responsible for.

For example, there are multiple clients that add new customers. This happens through the manager in a thread-safe way. Now, these clients that created the customers need to know of any changes that happen only to those customers. The manager polls the Customers table every N seconds and gets a list of all customers that changed. Then, the manager will need to "route" (for a lack of a better word) the notifications to the interested clients.

Will this have to be implemented with some sort of a callback that each client will have to supply to the manager? This sounds like something I need, but I dont know how I can pass parameters to this callback (here, these are the customers Im interested in, dont bother me when you have updates for any other customer)

Im using C#, .NET 2.0. Thanks!

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

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

发布评论

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

评论(1

辞旧 2024-10-08 12:06:31

这是对观察者模式的很好的描述。通常,客户向管理器注册对其相关的一组数据的兴趣,并提供一种通知方式(这将是您的回调)。如果客户端不再对以前有用的数据感兴趣,也可以取消注册。然后,经理的工作是将更改传播给所有感兴趣的观察者(即客户端)。

在 C# 中,所需的基础设施可作为一流的语言功能 - 事件和委托。 这里有很好的(如果简单的话)示例代码

在 .Net 4 中,通过 ObservableCollection进一步简化了这种便利性。可用于自动化通知过程。

顺便说一句 - 如果可能的话,我会避免轮询数据库。有没有办法让您收到有关数据库中必要更改的通知?在 C#/SQL Server 中,您可以使用 SqlDependency

This is a good description of the Observer pattern. Typically a client registers interest with the manager for a set of data that is relevant for it, providing a means of notification (this would be your callback). A client may also unregister if it's no longer interested in previously-useful data. Then the manager's job is to propagate changes to all interested Observers (i.e. clients).

In C#, the required infrastructure is available as first-class language features - events and delegates. There is good (if simple) example code here.

In .Net 4 this convenience is taken a step further with ObservableCollection<T> available to automate the notification process.

By the way - I would avoid polling the database if possible. Is there no way you can get notified on the necessary changes in your DB? In C#/SQL Server you can use SqlDependency.

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