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

前端 未结 4 1609
情书的邮戳
情书的邮戳 2021-02-13 11:10

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

相关标签:
4条回答
  • 2021-02-13 11:35

    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));
    
    0 讨论(0)
  • 2021-02-13 11:47

    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.

    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. This attribute cannot be inherited, so you cannot customize it.
    3. On the other side, this attribute can be applied to partial class which is the main purpose of this attribute.
    4. This attribute will be respected by ASP.NET MVC but will not be read by Entity Framework.

    Cons of using MetadataType

    • you have to use ViewBag, ViewData or something else to pass additional information to the view
    • 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.

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

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

    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;}
    }
    
    0 讨论(0)
  • 2021-02-13 11:49

    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.

    0 讨论(0)
  • 2021-02-13 12:01

    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.

    0 讨论(0)
提交回复
热议问题