MVC体系结构-重新使用相同的视图模型进行读取和编辑 [英] MVC architecture - re-using the same viewmodel for reads and edits

查看:56
本文介绍了MVC体系结构-重新使用相同的视图模型进行读取和编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有以下(过于简单)的情况:

Say we have the following (overly simple) scenario:

我们有一个用于查看人员详细信息的屏幕和一个用于编辑人员详细信息的屏幕.

We have a screen to view person details and a screen to edit person details.

屏幕显示人员的详细信息具有以下字段(仅作为显示):

The screen display person details has the following fields (as display only):

名字 姓 生物

屏幕编辑人员的详细信息显示具有以下字段(在输入控件中):

The screen edit person details shows has following fields (in input controls):

ID(隐藏) 名 姓 生物

ID (hidden) First Name Last Name Bio

说我们的显示视图模型如下:

Say our display viewmodel looks like this:

    public class DisplayPersonViewModel
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Bio { get; set; }
    }

我们的编辑视图模型如下:

And our edit viewmodel looks like this:

public class EditPersonViewModel
{
    [Required]
    public int ID { get; set; }

    [Required]
    [StringLength(20)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(20)]
    public string LastName { get; set; }

    [Required]
    public string Bio { get; set; }
}

2之间相差不大,是吗?编辑模型具有一个额外的字段(ID)和属性上的一些属性.现在,如果我们要像这样将两个结合起来:

Not much difference between the 2, eh? The edit model has one extra field (ID) and some of attributes on the properties. Now, if we were to combine the 2 like this:

    public class DisplayPersonViewModel
    {
        [Required]
        [StringLength(20)]
        public string FirstName { get; set; }

        [Required]
        [StringLength(20)]
        public string LastName { get; set; }

        [Required]
        public string Bio { get; set; }
    }

    public class EditPersonViewModel : DisplayPersonViewModel
    {
        [Required]
        public int ID { get; set; }
    }

这肯定是更干燥的,因为我们没有要维护的重复字段,但是现在我们在显示视图模型上有了无关的信息(属性).无论如何,我更倾向于第二种方法,因为我们的某些屏幕具有25个以上的字段! (...这是我无法控制的,所以请不要竖琴:)...)但是,我只是想听听大家的意见,以更好地理解什么是最佳实践".

This is certainly more DRY, since we don't have duplicate fields to maintain, but now we have extraneous info (attributes) on our display viewmodel. I'm leaning more towards the second approach, regardless, because some of our screens have over 25 fields! (...and that's out of my control, so please don't harp on it :) ...) However, I just want to hear opinions to get a better sense of what may be "best practice".

推荐答案

是的,第二种方法对我来说似乎不错.除了胃部发痒的感觉,告诉您为什么在地球上装饰带有验证属性的显示视图模型外,您无需担心.但是,如果可以忍受的话,与复制视图模型相比,它确实是首选.

Yes, the second approach seems fine to me. No worries other than this itchy feeling in your stomach telling you why on earth are you decorating a display view model with validation attributes. But if you can live with it it's really something that is preferred compared to duplicating the view models.

不幸的是,我个人无法忍受这种感觉,这就是为什么我使用 FluentValidation.NET 进行定义的原因我的验证规则,而不是数据注释.它使我可以将这些规则与视图模型分开,然后不必担心会用验证规则污染所谓的显示视图模型.因此,我将以与您2个视图模型相同的方式进行定义,并且EditPersonViewModel将从DisplayPersonViewModel派生,然后在单独的类中为EditPersonViewModel定义我的EditPersonViewModelValidator.

Unfortunately personally I cannot live with this feeling in my stomach and that's why I use FluentValidation.NET to define my validation rules instead of data annotations. It allows me to have those rules separately from my view models and then I don't worry about polluting the so called display view model with validation rules. So I would define in the same way as you 2 view models and EditPersonViewModel would derive from DisplayPersonViewModel and then define my EditPersonViewModelValidator for the EditPersonViewModel in a separate class.

哦,还有一个注意事项:不需要用[Required]属性修饰非空类型.由于其非常基本的性质,必需是所有非空类型.因此,代替:

Oh and a side note: decorating a non-nullable type with the [Required] attribute is not necessary. All non-nullable types are required by their very basic nature. So instead of:

[Required]
public int ID { get; set; }

您应该只有:

public int ID { get; set; }

这篇关于MVC体系结构-重新使用相同的视图模型进行读取和编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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