通过服务保存时如何保持模型一致?
我正在使用 MVVM 模式开发客户端/服务器 (WPF/WCF) 应用程序。
ViewModel 上的属性绑定到 Model 上的属性,这样当 Model 更改时,更改会立即传播到 View。
当用户单击刷新时,服务器上的任何新的/更改的数据都会合并到模型中,因此刷新机制运行良好。
如何处理用户编辑和保存属性?这是我想要发生的事情:
- 用户点击“编辑”
- 用户修改文本框中的值
- 用户点击“保存”
- 更改将发送到服务器
- 如果成功,服务器返回更新的项目,该项目将合并到 模型,并且 UI 已更新。
- 如果不成功,服务器会返回错误并且模型不会更新。
看起来,通过使用MVVM,模型在第3步更新(使第5和6步变得多余),如果服务器上发生错误,客户端的数据就会与服务器不一致。
是否有最佳实践方法来处理这种情况?
I am working on a client/server (WPF/WCF) application using the MVVM pattern.
Properties on the ViewModel are bound to properties on the Model such that when the Model is changed, the change is propagated immediately to the View.
When the user clicks refresh, any new/changed data on the server is merged into Model, so the refresh mechanism is working well.
How do I handle the user editing and saving properties? Here is what I want to happen:
- User clicks "Edit"
- User modifies values in TextBoxes
- User clicks "Save"
- Changes are sent to server
- If successful, server returns updated item, which is merged into
Model, and the UI is updated.- If not successful, server returns error and Model is not updated.
It seems that by using MVVM, the Model is updated at step 3 (making steps 5 & 6 redundant), and if an error occurs on the server, data at the client becomes inconsistent with the server.
Is there a best practice way to deal with this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们处理此问题的方式存储了原始数据的副本。
每当更新失败时,我们都会将原始数据恢复到视图中。
The way we have handled this stored a copy of original data.
Whenever an update fails, we restored the original data to the view.
我提出的解决方案是创建第二组对象 - ModelUpdates。
当用户单击“编辑”时,将实例化一个 ModelUpdate 对象(与 Model 对象具有相同的属性),并将 DataContext 切换到该对象。
当用户单击“保存”时,ModelUpdate 对象的详细信息将发送到服务。该服务会响应有关更新对象的详细信息,然后将其合并到模型存储库中。
这意味着模型仅使用已成功保存的数据进行更新。如果对象保存不正确,数据库版本将被发送回客户端。如果保存时发生错误,数据库版本将被发送回客户端。如果服务器不可用,客户端将保留预编辑版本。
The solution I have come up with is to create a second set of objects - ModelUpdates.
When the user clicks Edit, a ModelUpdate object (which has the same properties as the Model object) is instantiated and the DataContext is switched to that object.
When the user clicks Save, details of the ModelUpdate object are sent to the service. The service responds with details about the updated object, which are then merged into the Model repository.
This means that the Model is only ever updated with data that has been successfully saved. If the object was saved incorrectly, the database version will be sent back to the client. If an error occurred in saving, the database version will be sent back to the client. If the server is unavailable, the client will keep the pre-edit version.
我认为应该使用同步post方法。
这样,如果发生错误,模型和视图侧是相同的。此外,在恢复保存的数据之前,您的视图不会更新。
简而言之:
1)用户点击编辑
2) 用户进行更改
3)用户点击保存
3.1)应用程序将数据提交给模型
3.2) 模型保存更改
3.3)模型返回改变的数据
4) 查看更改并显示成功(更改的数据)或失败消息。
I think a synchronous post method should be used.
This way, if an error occurs, the model and the view side are the same. Also, your view won't be updated until it get's the saved data back.
In simple words:
1) User clicks edit
2) User makes changes
3) User clicks save
3.1) Application sumbits the data to the model
3.2) Model saves changes
3.3) Model returns changed data
4) View changes with a succes (changed data) or failed message.