Print Page | Close Window

Custom Verifier Attribute?

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2747
Printed Date: 26-Jan-2026 at 9:13am


Topic: Custom Verifier Attribute?
Posted By: bap1
Subject: Custom Verifier Attribute?
Date Posted: 07-Jun-2011 at 1:39pm
I've created a custom verifer.  I'm now trying to apply it to the FirstName property of the Devforce entity using a buddy class but it doesn't like the attribute.  Can someone help me here?

[MetadataType(typeof(DirectoryItemMetaData))]
    public partial class DirectoryItem
    {
        public class DirectoryItemMetaData
        {
            [DirectoryItem_NameVerifier()]
            public static string FirstName;


            public static Verifier DirectoryItem_NameVerifier()
            {
                string Description = "FirstName and LastName is required for individuals.";
                DelegateVerifier<DirectoryItem> v = new DelegateVerifier<DirectoryItem>(Description, DirectoryNameCondition);
                return v;
            }

            private static VerifierResult DirectoryNameCondition(DirectoryItem dirItem, TriggerContext triggerContext, VerifierContext verifierContext)
            {
                if (triggerContext != null && triggerContext.Timing == TriggerTiming.BeforeSet)
                {
                    throw new VerifierException("DirectoryNameValidation is not implemented for Preset.");
                }

                if (!dirItem.IsCompany)
                    return new VerifierResult(!string.IsNullOrEmpty(dirItem.FirstName) && !string.IsNullOrEmpty(dirItem.LastName));
                else
                    return new VerifierResult(true);
            }
        }
    }




Replies:
Posted By: bap1
Date Posted: 07-Jun-2011 at 6:31pm

I found out how to add triggers on specific properties below.  Is this the only way how to add a custom verifier?  Can I not add a VerifyAttribute on the property of my buddy class?

public static Verifier DirectoryItem_NameVerifier()
            {
                string Description = "FirstName and LastName is required for individuals.";
                DelegateVerifier<DirectoryItem> v = new DelegateVerifier<DirectoryItem>(Description, DirectoryNameCondition);
               
                v.AddTriggers(DirectoryItem.PropertyMetadata.FirstName.Name, DirectoryItem.PropertyMetadata.LastName.Name);

                v.VerifierOptions.ExecutionModes = VerifierExecutionModes.InstanceAndOnAfterSetTriggers;
                return v;
            }



Posted By: DenisK
Date Posted: 09-Jun-2011 at 8:14am
Hi bap1;

To create your own custom attribute, you can inherit from one of our pre-defined verifier attribute classes. Below is an example of inheriting from a PropertyValueVerifierAttribute class.

//The NullEntityVerifier

    public class NullEntityVerifier : PropertyValueVerifier {
      public NullEntityVerifier(Type entityType, String propertyName,
        String displayName = null, bool? shouldTreatEmptyStringAsNull = null)
        : base(new PropertyValueVerifierArgs(entityType, propertyName, false,
          displayName, shouldTreatEmptyStringAsNull)) {
      }

      public NullEntityVerifier(PropertyValueVerifierArgs args) : base(args) { }

      protected override VerifierResult VerifyValue(object itemToVerify, object valueToVerify, 
        TriggerContext triggerContext, VerifierContext verifierContext) {

        var entity = (Entity)valueToVerify;
        var result = !entity.EntityAspect.IsNullEntity;
        return new VerifierResult(result);
      }
    }

//The NullEntityVerifierAttribute

    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false)]
    public class NullEntityVerifierAttribute : PropertyValueVerifierAttribute {
      protected override Verifier BuildVerifierCore(Type pType, String propertyName) {
        return new NullEntityVerifier(
          new PropertyValueVerifierArgs(pType, propertyName, false, DisplayName, 
            GetShouldTreatEmptyStringAsNull())
          );
      }
    }

//The buddy class

    public class EmployeeMetadata {

      [NullEntityVerifier]
      public static Employee Manager;
    }

//The generated code

    #region Manager property

    /// <summary>Gets or sets the Manager. </summary>
    [DomainModel.Employee.NullEntityVerifier]
    [Bindable(false)]
    [Display(Name="Manager", AutoGenerateField=false)]
    [DataMember]
    [IbEm.RelationProperty("FK_Employee_Employee", IbEm.QueryDirection.ToRole2)]
    public Employee Manager {
      get { return PropertyMetadata.Manager.GetValue(this); }
      set { PropertyMetadata.Manager.SetValue(this, value); }
    }
    #endregion Manager property



Print Page | Close Window