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

查看:25
本文介绍了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(隐藏)名姓生物

假设我们的显示视图模型如下所示:

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; }
}

这两个没多大区别吧?编辑模型有一个额外的字段 (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; }
    }

这当然更 DRY,因为我们没有要维护的重复字段,但是现在我们的显示视图模型上有无关的信息(属性).无论如何,我更倾向于第二种方法,因为我们的一些屏幕有超过 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 派生,然后为 定义我的 EditPersonViewModelValidatorEditPersonViewModel 在一个单独的类中.

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] 属性装饰一个不可为空的类型是没有必要的.所有不可为 null 的类型都是必需的,因为它们非常基本的性质.所以,而不是:

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天全站免登陆