Print Page | Close Window

Navigation Property Verification

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=2540
Printed Date: 29-Jul-2026 at 4:58pm


Topic: Navigation Property Verification
Posted By: smi-mark
Subject: Navigation Property Verification
Date Posted: 02-Mar-2011 at 5:09pm
Is there an inbuilt way for making sure navigation properties are not null? I've done this, but i wondered if there was perhaps a better way.

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

        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);
        }

    }

Then I added this into my provider. I attempted to use the buddy class method to add the verifier but it didn't seem to work, so at this moment in time I have it in my VerifierProvider.







Replies:
Posted By: DenisK
Date Posted: 04-Mar-2011 at 10:50am
Hi smi-mark;

We don't have an in-built way of doing this. So your verifier solution seems to be perfectly reasonable. 

Let me check on the buddy class method and get back to you.


Posted By: DenisK
Date Posted: 07-Mar-2011 at 12:29pm
Hi smi-mark;

I've tested the buddy class method of adding the verifier and it seems to work for me. Perhaps I misunderstood something specific to your use case?

Here's my test.

//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


Posted By: smi-mark
Date Posted: 07-Mar-2011 at 9:35pm
Hi Denis,

I think I tested it with the RequiredValueVerifier at first, and since it didn't generate anything for the navigation property, I assumed it didn't work. I'll try it with my null verifier tomorrow.

Thanks for looking into it.



Posted By: DenisK
Date Posted: 08-Mar-2011 at 11:18am
Hi smi-mark;

I just tested it with the RequiredValueVerifier. And although code generation picked it up, it won't work with navigational property since RequiredValueVerifier deals with null values and a "null" navigational property is not exactly null but rather a "detached/null entity".



Print Page | Close Window