在 MVC 中使用数据注释时,使用接口与元数据类型的优缺点 [英] When using Data Annotations with MVC, Pro and Cons of using an interface vs. a MetadataType

查看:28
本文介绍了在 MVC 中使用数据注释时,使用接口与元数据类型的优缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您阅读了这篇文章关于使用数据注释验证器进行验证,它表明您可以使用 MetadataType 特性将验证特性添加到部分类的属性中.在处理 LINQ to SQL、实体框架或 Subsonic 等 ORM 时,您可以使用它.然后您可以使用自动"客户端和服务器端验证.它与 MVC 配合得非常好.

If you read this article on Validation with the Data Annotation Validators, it shows that you can use the MetadataType attribute to add validation attributes to properties on partial classes. You use this when working with ORMs like LINQ to SQL, Entity Framework, or Subsonic. Then you can use the "automagic" client and server side validation. It plays very nicely with MVC.

然而,我的一位同事使用了一个接口来完成完全相同的结果.它看起来几乎完全一样,并且在功能上完成了同样的事情.所以不要这样做:

However, a colleague of mine used an interface to accomplish exactly the same result. it looks almost exactly the same, and functionally accomplishes the same thing. So instead of doing this:

[MetadataType(typeof(MovieMetaData))]
public partial class Movie
{
}

public class MovieMetaData
{
    [Required]
    public object Title { get; set; }

    [Required]
    [StringLength(5)]
    public object Director { get; set; }


    [DisplayName("Date Released")]
    [Required]
    public object DateReleased { get; set; }
}

他这样做了:

public partial class Movie :IMovie
{
}

public interface IMovie
{
    [Required]
    object Title { get; set; }

    [Required]
    [StringLength(5)]
    object Director { get; set; }


    [DisplayName("Date Released")]
    [Required]
    object DateReleased { get; set; }
}

所以我的问题是,这种差异何时真正重要?

我的想法是接口往往更可重用",并且只为一个类创建一个接口并没有多大意义.您也可以争辩说,您可以以允许您在多个对象上使用接口的方式来设计您的类和接口,但我觉得这是试图将您的模型适合其他东西,而当它们真正独立时.你怎么看?

My thoughts are that interfaces tend to be more "reusable", and that making one for just a single class doesn't make that much sense. You could also argue that you could design your classes and interfaces in a way that allows you to use interfaces on multiple objects, but I feel like that is trying to fit your models into something else, when they should really stand on their own. What do you think?

推荐答案

我喜欢你的接口方法,因为它允许你为你的模型定义一个契约,你可以用它来调整你的 ORM 生成的类.这将允许您将您的应用程序与 ORM 框架分离,并更多地利用 MetadataType 接口,因为它用作数据验证元数据以及模型的契约.您还可以使用序列化属性装饰您的界面,以便在 WCF 中使用,从而获得更多的界面使用.我关注了一些推荐创建元数据类的早期博客,但我再次认为接口解决方案是个好主意.

I like your interface approach as it allows you to define a contract for your model which you can use to adapt your ORM generated classes to. That would allow you to decouple your app from the ORM framework and get more use out of the MetadataType interface as it serves as data validation metadata as well as a contract for your model. You could also decorate your interface with serialization attributes for use in WCF gaining more use out of the interface. I followed a few early blogs that recommended creating a metadata class but again I think the interface solution is a nice idea.

这篇关于在 MVC 中使用数据注释时,使用接口与元数据类型的优缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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