The easiest point of entry to DevForce validation is through the DevForce extensions to the Entity Data Model Designer. The idea is that we can get a certain set of automatic validation code generated for us simply as a side effect of setting up our domain model. This page details how to use Visual Studio Entity Data Model Designer to automatically generate validation attributes.
Here we’ll show you the results of the three different values for Validation Attribute Mode:
Generate DevForceVerification Attributes
Here is the FirstName property of an Employee object as generated with the settings shown above:
C# | #region FirstName property /// <summary>Gets or sets the FirstName. </summary> [Bindable(true, BindingDirection.TwoWay)] [Editable(true)] [Display(Name="FirstName", AutoGenerateField=true)] [IbVal.StringLengthVerifier(MaxValue=30, IsRequired=true, ErrorMessageResourceName="Employee_FirstName")] [DataMember] public string FirstName { get { return PropertyMetadata.FirstName.GetValue(this); } set { PropertyMetadata.FirstName.SetValue(this, value); } } #endregion FirstName property |
VB | #Region "FirstName property" ''' <summary>Gets or sets the FirstName. </summary> <Bindable(True, BindingDirection.TwoWay), Editable(True), _ Display(Name:="FirstName", AutoGenerateField:=True), _ IbVal.StringLengthVerifier(MaxValue:=30, IsRequired:=True, _ ErrorMessageResourceName:="Employee_FirstName"), DataMember()> _ Public Property FirstName() As String Get Return PropertyMetadata.FirstName.GetValue(Me) End Get Set(ByVal value As String) PropertyMetadata.FirstName.SetValue(Me, value) End Set End Property #End Region ' FirstName property |
IbVal is an alias for the IdeaBlade.Validation namespace, defined at the top of the code file. The IbVal.StringLengthVerifier sets a maximum length on the (text) value, and its IsRequired argument declares the property non-nullable.
Generate .NET validation attributes
Here is the generated code that the above settings in the EDM designer:
C# | #region FirstName property /// <summary>Gets or sets the FirstName. </summary> [Bindable(true, BindingDirection.TwoWay)] [Editable(true)] [Display(Name = "FirstName", AutoGenerateField = true)] [Required()] [StringLength(30)] [DataMember] public string FirstName { get { return PropertyMetadata.FirstName.GetValue(this); } set { PropertyMetadata.FirstName.SetValue(this, value); } } #endregion FirstName property |
VB | #Region "FirstName property" ''' <summary>Gets or sets the FirstName. </summary> <Bindable(True, BindingDirection.TwoWay), Editable(True), _ Display(Name:="FirstName", AutoGenerateField:=True), _ Required(), _ StringLength(30), _ DataMember()> _ Public Property FirstName() As String Get Return PropertyMetadata.FirstName.GetValue(Me) End Get Set(ByVal value As String) PropertyMetadata.FirstName.SetValue(Me, value) End Set End Property #End Region ' FirstName property |
This time the non-nullability (i.e., Required) and string length constraints are specified using the .NET validation attributes.
Generate No Verification or Validation Attributes
This setting results in the absence of validation-related attributes of any sort:
C# | #region FirstName property /// <summary>Gets or sets the FirstName. </summary> [Bindable(true, BindingDirection.TwoWay)] [Editable(true)] [Display(Name = "FirstName", AutoGenerateField = true)] [DataMember] public string FirstName { get { return PropertyMetadata.FirstName.GetValue(this); } set { PropertyMetadata.FirstName.SetValue(this, value); } } #endregion FirstName property |
VB | #Region "FirstName property" ''' <summary>Gets or sets the FirstName. </summary> <Bindable(True, BindingDirection.TwoWay), Editable(True), _ Display(Name:="FirstName", AutoGenerateField:=True), DataMember()> _ Public Property FirstName() As String Get Return PropertyMetadata.FirstName.GetValue(Me) End Get Set(ByVal value As String) PropertyMetadata.FirstName.SetValue(Me, value) End Set End Property #End Region ' FirstName property |