使用数据注释与MVC,Pro和Cons使用接口与MetadataType时 [英] When using Data Annotations with MVC, Pro and Cons of using an interface vs. a MetadataType

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

问题描述

如果您阅读这篇文章在使用数据注释验证器进行验证时,它表明您可以使用MetadataType属性将验证属性添加到部分类的属性。在使用LINM to SQL,Entity Framework或Subsonic等ORM时,可以使用它。然后,您可以使用automagic客户端和服务器端验证。它与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,Pro和Cons使用接口与MetadataType时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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