使用 MVVM 设计模式将 WCF 服务用作 WPF 应用程序中的模型
当使用 MVVM 编写 WPF 应用程序时,我想使用 WCF 服务,并使用其方法来提供应用程序数据库中的相关数据。
例如,如果我的系统有一个任务列表,它们将存储在 SQL 数据库中。我可以在 Web 服务上放置一个方法来从数据库中检索所有任务。
我可以从 ViewModel 调用此方法并将结果存储在 a 中,
public List<Task> Tasks { get; set; }
然后我会将 View 上的控件绑定到此属性。
当任务属性更改时,我已经准备好一些内容供我的视图更新(为了使示例简单,我没有将其放在上面)。
我的问题是,当其他人将新任务添加到数据库时,如何更新 ViewModel 上的 Tasks 属性?
我是否需要每 x 分钟轮询一次数据库以查找新任务(通过 WCF 服务上的方法)?
或者我可以以某种方式做一些事情,在将任务添加到数据库时更新任务属性吗?
When writing a WPF application using MVVM, I want to use a WCF service, with methods on it to provide the relevant data from the applications database.
As an example, if my system has a list of Tasks, they are stored in a SQL database. I can put a method on the Web Service to retrieve all tasks from the database.
I can call this method from a ViewModel and store the results in a
public List<Task> Tasks { get; set; }
Then I would bind a control on my View to this property.
I already have something in place for my View to update, when the Tasks property changed (I didn't put it above to keep the example simple).
My question is, when a new task is added to the database by somebody else, how do I update the Tasks property on the ViewModel?
Do I need to poll the database every x minutes to look for new tasks (Via a method on the WCF service)?
Or can I somehow do something that will update the Tasks property when tasks are added to the database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用WCF 服务的发布订阅框架来实现此操作。这允许您的 WPF 应用程序从 WCF 服务器订阅更新。当新数据可用时,服务器将调用 WPF 应用程序提供的 WCF 服务。然后,您可以使用新数据更新视图模型。如果您的 WCF 服务还控制对数据库的存储,那么您可以简单地捕获所做的更改,并向您订阅的 WPF 客户端发送通知。
You could implement this using the Publish Subscribe Framework for WCF Services. This allows your WPF application to subscribe to updates from the WCF Server. The server would then call WCF Services provided by your WPF application when new data is available. You could then update your view models with the new data. If your WCF Service also controls stores to the database, then you can simply catch the changes as they are made, and send out notifications to your subscribed WPF clients.
您需要执行某种轮询才能完成这项工作。对 ViewModel 中的数据所做的更改会直接更新(通过绑定),因为所有更改都发生在 WPF 应用程序的内存中。然而,对数据库的更改只有数据库知道。
您可能想要向 WCF 服务添加一个采用
DateTime
的方法,该方法将返回自给定时间以来添加的所有任务。然后,以对您的使用场景和预期数据更新率有意义的频率从 ViewModel 调用该方法。此时,添加的任何新闻任务都应该反映在您的视图中。You would need to perform some kind of polling to make this work. Changes made to data in your ViewModel are updated directly (via your bindings) because all the changes occur in the WPF app's memory. Changes to the database, though, will only be known to the database.
You might want to add a method to your WCF service that takes a
DateTime
which will return all tasks added since a given time. Then, call that method from your ViewModel at a frequency that makes sense for your usage scenarios and expected data update rates. At that point, any news tasks added should be reflected in your View.您可以根据应用使用 CQRS 方法。如果应用程序是简单的 CRUD,那么它可能不值得,但如果您正在构建更复杂的域模型或寻找显着的可扩展性,这可能值得研究。 CQRS 基本上将命令与查询分开。在您的场景中,您可能将实际的 ViewModel 存储在数据库中,客户端直接从那里获取它们,而不需要通过 WCF。您还可以让您的客户端订阅域事件,以使它们能够根据需要动态刷新。
You could use a CQRS approach depending on the app. If the app is simple CRUD then it's probably not worth it but if you are building a more complex domain model or looking for significant scalability this might be worth investigating. CQRS basically separates your commands from your queries. In your scenario you might have the actual ViewModels stored in the DB with clients getting them directly from there, without needing to go through WCF. You could also have your clients subscribing to domain events to enable them to dynamically refresh as required.