在使用“代码优先"的情况下,使用MetadataType强制进行验证是否有意义? [英] Does it make sense to use MetadataType to enforce validations in case of Code First?

查看:90
本文介绍了在使用“代码优先"的情况下,使用MetadataType强制进行验证是否有意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎理解了使用MetadataTypeAttribute帮助 > Database First ,因为我们希望避免下次从数据库生成模型时所做的更改被覆盖.

我注意到很少有人使用MetadataType定义验证,即使他们使用的是解决方案

不直接在实际的Entity类上应用这些DataAnnotation有意义,而是将它们分成部分类定义,然后使用MetadataType进行链接,即使使用代码优先"方法来定义实体模型,也有意义吗?

在大多数情况下,这是没有意义的,因为它涉及不必要的和冗余的代码重复,只是为了将某些属性与属性相关联.

如果实体类模型是由您使用代码创建的,则没有意义.

如果它是由您可以控制的某些自定义代码生成(例如T4模板)创建的,也没有任何意义,因为您可以自定义生成本身.

唯一有意义的情况是您无法控制实体类代码(例如,来自第三方库的类).在这种情况下,您可以使用 AssociatedMetadataTypeTypeDescriptionProvider 类,以将元数据与第三方类相关联.

例如,假设以下类来自另一个没有源代码的库:

public sealed class ExternalEntity
{
    public string Name { get; set;}
}

然后您可以定义元数据类:

public class ExternalEntityMetadata
{
    [Required]
    public string Name { get; set;}
}

并使用 TypeDescriptor.AddProvider 方法一次(在应用程序启动过程中):

TypeDescriptor.AddProvider(new AssociatedMetadataTypeTypeDescriptionProvider(
    typeof(ExternalEntity), typeof(ExternalEntityMetadata),
    typeof(ExternalEntity));

I seem to understand the reason behind taking help of MetadataTypeAttribute to Add Validation to the Model in case of Database First as we want to avoid the changes being overwritten when the model is generated from the database next time.

I've noticed few people defining validation using MetadataType even when they're using Code First approach and there is no chance of their Entity Classes being overwritten by some kind of auto-generation of code.

Does it make any sense to not apply these DataAnnotations on the actual Entity class directly and instead, separate these into partial class definitions and then link using MetadataType, even when using Code First approach to define Entity Model?

public class MyEntity
{
    [Required]
    public string Name { get; set;}
}

vs

public partial class MyEntity
{
    public string Name { get; set;}
}

[MetadataType(typeof(MyEntityMetadata))]
public partial class MyEntity
{
}

public class MyEntityMetadata
{
    [Required]
    public string Name { get; set;}
}

解决方案

Does it make any sense to not apply these DataAnnotations on the actual Entity class directly and instead, separate these into partial class definitions and then link using MetadataType, even when using Code First approach to define Entity Model?

In most of the cases it doesn't make sense because it involves unnecessary and redundant code duplication just to associate some attributes with the properties.

It doesn't make sense if the entity class model is created by you with code.

It also doesn't make sense if it's created with some custom code generation you have control over (like T4 template) because you can customize the generation itself.

The only case when it makes sense is when you have no control over the entity class code (for instance, the class coming from 3rd party library). In such case, you can use AssociatedMetadataTypeTypeDescriptionProvider class to associate metadata with the 3rd party class.

For instance, let say the following class is coming from another library with no source code:

public sealed class ExternalEntity
{
    public string Name { get; set;}
}

Then you can define the metadata class:

public class ExternalEntityMetadata
{
    [Required]
    public string Name { get; set;}
}

and associate it with the ExternalEntity using TypeDescriptor.AddProvider method once (during the application startup or something):

TypeDescriptor.AddProvider(new AssociatedMetadataTypeTypeDescriptionProvider(
    typeof(ExternalEntity), typeof(ExternalEntityMetadata),
    typeof(ExternalEntity));

这篇关于在使用“代码优先"的情况下,使用MetadataType强制进行验证是否有意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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