架构帮助:自定义 json Web 服务的 wpf/mvvm 数据输入前端
我发现自己遇到了一些架构问题:我正在开发一个小型项目,其中包括数据输入和持久性,以及使用带有自定义 JSON 协议的 Web 服务的 DAL。到目前为止,一切都很好,将一些快速而肮脏的 DataTable
+ DataGrid
代码拼凑在一起并完成是一件相对简单的事情。
然而,这是一个学习项目,我试图弄清楚如何使用 Caliburn.Micro 框架进行更简洁的设计,特别是带有 WPF gui 的 MVVM。服务器部分是固定的,但我正在做整个客户端部分,包括 DAL。
使用 DG+DT 组合,可以非常轻松地在 DG 中进行大量编辑,并且当用户提交时,只需迭代 Rows
、检查 RowState
属性并触发 create/根据需要更新/删除 DAL 方法。不过,DataTable 似乎不太适合 MVVM 数据绑定,并且所涉及的 ViewModel 不应该关心它们所使用的 UI 控件类型……因为持久性是通过 Web 服务完成的,不过,要求批量提交修改似乎是足够合理的。
所以我正在思考我的设计选择是什么。
据我了解,DAL 应该处理模型层对象(我认为没有必要为这个项目引入 DTO),并且这些对象将在编辑器 ViewModel 中进行数据绑定之前包装在 ViewModel 中。
到目前为止,我能想到的最好的想法是在启动编辑器 ViewModel 时克隆要编辑的项目集合,然后在提交时根据副本检查数据绑定集合 - 这将让我检测新的/修改的/删除的对象,但似乎有点乏味。
我还考虑过保留 IsModified
和 IsNewlyCreated
属性(我猜这些会进入 ViewModel?) - 跟踪已删除的项目可能可以通过将可编辑项目保留在 ObservableCollection
中,处理 CollectionChanged
事件,并将已删除的项目添加到单独的列表中?
正如你所看到的,我非常不确定如何处理这个问题,任何建议将不胜感激:)
I'm finding myself with a bit of anarchitectural problem: I'm working on a smallish project that, among other things, involves data entry and persistance, with a DAL using a webservice with a custom JSON protocol. So far, so good, and it would be a relatively simple matter slapping together some quick&dirty DataTable
+ DataGrid
code and be done with it.
This is a learning project, however, and I'm trying to figure out how to do a somewhat cleaner design, specifically MVVM with a WPF gui, using the Caliburn.Micro framework. The server part is fixed, but I'm doing the entire client part, including the DAL.
With the DG+DT combo, it's pretty easy doing a bunch of edits in the DG, and when user commits simply iterate the Rows
, checking the RowState
property and firing create/update/delete DAL methods as necessary. DataTable
doesn't seem very MVVM databinding-friendly, though, and the ViewModels involved shouldn't care what kind of UI control they're being used with... given that persistance is done through a webservice, requiring batch commit of modifications seems reasonable enough, though.
So I'm pondering what my design options are.
As I understand it, the DAL should deal with model-layer objects (I don't think it's necessary to introduce DTOs for this project), and these will be wrapped in ViewModels before being databound in editor ViewModels.
The best idea I've been able to come up with so far is to do a clone of the items-to-be-edited collection when firing up an editor ViewModel, then on commit checking the databound collection against the copy - that'll let me detect new/modified/deleted objects, but seems somewhat tedious.
I've also toyed with the idea of keeping IsModified
and IsNewlyCreated
properties (I guess those would go in the ViewModel?) - keeping track of deleted items could probably be handled by keeping the editable items in an ObservableCollection
, handling the CollectionChanged
event, and adding deleted items to a separate list?
As you can see, I'm pretty unsure how to handle this, and any suggestions would be greatly appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先
1- 不要进行任何更改,直到您达到不更改代码就无法生存的地步。
2-正如您已经说过的,您正在创建一个学习项目,并且想要更多的模块化应用程序,因此我的想法将围绕如何在深入实现细节之前首先使我的应用程序更加模块化。
3-您是否考虑过使用Prism + MVVM框架?
4-我仍然建议在您的 ViewModel 中,您仍然可以使用 DataTable 将数据绑定到网格,并通过使用 DataTable.GetChanges() 方法将为您提供表中的所有更改,因此您无需维护布尔值IsNew 或 IsModified 等变量。
5- 如果您还不相信使用 DataTable,请使用 ObservrableCollection 将数据绑定到网格。 ObservrableCollection 不会通知单个项目发生更改,它只会在添加或删除项目时通知。
First of all
1- Don't do any changes untill you reached a point where you can't live without code changes.
2- As you already said that , you are creating a learning project and you want more modular application so my thoughts would be revolving around how to make my application more modular first before going deep into implementational details.
3- Have you considered using Prism + MVVM framework?
4- I would still suggest that in your ViewModel , you can still use DataTable to bind the data to the grids and byusing DataTable.GetChanges() method will give you all the changes in the table so you don't ever need to maintain boolean varaibles like IsNew or IsModified.
5- If you are not convinced using DataTable yet than use ObservrableCollection to bind data to the grid. ObservrableCollection does not notify indivisual item changed , it will only notify when item are added or removed.