Print Page | Close Window

How to trigger validation on a property in the developer partial class?

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=2361
Printed Date: 03-Jul-2026 at 9:16am


Topic: How to trigger validation on a property in the developer partial class?
Posted By: as123
Subject: How to trigger validation on a property in the developer partial class?
Date Posted: 09-Dec-2010 at 1:57pm
         How to trigger validation on a new property (based on a property on the entity model) in the developer class respresenting a DF entity?
 
          For example, how to trigger validation on the "NameCustom" property in the following scenario? Using AddTrigger("NameCustom") does not fire the delegate validation function after hitting the setter for NameCustom (checked using the debugger).  In the verifier collection (during debug), I do see a verifier with TriggerItem set to "NameCustom".
 
       If I set the trigger for "Name", it works fine. Do I need to decorate the developer class or NameCustom property with any attributes so that the VerifierEngine can detect the trigger set for NameCustom?
 
       I could not find any documentation or sample in this regard.
 
          Please let me know if I need to post this as a seperate issue.
 
Thanks
 
 
        /* ...IB.Designer.cs */
        public partial class Employee : Entity
        {
          ....      
          public override string Name 
              get { return PropertyMetadata.Name.GetValue(this); }
              set { PropertyMetadata.Name.SetValue(this, value); }
              }
 
             /* The developer class representing the Employee business object */
 
        public partial class Employee : Entity
        {
          ....      
          public override string NameCustom
              get { /* custom getter based on Name property */ ... ; }
              set { /* custom setter based on Name property */ ... ; }
        }
 



Replies:
Posted By: DenisK
Date Posted: 10-Dec-2010 at 10:24am
as123;

How are you setting your setter on your NameCustom? If you're setting the setter by using the Name property, the verifier for the Name property should have fired. Is that what you want it to do?

In general, to add a custom property to your partial entity class, you need to decorate the getter and setter of the NameCustom with the PropertyMetaData class as follows:

public partial class PropertyMetadata {
      public static readonly DataEntityProperty<Employee, string> NameCustom =
        new DataEntityProperty<Employee, string>("NameCustom", true, false, ConcurrencyStrategy.None, false, false);
}

public override string NameCustom 
              get { return PropertyMetadata.NameCustom.GetValue(this); }
              set { PropertyMetadata.NameCustom.SetValue(this, value); }

However, it is generally not recommended for a custom property to have a public setter as its function is normally a display only. Therefore, a custom property should always be an extension of an existing property of an entity model and let the validation of that custom property be fired on the existing property.

I hope this make sense.


Posted By: as123
Date Posted: 10-Dec-2010 at 2:42pm

                  I am setting the getter and setter for 'NameCustom' and property metadata as you have suggested .  If I specify Verifiers to be triggered by 'Name', the specified verifiers are executed, but the  error notification (INotifyDataErrorInfo) does not occur on the silverlight client, if the binding is to the property 'NameCustom'. 

                  So, in my scenario, is it possible to bind to a property (NameCustom) in the develop class, specify validations to be trigged based on the corresponding Designer class property (Name), and have the errors notified on the client after executing the validators?  The reason why I want to bind to NameCustom (public) is because I want to do some custom logic in the setter (of course, will do setting on Name after some custom logic) when UI is bound to NameCustom. There are other situations where I want to directly access the public Name property to bypass the custom logic.

                Is it possible to attach verifiers to properties like NameCustom? Or, should verifiers be always attached to properties on DevForce Entities? Please clarify.

               Thanks.

 



Posted By: DenisK
Date Posted: 13-Dec-2010 at 11:38am
as123;

Yes, it's possible. Attached is a ValidationSample.zip. I've added a custom property to the Order entity named "Total" and bind that to both a DataGrid and Textbox. The verifier fires an error if a user enters a value that is not between 0 and 100.

http://ideablade.com/friends/ValidationSample.zip - http://ideablade.com/friends/ValidationSample.zip


Posted By: as123
Date Posted: 13-Dec-2010 at 1:50pm
    Thanks for the sample app. I was not setting the metadata properly before.



Print Page | Close Window