New Posts New Posts RSS Feed: Custom Verifier Attribute?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Custom Verifier Attribute?

 Post Reply Post Reply
Author
bap1 View Drop Down
Newbie
Newbie


Joined: 08-Mar-2011
Posts: 9
Post Options Post Options   Quote bap1 Quote  Post ReplyReply Direct Link To This Post Topic: Custom Verifier Attribute?
    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);
            }
        }
    }

Back to Top
bap1 View Drop Down
Newbie
Newbie


Joined: 08-Mar-2011
Posts: 9
Post Options Post Options   Quote bap1 Quote  Post ReplyReply Direct Link To This Post 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;
            }

Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down