Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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;}
}
like image 531
sachin Avatar asked Aug 14 '16 08:08

sachin


2 Answers

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));
like image 117
Ivan Stoev Avatar answered Nov 16 '22 23:11

Ivan Stoev


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.

like image 1
Mohsen Shadman Avatar answered Nov 17 '22 01:11

Mohsen Shadman