Data Annotations

Exo supports rich attribute-based validation for models using System.ComponentModel.DataAnnotations attributes.  The ExoRule assembly includes a robust set of property-centric validation rules, including:

The ExoRule.DataAnnotations assembly provides the AnnotationsRuleProvider, which detects and translates data annotation attributes into corresponding model rules.  This assembly adds the following additional data annotion attributes: AllowedValuesAttribute, CompareAttribute, and ListLengthAttribute.  The annotations therefore translate as follows:

The ExoModel.EntityFramework assembly also leverages data annotations to help describe the model:

In the following example, all of the attributes are data annotations, and all of them are leveraged by EF and Exo to support a rich server and client model with no additional effort.

[Required]

public string Description { get; set; }

  

[DisplayFormat(DataFormatString = "d")]

public DateTime? DueDate { get; set; }

 

[AllowedValues("Priority.All")]

public Priority Priority { get; set; }

 In ExoWeb, these anotations translate directly to the client model as corresponding model metadata or client rules, resulting in a wide variety of automatic behavior in the user interface.  For example, when editing instances of this type (todo items), the client dialog template is just:

<div class="sys-template board-list-edititem-dialog" sys:attach="dataview" width="400">

       <div class="dialog-content">

              <div sys:attach="content" content:template="form" content:data="{@ Description }"></div>

              <div sys:attach="content" content:template="form" content:data="{@ DueDate }"></div>

              <div sys:attach="content" content:template="form" content:data="{@ Priority }"></div>

       </div>

       <div class="dialog-footer">

              <input type="button" sys:command="ok" onclick="return false;" value="Ok" />

              <input type="button" sys:command="cancel" onclick="return false;" value="Cancel" />

       </div>

</div>

Which results in rendering:

The user interface leverages model information and rules derived from data annotations as follows:


  1. Insert the following code snippet into the TodoContext class to enable data annotation rule support:
    		static IRuleProvider annotationRules = new AnnotationsRuleProvider(typeof(List).Assembly);
    				
  2. Replace the contents of the "ok" button handler in the scripts/board-list-edititem.js file with the following snippet:
    if (item.meta.conditions().length === 0) {
    	context.server.enableSave(item);
    }
    else {
    	return false;
    }