MVC中的视图模型使用领域模型 [英] MVC Using Domain Models in View Models

查看:409
本文介绍了MVC中的视图模型使用领域模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的确定呢?我知道,领域模型不应该在视图中使用,但它是确定你的视图模型使用领域模型?对于一些非常小的模型似乎并不值得被创建和管理视图模型他们。

例如

 公共类LoginDomainModel
{
    公共字符串电子邮件{获得;组; }
    公共字符串密码{搞定;组; }
    公共字符串显示名称{搞定;组; }
    众长UserTypeID {搞定;组; }
    公共虚拟用户类型用户类型{搞定;组; }
}
公共类UserTypeDomainModel
{
    公众用户类型()
    {
        this.Logins =新的List<登录与GT;();
    }
    众长UserTypeID {搞定;组; }
    公共字符串用户类型{搞定;组; }
    公共字符串描述{搞定;组; }
    公共虚拟的ICollection<登录与GT;登录{搞定;组; }
}公共类LoginViewModel
{
    公共字符串电子邮件{获得;组; }
    众长UserTypeID {搞定;组;}    //就在这儿
    公开名单< UserTypeDomainModel> UserTypesSelectList {搞定;组;}
}


解决方案

我个人使用域模型视图的如果他们自然会精确配合的。这很可能仅在本质上CRUD琐碎项目(编辑域实体以简单的方式)的情况发生。我觉得它的时间来创建一个域实体的精确副本纯度的缘故一种浪费。

我会的从不的修改丝毫的领域模型来解释视图的需求。在95%的我的项目的+,这是我发现自己在该时刻的情况下,你污染域视图的缘故,你介绍可维护性头疼的问题。

Is the following OK to do? I know Domain Models should never be used in views but is it ok to use Domain Models in your View Models? For some very small models it doesn't seem worth it to be creating and managing a View Model for them.

For Example

public class LoginDomainModel
{
    public string Email { get; set; }
    public string Password { get; set; }
    public string DisplayName { get; set; }
    public long UserTypeID { get; set; }      
    public virtual UserType UserType { get; set; } 
}
public class UserTypeDomainModel
{
    public UserType()
    {
        this.Logins = new List<Login>();
    }
    public long UserTypeID { get; set; }
    public string UserType { get; set; }
    public string Description { get; set; }
    public virtual ICollection<Login> Logins { get; set; }
}

public class LoginViewModel
{
    public string Email { get; set; }
    public long UserTypeID {get; set;}

    //Right here
    public List<UserTypeDomainModel> UserTypesSelectList {get; set;}
}

解决方案

Personally I use domain models in the view if they would naturally be an exact fit. That is likely to happen only on trivial projects that are CRUD in nature (editing the domain entities in a straightforward way). I find it a waste of time to create an exact copy of a domain entity for the sake of purity.

I will never modify a domain model in the slightest to account for needs of the view. In 95%+ of my projects, this is the circumstance I find myself in. The moment you pollute the domain for the sake of the view, you introduce maintainability headaches.

这篇关于MVC中的视图模型使用领域模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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