在Code First的情况下,使用MetadataType强制执行验证是否有意义?

[英]Does it make sense to use MetadataType to enforce validations in case of Code First?


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.

我似乎理解在数据库优先的情况下帮助MetadataTypeAttribute向模型添加验证背后的原因,因为我们希望避免在下次从数据库生成模型时覆盖的更改。

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.

我注意到很少有人使用MetadataType定义验证,即使他们使用Code First方法并且他们的实体类没有被某种自动生成代码覆盖的可能性。

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?

将这些DataAnnotations直接应用于实际的Entity类是否有意义,而是将它们分离为部分类定义,然后使用MetadataType进行链接,即使使用Code First方法定义实体模型也是如此?

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

vs

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

4 个解决方案

#1


7  

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?

将这些DataAnnotations直接应用于实际的Entity类是否有意义,而是将它们分离为部分类定义,然后使用MetadataType进行链接,即使使用Code First方法定义实体模型也是如此?

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.

如果使用您可以控制的一些自定义代码生成(如T4模板)创建它也没有意义,因为您可以自定义生成本身。

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.

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

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方法将其与ExternalEntity关联一次(在应用程序启动期间或其他事情):

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

#2


0  

It really makes sense to create a class and use it many times. In code first approach you need data validation which is possible to implement by using data annotations and When you have many props with the same features your life will be easier by doing so. It is not just about overwriting and in this case it has some other reasons. Hope to understand your question well and my answer is appropriate.

创建一个类并多次使用它确实很有意义。在代码优先方法中,您需要通过使用数据注释来实现数据验证。当您拥有许多具有相同功能的道具时,您的生活将更容易实现。这不仅仅是覆盖,在这种情况下还有其他一些原因。希望能很好地理解你的问题,我的答案是恰当的。

#3


0  

I think the questions is where is the difference between data annotations on model and on code first.

我认为问题在于模型上的数据注释和代码之间的区别。

So at first you have data validation

所以首先你有数据验证

this is setting up attributes on your code first model and this sets up configuration of database columns and this will set the size and restrictions on your data model. (this once populated usually does not change without migrating data.)

这是在代码优先模型上设置属性,这将设置数据库列的配置,这将设置数据模型的大小和限制。 (如果没有迁移数据,这一次填充通常不会改变。)

Model validation

模型验证

model validation is your model you are binding your form into. This model would contain more information for your UI.

模型验证是您将表单绑定到的模型。此模型将包含UI的更多信息。

#4


0  

I don't know why you are trying to employ a Database first technique to a more complete, say, Code first since you can create ViewModels to meet your purpose. Also not all the data annotations are supported in Entity Framework.

我不知道你为什么要尝试使用数据库优先技术来更完整,比如代码,因为你可以创建ViewModel来满足你的目的。实体框架中也不支持所有数据注释。

MetaDataType limitations

  1. It cannot be applied to a property and can be only be applied to a single class for each class type.
  2. 它不能应用于属性,只能应用于每个类类型的单个类。
  3. This attribute cannot be inherited, so you cannot customize it.
  4. 此属性无法继承,因此您无法对其进行自定义。
  5. On the other side, this attribute can be applied to partial class which is the main purpose of this attribute.
  6. 另一方面,此属性可以应用于部分类,这是此属性的主要目的。
  7. This attribute will be respected by ASP.NET MVC but will not be read by Entity Framework.
  8. ASP.NET MVC将遵循此属性,但实体框架不会读取此属性。

Cons of using MetaDataType

  • you have to use ViewBag, ViewData or something else to pass additional information to the view
  • 您必须使用ViewBag,ViewData或其他东西将其他信息传递给视图
  • Your design is less testable since it relies on a static object mechanism.
  • 您的设计不太可测试,因为它依赖于静态对象机制。
  • It is also not required and someone could omit it without breaking anything.
  • 它也不是必需的,有人可以省略它而不会破坏任何东西。
  • It also means that you are splitting your model class into 3 files. One generated, one of yours and one with attributes.
  • 它还意味着您将模型类拆分为3个文件。一个生成,一个生成,一个生成属性。

If you want to add attributes to existing properties in a class (partially) :

如果要将属性添加到类中的现有属性(部分):

This may work or be ignored by the EF, test it:

这可能会被EF工作或忽略,测试它:

public partial class YourModelClass
{
    public string YourProperty{get;set;}
}

//Your Partial Class
[MetadataType(typeof(YourModelClassMetaData))]
public partial class YourModelClass
{
}

//The class that adds your attributes
public class YourModelClassMetaData
{
    [Required]
    public object YourProperty{get;set;}
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2016/08/14/16c0c68d91b27b52d2d51632f41477a3.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号