我应该在 MVVM 中使用 DTO 作为我的数据模型吗? [英] Should I use DTOs as my data models in MVVM?

查看:35
本文介绍了我应该在 MVVM 中使用 DTO 作为我的数据模型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究如何第一次真正尝试使用 MVVM,并且一直在阅读有关如何最好地实施它的各种文章.

I'm currently working on what will be my first real foray into using MVVM and have been reading various articles on how best to implement it.

我目前的想法是将我的数据模型有效地用作数据传输对象,使它们可序列化并使它们同时存在于客户端和服务器端.考虑到这两种对象类型实际上只是属性 getter 和 setter 的集合,这似乎是一个合乎逻辑的步骤,而介于两者之间的另一层似乎完全矫枉过正.

My current thoughts are to use my data models effectively as data transfer objects, make them serializable and have them exist on both the client and server sides. It seems like a logical step given that both object types are really just collections of property getters and setters and another layer in between seems like complete overkill.

显然,INotifyPropertyChanged 在服务器端无法正常工作会出现问题,因为没有可与之通信的 ViewModel,但只要我们小心地从服务层中的数据模型构建我们正确的域模型对象,而不是在服务器端处理数据模型我认为这不是一个大问题.

Obviously there would be issues with INotifyPropertyChanged not working correctly on the server side as there is no ViewModel to which to communicate, but as long as we are careful about constructing our proper domain model objects from data models in the service layer and not dealing the the data models on the server side I don't think it should be a big issue.

我在阅读中没有找到太多关于这种方法的信息,所以我想知道这是否是一个非常标准的事情,这是否只是假设是在多层中进行 MVVM 的事实上的方式环境?如果我对事物有完全错误的想法,那么对其他方法的想法也将不胜感激.

I haven't found too much info about this approach in my reading, so I would like to know if this is a pretty standard thing, is this just assumed to be the de facto way of doing MVVM in a multi-tier environment? If I've got completely the wrong idea about things then thoughts on other approaches would be appreciated too.

推荐答案

我不是这方面的专家.我有同样的场景.我同意你的看法,这是一种矫枉过正的做法.我已经使用这个解决方案有一段时间了,没有遇到任何问题.INotifyPropertyChanged 对我来说不是一个大问题,因为服务器端没有任何东西会订阅 PropertyChanged 事件.如果您将在数据模型上使用继承,那么所有模型都必须是可序列化的.在我的场景中,我的数据模型有两个基类:一个用于数据传输,另一个没有.

I'm no expert on this. I had just the same scenario. I agree with you that that is quite an overkill. I've been using this solution for quite some time now and haven't encountered any issues. The INotifyPropertyChanged isn't a big problem for me since nothing on the server-side will subscribe to the PropertyChanged event. If you will use inheritance on your data models, then all must be serializable. In my scenario, I have two base classes for my data models: one that is used for data transfer, and the other not.

这篇关于我应该在 MVVM 中使用 DTO 作为我的数据模型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆