Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do Data Annotations work?

Tags:

I use Data Annotations in my ASP.NET MVC 3 project to validate the model. These are extremely convenient but currently they are magic to me. I read that data annotations do not throw exceptions. How then does MVC know to add validation errors to the model state dictionary? How does the failure to set a property on the model because of model validation bubble up to MVC if no exception is thrown? I always assumed that exceptions were thrown every time a property failed and that MVC model binding caught the exception and added it to the model state dictionary.

To test this I created a console application and added a sample class with a validation annotation to it:

public class MyObject {     [StringLength(10, MinimumLength=3)]     public string Name { get; set; } } 

I then instantiated the object and tried to assign values to the Name property that were less than 3. The property assigned just fine, despite the annotation that says string length of less than 3 is not allowed.

    static void Main(string[] args)     {         MyObject mine = new MyObject();         mine.Name = "hi";         Console.WriteLine(mine.Name);         Console.ReadLine();     } 

This little program writes out "hi" to the console. Why? I was expecting it to get angry when trying to set mine.Name to "hi".

What am I missing?

Thanks in advance.

like image 964
Chev Avatar asked Jun 27 '11 17:06

Chev


People also ask

How are data annotations done?

It involves creating bounding boxes (for object detection) and segmentation masks (for semantic and instance segmentation) to differentiate the objects of different classes. In V7, you can also annotate the image using tools such as keypoint, 3D cuboids, polyline, keypoint skeleton, and a brush.

What is data annotation explain?

Data annotation is the categorization and labeling of data for AI applications. Training data must be properly categorized and annotated for a specific use case. With high-quality, human-powered data annotation, companies can build and improve AI implementations.

Is data annotation a good career?

Emerging field with high salaries The job demands long periods of focus, attention to detail, and ability to handle different aspects of the machine training process. The freshers in the field of data annotation can expect packages ranging from INR 1.1 lakhs to INR 3 lakhs per annum.

What is the importance of data annotation?

Without data annotation, every image would be the same for machines as they don't have any inherent information or knowledge about anything in the world. Data annotation is required to make systems deliver accurate results, help modules identify elements to train computer vision and speech, recognition models.


1 Answers

You never call anything to validate the properties. The validation doesn't happen magically on its own. from http://msdn.microsoft.com/en-us/library/dd901590%28v=vs.95%29.aspx

Manually Validating Values

When you do not use the DataGrid control to provide the interface for editing data, the validation attributes are not automatically applied. However, you can manually apply the validation test by using the Validator class. You can call the ValidateProperty method on the set accessor of a property to check the value against the validation attributes for the property. You must also set bothValidatesOnExceptions and NotifyOnValidationError properties to true when data binding to receive validation exceptions from validation attributes. For an example of manually applying validation, see the Data Binding Example below.

like image 164
John Gardner Avatar answered Sep 22 '22 13:09

John Gardner