Default code generation of your entity model will automatically add some validation attributes to your entity properties. You can also have code generation generate custom validation attributes onto your entity properties using a metadata "buddy" class.
These following verifier attributes are automatically added to the code that DevForce generates based on the property attributes defined in the Entity Framework Designer:
These attributes appear as follows in the generated code:
C# | [IbVal.RequiredValueVerifier( ErrorMessageResourceName="Employee_Id")] public long Id { ... } [IbVal.StringLengthVerifier(MaxValue=30, IsRequired=true, ErrorMessageResourceName="Employee_LastName")] public string LastName { ... } |
VB | <IbVal.RequiredValueVerifier(ErrorMessageResourceName:="Employee_Id")> _ Public ReadOnly Property Id() As Long ... End Property <IbVal.StringLengthVerifier(MaxValue:=30, IsRequired:=True, ErrorMessageResourceName:="Employee_LastName")> _ Public ReadOnly Property LastName() As String |
To add additional verifiers to your entities, you can programmatically add verifiers, as we've seen earlier. But you have another option too: you can use a "buddy" class containing metadata to be applied to entity properties. In the buddy class you define additional validation attributes for your entity properties, and "link" the buddy class to the entity class by placing the MetadataType attribute on the class. Here's an example to make this clearer:
C# | [MetadataType(typeof(EmployeeMetadata))] public partial class Employee { ... } public class EmployeeMetadata { [RequiredValueVerifier()] public static string City; [DateTimeRangeVerifier(MinValue="1/1/2010", MaxValue="12/31/2020")] public static DateTime? HireDate; [RegexVerifier("Home phone", "USPhone")] public static string HomePhone; } |
VB | <MetadataType(GetType(EmployeeMetadata))> _ Public Partial Class Employee End Class Public Class EmployeeMetadata <RequiredValueVerifier> _ Public Shared City As String <DateTimeRangeVerifier(MinValue := "1/1/2010", MaxValue := "12/31/2020")> _ Public Shared HireDate As System.Nullable(Of DateTime) <RegexVerifier("Home phone", "USPhone")> _ Public Shared HomePhone As String End Class |
The verifiers may be a bit contrived here (unless you really do want to hire only US employees in this decade), but should give you an idea of what you can do. Any of the DevForce validation attributes, such as any of the range verifiers Int32RangeVerifierAttribute, Int64RangeVerifierAttribute, DecimalRangeVerifierAttribute, DoubleRangeVerifierAttribute, DateTimeRangeVerifierAttribute, etc.) or RegexVerifierAttribute, or any custom validation attributes you write which extend a DevForce verifier attribute, can be used.
There are a few rules to follow, to ensure that your attributes are found: