Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Validatable Objects and MetadataType attributes are used in the partial classes by frameworks such as MVC, for example.</p> <p><strong>Here's an example of using the MetadataType attribute:</strong></p> <pre><code>[MetadataType(typeof(UserMetadata))] public partial class User { private class UserMetadata { [DisplayName("User Id")] public long UserId { get; set; } [DisplayName("User Name")] public string UserName { get; set; } } } </code></pre> <p>When you use the MVC framework, any model that has these attributes will be read for purposes of automatic generation of label fields for the corresponding displays/editors.</p> <p><strong>Example of using IValidatableObject</strong></p> <pre><code>public partial class Apple : IValidatableObject // Assume the Apple class has an IList&lt;Color&gt; property called AvailableColors { public IEnumerable&lt;ValidationResult&gt; Validate(ValidationContext validationContext) { foreach (var color in this.AvailableColors) { if (color.Name == "Blue") // No blue apples, ever! { yield return new ValidationResult("You cannot have blue apples."); } } } } </code></pre> <p>MVC will pick up on this IValidatableObject and ensure that any Apple that comes its way through it's validation step will never come in blue.</p> <p><strong>EDIT</strong></p> <p>As an example of your date range concern using IValidatableObject:</p> <pre><code>public partial class ObjectWithAStartAndEndDate : IValidatableObject { public IEnumerable&lt;ValidationResult&gt; Validate(ValidationContext validationContext) { if (this.StartDate &gt;= this.EndDate) { yield return new ValidationResult("Start and End dates cannot overlap."); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload