是否有可能重新使用视图模型的DataAnnotations? [英] Is it possible to reuse the DataAnnotations in ViewModel?

查看:134
本文介绍了是否有可能重新使用视图模型的DataAnnotations?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的MVC应用程序,我在域模型中定义的DataAnnotations。虽然DataAnnotations属性显示等,可以使用域模型时被检索,它们不能使用在视图模型相同的属性,并使用该视图模型时检索。我觉得这似乎是好到不能再定义视图模型的DataAnnotations。因此,它是可能的,或者我应该遵循何种方式?


域模型:

 公共类问题
{
    [键]
    公众诠释ID {搞定;组; }    [必需的(=的ErrorMessage必需)]
    [显示(名称=项目编号)]
    公众诠释专案编号{搞定;组; }    [必需的(=的ErrorMessage必需)]
    [显示(NAME =问题定义)
    公共字符串描述{搞定;组; }    // ...删除为了简洁    //导航属性:
    公共虚拟的ICollection<&FileAttachment的GT; FileAttachments {搞定;组; }
}


视图模型:

 公共类IssueViewModel
{
    公众诠释ID {搞定;组; }    公众诠释专案编号{搞定;组; }    公共字符串描述{搞定;组; }    // ...删除为了简洁    //导航属性:
    公共虚拟的ICollection<&FileAttachment的GT; FileAttachments {搞定;组; }
}


解决方案

您可以创建一个新的好友类,它包含有关属性和类别的所有元数据。

 公共部分类IssueMetadata
{
    [必需的(=的ErrorMessage必需)]
    [显示(名称=项目编号)]
    公众诠释专案编号{搞定;组; }    [必需的(=的ErrorMessage必需)]
    [显示(NAME =问题定义)
    公共字符串描述{搞定;组; }
}

然后,我们必须告诉MVC框架关于通过 MetadataType 属性哥们类,这需要好友类作为其参数的类型。巴迪班必须在相同的命名空间进行定义和
还必须部分类。

  [MetadataType(typeof运算(IssueMetadata))]
公共部分类IssueViewModel
{
      // ...      公众诠释专案编号{搞定;组; }
      公共字符串描述{搞定;组; }      // ...
}[MetadataType(typeof运算(IssueMetadata))]
公共部分类问题
{
      [键]
      公众诠释ID {搞定;组; }      公众诠释专案编号{搞定;组; }
      公共字符串描述{搞定;组; }      // ...删除为了简洁      //导航属性:
      公共虚拟的ICollection<&FileAttachment的GT; FileAttachments {搞定;组; }
}

附加说明:

如果 IssueMetadata 位于问题(或 IssueViewModel )班不同的组件,那么你可以用类运行时的哥们类关联,这样的:

 公共类AssociatedMetadataConfig
{
    公共静态无效RegisterMetadatas()
    {
        RegisterPairOfTypes(typeof运算(发行)的typeof(IssueMetadata));
        RegisterPairOfTypes(typeof运算(IssueViewModel)的typeof(IssueMetadata));
    }    私有静态无效RegisterPairOfTypes(类型mainType,类型buddyType)
    {
        AssociatedMetadataTypeTypeDescriptionProvider typeDescriptionProvider
          =新AssociatedMetadataTypeTypeDescriptionProvider(mainType,buddyType);        TypeDescriptor.AddProviderTransparent(typeDescriptionProvider,mainType);
    }
}

和,就调用这个静态方法的Global.asax

  AssociatedMetadataConfig.RegisterMetadatas();

In my MVC application, I defined the DataAnnotations in the domain models. Although the DataAnnotations properties as Display, etc. can be retrieved when using Domain model, they cannot be retrieved when using the same properties on ViewModel and using this ViewModel. I think it is not seem to good to define the DataAnnotations in ViewModel again. So, is it possible or which way should I follow?


Domain Model:

public class Issue
{
    [Key] 
    public int ID { get; set; }

    [Required(ErrorMessage = "Required")]
    [Display(Name = "Project Number")]
    public int ProjectID { get; set; }

    [Required(ErrorMessage = "Required")]
    [Display(Name = "Issue Definition")]
    public string Description { get; set; }

    //... removed for brevity

    //Navigation Properties:
    public virtual ICollection<FileAttachment> FileAttachments { get; set; }
}


ViewModel:

public class IssueViewModel
{
    public int ID { get; set; }

    public int ProjectID { get; set; }

    public string Description { get; set; }

    //... removed for brevity

    //Navigation Properties:
    public virtual ICollection<FileAttachment> FileAttachments { get; set; }      
}

解决方案

You can create a new buddy class which holds all metadata about properties and class.

public partial class IssueMetadata
{
    [Required(ErrorMessage = "Required")]
    [Display(Name = "Project Number")]
    public int ProjectID { get; set; }

    [Required(ErrorMessage = "Required")]
    [Display(Name = "Issue Definition")]
    public string Description { get; set; }
}

Then, we must tell the MVC Framework about the buddy class through the MetadataType attribute, which takes the type of the buddy class as its argument. Buddy classes must be defined in the same namespace and must also be partial classes.

[MetadataType(typeof(IssueMetadata))]
public partial class IssueViewModel
{
      //...

      public int ProjectID { get; set; }
      public string Description { get; set; }

      //...
}

[MetadataType(typeof(IssueMetadata))]
public partial class Issue
{
      [Key] 
      public int ID { get; set; }

      public int ProjectID { get; set; }
      public string Description { get; set; }

      //... removed for brevity

      //Navigation Properties:
      public virtual ICollection<FileAttachment> FileAttachments { get; set; }
}

Additional note:
If IssueMetadata and Issue (or IssueViewModel) classes located in different assemblies, then you can associate classes with their buddy class in runtime, like that:

public class AssociatedMetadataConfig
{
    public static void RegisterMetadatas()
    {
        RegisterPairOfTypes(typeof(Issue), typeof(IssueMetadata));
        RegisterPairOfTypes(typeof(IssueViewModel), typeof(IssueMetadata));
    }

    private static void RegisterPairOfTypes(Type mainType, Type buddyType)
    {
        AssociatedMetadataTypeTypeDescriptionProvider typeDescriptionProvider 
          = new AssociatedMetadataTypeTypeDescriptionProvider(mainType, buddyType);

        TypeDescriptor.AddProviderTransparent(typeDescriptionProvider, mainType);
    }
}

And, just call this static method in global.asax:

AssociatedMetadataConfig.RegisterMetadatas();

这篇关于是否有可能重新使用视图模型的DataAnnotations?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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