c# - 在MVVM设计模式中,模型应该包含其他模型吗?

标签 c# mvvm xamarin.forms

我想我没有看过这样的例子,但是我也没有读过任何明确指出不应这样做的地方。例如,假设我有一些用户模型,其中包含诸如姓氏等的常用信息:

public class UserModel
{
    private int userID;
    public int UserID
    {
        get { return userID; }
    }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string MiddleInitial { get; set; }

    ...

}

如果我严格遵循MVVM模式,是否可以使用其他模型列表,例如
public class UserModel
{
    ...
    public List<SomeOtherModel> SomeList { get; set; }

}

还是模型应该只有简单的类型?

最佳答案

很简单,是的。
在进行MVVM时,您需要稍微改变一下观点。 “模型”的定义与MVC不同,而是包含 View 和 View 模型中不是的所有内容。这意味着“模型”是数据实体(即POCO),包括任何服务或 Controller ,包括业务逻辑等:

Model

The model in MVVM is an implementation of the application's domain model that includes a data model along with business and validation logic. Examples of model objects include repositories, business objects, data transfer objects (DTOs), Plain Old CLR Objects (POCOs), and generated entity and proxy objects.


引用自MSDN文章The MVVM Pattern
因此,现在您已经改变了看法,如果您的问题变成POCO(DTO)是否应包含其他POCO ?,那么答案仍然是。

关于c# - 在MVVM设计模式中,模型应该包含其他模型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49484081/

相关文章:

android - MVVM从模型到存储库的返回值

c# - Xamarin 使用 xaml 创建具有多个详细 View 的 masterdetailpage

c# - 如何确定进程对象正在运行

c# - 在 xaml 窗口和用户控件 WPF 之间传递参数

mvvm - Xamarin.Forms 控件模板绑定(bind)不起作用

c# - ZXing.Net后退导航C#

Xamarin Forms TableView 所选项目处理程序?

c# - 搜索 .NET(和 ASP.NET)的标签系统

c# - 将 bytes[] 转换为文字字符串

c# - 使用 c# 和 TPL 数据流的并发计数器模式