简体   繁体   English

使用WVM服务作为使用MVVM设计模式的WPF应用程序中的模型

[英]Using a WCF service as the model in a WPF application using the MVVM design pattern

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. 使用MVVM编写WPF应用程序时,我想使用WCF服务,并在其上使用方法从应用程序数据库中提供相关数据。

As an example, if my system has a list of Tasks, they are stored in a SQL database. 例如,如果我的系统有一个任务列表,它们将存储在SQL数据库中。 I can put a method on the Web Service to retrieve all tasks from the database. 我可以在Web服务上放置一个方法来从数据库中检索所有任务。

I can call this method from a ViewModel and store the results in a 我可以从ViewModel调用此方法并将结果存储在a中

public List<Task> Tasks { get; set; }

Then I would bind a control on my View to this property. 然后我将我的View上的控件绑定到此属性。

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). 当Tasks属性发生变化时,我已经有了适合我的View更新的东西(我没有把它放在上面以保持示例简单)。

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? 我的问题是,当其他人将新任务添加到数据库时,如何更新ViewModel上的Tasks属性?

Do I need to poll the database every x minutes to look for new tasks (Via a method on the WCF service)? 我是否需要每隔x分钟轮询数据库以查找新任务(通过WCF服务上的方法)?

Or can I somehow do something that will update the Tasks property when tasks are added to the database? 或者我可以以某种方式执行某些操作,以便在将任务添加到数据库时更新Tasks属性?

You could implement this using the Publish Subscribe Framework for WCF Services . 您可以使用发布订阅框架为WCF服务实现此功能。 This allows your WPF application to subscribe to updates from the WCF Server. 这允许您的WPF应用程序从WCF服务器订阅更新。 The server would then call WCF Services provided by your WPF application when new data is available. 然后,当新数据可用时,服务器将调用WPF应用程序提供的WCF服务。 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. 如果您的WCF服务还控制数据库的存储,那么您可以简单地捕获更改,并向您订阅的WPF客户端发送通知。

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. 对ViewModel中的数据所做的更改会直接更新(通过绑定),因为所有更改都发生在WPF应用程序的内存中。 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. 您可能希望向WCF服务添加一个方法,该方法接受DateTime ,该方法将返回DateTime定时间以来添加的所有任务。 Then, call that method from your ViewModel at a frequency that makes sense for your usage scenarios and expected data update rates. 然后,以对您的使用方案和预期数据更新率有意义的频率从ViewModel调用该方法。 At that point, any news tasks added should be reflected in your View. 此时,添加的任何新闻任务都应反映在您的视图中。

You could use a CQRS approach depending on the app. 您可以根据应用程序使用CQRS方法 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. 如果应用程序是简单的CRUD,那么它可能不值得,但如果您正在构建更复杂的域模型或寻找显着的可伸缩性,这可能值得研究。 CQRS basically separates your commands from your queries. CQRS基本上将您的命令与查询分开。 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. 在您的方案中,您可能将实际的ViewModel存储在数据库中,客户端可以直接从那里获取它们,而无需通过WCF。 You could also have your clients subscribing to domain events to enable them to dynamically refresh as required. 您还可以让客户订阅域事件,以使其能够根据需要动态刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM