MVC中的视图模型使用领域模型

编程入门 行业动态 更新时间:2024-10-23 23:31:57
本文介绍了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中的视图模型使用领域模型

本文发布于:2023-11-25 13:52:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630018.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   视图   领域   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!