Created
December 28, 2015 12:11
-
-
Save miklund/108a553a92ca715981a4 to your computer and use it in GitHub Desktop.
2009-11-07 Data Annotations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Title: Data Annotations | |
| # Author: Mikael Lundin | |
| # Link: http://blog.mikaellundin.name/2009/11/07/data-annotations.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var person = new Person | |
| { | |
| Name = "John Doe", | |
| Age = 54, | |
| Email = "[email protected]" | |
| }; | |
| /* Validate */ | |
| var result = person.ValidateAnnotations(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [MetadataType(typeof(IPerson))] | |
| public class Person : IPerson | |
| { | |
| public string Name { get; set; } | |
| public int Age { get; set; } | |
| public string Email { get; set; } | |
| } | |
| public interface IPerson | |
| { | |
| [StringLength(20), Required(ErrorMessage = "Name is a required property")] | |
| string Name { get; set; } | |
| [Range(0, 150)] | |
| int Age { get; set; } | |
| [RegularExpression(@"(?i:^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$)")] | |
| string Email { get; set; } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Person | |
| { | |
| [StringLength(20), Required(ErrorMessage = "Name is a required property")] | |
| public string Name { get; set; } | |
| [Range(0, 150)] | |
| public int Age { get; set; } | |
| [RegularExpression(@"(?i:^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$)")] | |
| public string Email { get; set; } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment