Print Page | Close Window

How to Instantiate Verifiers when creating new record

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2009
Forum Discription: For .NET 3.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1546
Printed Date: 08-Apr-2025 at 8:35pm


Topic: How to Instantiate Verifiers when creating new record
Posted By: pchavan2009
Subject: How to Instantiate Verifiers when creating new record
Date Posted: 06-Nov-2009 at 2:58am
I am not able to instantiate verifiers when creating the new record

public static IVRMenuOptions Create(EntityManager manager, short optionId)

{

EntityValues values = new EntityValues();

values.Add(IVRMenuOptions.IvrOptionEntityProperty, optionId);

values.Add(IVRMenuOptions.IvrMenuType_InternalEntityProperty, 0);

return EntityFactory<IVRMenuOptions>.Create(manager, values);

}

Verifiers are applied on IvrMenuType_InternalEntityProperty as shown below:

private static Verifier CreateIVRMenuTypeVerifier()

{

DelegateVerifier<IVRMenuOptions> verifier = new DelegateVerifier<IVRMenuOptions>(string.Empty, VerifyIVRMenuTypeValue);

verifier.ExecutionModes = VerifierExecutionModes.InstanceAndOnPostsetTriggers;

verifier.AddTrigger(IVRMenuOptions.PathFor(s => s.IvrMenuType_Internal));

return verifier;

}




Replies:
Posted By: GregD
Date Posted: 09-Nov-2009 at 4:48pm
PChavan:

Sorry, I'm not quite understanding what you're trying to do. Why do you want to instantiate verifiers when you create a new record? Do you mean that verifiers are not kicking in properly when you initialize properties that have verifiers during the entity creation process?


Posted By: pchavan2009
Date Posted: 10-Nov-2009 at 11:03pm
Verifiers Triggers are called only when record is updated.
We add triggers to a column of a table in the Model. Those triggers execute the respective functions.
When I am creating a records whose table name is IVRMenuOptions with primary key as IvrOption(int), I am assigning a default value as 0.
When I add a default value as 10 during creation it should raise the trigger.
 
Ok Is it possible to raise a trigger manually.
Triggers can be raise when u change the value in the code.
I want to raise the triggers defined in model manually without changing the data in the row. So that it should return the VerifierResult for that column on the record


Posted By: GregD
Date Posted: 11-Nov-2009 at 10:35am
Yes.  Call the Execute method (13 overloads) on the entity's VerifierEngine:

   anEntity.EntityAspect.VerifierEngine.Execute(...);


However, your property value verifiers should run inside your Create method. For example, I just added statements inside my Customer.Create() method that set CompanyName to invalid values. (In the backing database, NorthwindIB, CompanyName is marked as non-nullable and is limited to 40 characters, so corresponding attributed verifiers were generated by the Object Mapper.)

    public static Customer Create(EntityManager manager,
      string companyName) {

      Customer aCustomer = manager.CreateEntity<Customer>();
      aCustomer.CustomerID=System.Guid.NewGuid();
      aCustomer.CompanyName = null;  // triggers verification exception - required value
      string pad = "01234567890123456789012345678901234567890123456789";
      aCustomer.CompanyName = companyName + pad;  // if get here, will also trigger a verification exception - string length
      aCustomer.CompanyName = companyName;
      aCustomer.EntityAspect.AddToManager();
      return aCustomer;
    }

Are you sure you have the generation of verifiers turned on in the Object Mapper?





Posted By: pchavan2009
Date Posted: 11-Nov-2009 at 9:21pm

Yes the generation of verifiers is turned on in the Object mapper?

I am using wpf datagrid where I am binding a collection of objects of Customer.
I am editing a record where I have updated the CustomerId(Primary key) to value 5 which already exist. I get an exception as VerifierResultException.
 
After the exception is raised,I want to catch the exception in my model.
My Customer class has ErrorCollection dictionary, where I store all the verfier result for all the columns. When an exception is raised I want a function to be executed where I can call the anEntity.EntityAspect.VerifierEngine.Execute(...); for each column to find the respective respective VerifierResult.
 
Is it possible to raise an event which will be called after all the exceptions are raised so that I can store them in the ErrorCollection Dictionary and display the list of error messages in the tooltip of the datagrid row?


Posted By: pchavan2009
Date Posted: 13-Nov-2009 at 1:41am
public static Customer Create(EntityManager manager,
      string companyName) {

      Customer aCustomer = manager.CreateEntity<Customer>();
      aCustomer.CustomerID=System.Guid.NewGuid();
      aCustomer.CompanyName = null;  // triggers verification exception - required value
      string pad = "01234567890123456789012345678901234567890123456789";
      aCustomer.CompanyName = companyName + pad;  // if get here, will also trigger a verification exception - string length
      aCustomer.CompanyName = companyName;
      aCustomer.EntityAspect.AddToManager();
      return aCustomer;
    }
The preceding functions as u said triggers the respective verifiers and raise a "IdeaBlade.Validation.VerifierResultException" exception due to which the remaining code does not get executed.
 
In my case I want whole code to get executed and  there should be particular function where I can check for each and every column of the row for the VerifierResult using the Execute function to check for the errors, so that I can store them in particular string and display them in my datagrid row


Posted By: GregD
Date Posted: 13-Nov-2009 at 11:29am

The ExecutionMode of the generated verifiers defaults to PreSet, which means it's their job to block an invalid value from making it into the business object. Since they're doing that, they have to throw an exception to let you know that what you tried didn't work.

If you change the ExecutionMode to PostSet (which you can do in the Object Mapper), then the verifier won't run until after the value is pushed into the business object (and won't throw an exception). Then you can order an instance verification to get the complete picture. 
 
In the example below, CompanyName has a StringLengthVerifier that prevents values > 40 characters in length. I changed its execution mode (called "Verification Setter Mode" in the Object Mapper) to PostSet. As you can see, the entire Create() method runs, and before it finishes it orders an instance verification of the entire object. I'm just displaying all the VerifierResults from that in the Console window:
 



Print Page | Close Window