Databases Reference
In-Depth Information
context.SaveChanges();
}
}
The EnumDataTypeAttribute supports not only the integral data types, such as Byte used
in this example, but also enum and string data types. For properties of enum types, the
EnumDataTypeAttribute ensures that the enum value is actually defined in the enum type.
For string properties, the EnumDataTypeAttribute parses the enum value from the string
and then checks if the enum type defines it. Keep in mind that it uses case-sensitive logic
for string parsing, which might not be appropriate for validating string values entered by
the users directly.
DataTypeAttribute
Although the DataTypeAttribute inherits from the ValidationAttribute class, it does not
perform any actual validation. Its IsValid method only checks the attribute's own para-
meters. Dynamic Data uses the DataTypeAttribute when looking for a field template
appropriate for a given property. For example, the HomePage property of the Supplier entity
stores URLs, and by applying the DataTypeAttribute as shown next, you can make
Dynamic Data use the URL field template, which generates a hyperlink:
[ DataType(DataType.Url)]
public object HomePage { get; set; }
The DataTypeAttribute can help you to implement client-side validation in Dynamic
Data web applications if you provide appropriate Edit mode field templates for the differ-
ent values of the DataType enumeration, such as Currency , Time , or PhoneNumber . For
details, please see Chapter 3.
Imperative Validation
The simple declarative validation discussed so far is a good first step but is rarely suffi-
cient. Most enterprise applications require more complex validation logic that can span
multiple properties, multiple entities, and require custom code.
CustomValidationAttribute
The CustomValidationAttribute allows you to specify a static method that will be
invoked to validate a particular property. As an example, suppose that at Northwind
Traders, all orders are entered electronically, directly into the system, so the OrderDate
property of the Order entity should never be in the past. You cannot express this
condition using the RangeAttribute because today's date is not a constant and cannot
be specified in an attribute. Here is how you could accomplish this task with the
CustomValidationAttribute :
 
 
Search WWH ::




Custom Search