在ASP.MVC中派生用于视图模型的数据库模型 [英] Deriving a database model for view model in ASP.MVC

查看:69
本文介绍了在ASP.MVC中派生用于视图模型的数据库模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为ASP MVC Web应用程序创建一些视图模型. 我为数据库创建了代码优先"模型.这是从数据库模型派生视图模型的好方法吗?

I am creating some view models for my ASP MVC web app. I created "code first" models for database. Is it a good way to derive view models from database models?

示例数据库模型:

public class Project
{
    [Key]
    public int Id { get; set; }
    public int? CustomerId { get; set; }   
    public int TypeId { get; set; }   
    public string Number { get; set; }  
    public string Name { get; set; }
}

查看模型:

public class ViewModelProject : Project
{
    [NotMapped]
    public DateTime? Start { get; set; }
    [NotMapped]
    public DateTime? End { get; set; }
    [NotMapped]
    public string Manager { get; set; }
}

这是正确的方法还是完全错误?

Is this the right way or is it completely false?

编辑(子问题): 我有一些非常简单的数据库模型,例如ProjectType,仅包含两个属性.我是否还应该在模型视图中将这些模型分段,还是可以这样: 简单的数据库模型:

EDIT (subquestion): I have some very simple database models like ProjectType, which only contains i.e. two properties. Should I also fragment those models in model view or can I make it that way: Simple database model:

public class ProjectType
{
    [Key]        
    public int Id { get; set; }    
    public string Name { get; set; }
    public int? Code { get; set; } 
}

我可以这样使用它吗?

public class ProjectVM
{
    public string Name { get; set; }
    public int Number { get; set; }
    public ProjectType Type { get; set; }
}

还是必须像这样分段:

public class ProjectVM

    {
        public string Name { get; set; }
        public int Number { get; set; }
        public string Type { get; set; }
        public int TypeCode { get; set; }
    }

推荐答案

我不建议这样做.我(和许多其他人)尝试过,但效果不佳.您将无意中遇到麻烦,因为必须针对视图量身定制MVC模型,而您从数据库中获得的内容却很少.当然,您可以将其固定到位,但是代码很快就会变得混乱,并且与商店相关,UI代码也开始混乱.在您的示例中甚至显示了这一点,因为您必须将NotMappedAttribute(与数据存储相关)放入ViewModelProject(UI级别的类).

I would not recommend doing it this way. I (and many others) have tried it and it doesn't work well. You will inadvertedly run into troubles, since an MVC model has to be tailored to the view and what you get from the DB rarely fits. Sure, you can hammer it into place, but the code quickly gets messy and store-related and UI code starts to mangle together. This even shows in your example, since you have to put the NotMappedAttribute (which is related to data storage), to ViewModelProject (a class at UI level).

还有许多其他示例来显示此问题,但是当您要将模型对象序列化为JSON并将其发送到JavaScript客户端时,我发现一个特别好的示例. JSON序列化器将所有公共属性的值添加到JSON中.如果要排除属性,则必须用ScriptIgnoreAttribute标记它,还必须将其应用于基类,这会破坏UI和商店相关代码之间的分隔.

There are many other examples to show this problem, but an especially good one I find when you want to serialize a model object to JSON and send it to a JavaScript client. The JSON serializer takes the values of all public properties and adds them to the JSON. If you want to exclude a property, you have to mark it with a ScriptIgnoreAttribute, which you would also have to apply to the base class, which breaks separation between UI and store-related code.

最好的方法是将存储模型和MVC模型保持分离,并将数据彼此映射(已经存在可以帮助您的框架,例如Automapper).这具有其他优点,例如更好的可测试性,因为您现在不再依赖特定的数据存储来创建模型实例.

The better way to go is to keep the staorage model and the MVC model separated and to map the data from one to the other (there are already pre-existing frameworks that help you with that, such as Automapper). This comes with additional advantages, for example better testability, since you are now not dependent on a specific data store to create model instances.

这篇关于在ASP.MVC中派生用于视图模型的数据库模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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