New Posts New Posts RSS Feed: Navigation Property Verification
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Navigation Property Verification

 Post Reply Post Reply
Author
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 307
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post Topic: Navigation Property Verification
    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.




Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 667
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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.


Edited by DenisK - 04-Mar-2011 at 10:51am
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 667
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 307
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post 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.

Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


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

Forum Jump Forum Permissions View Drop Down