Problems upgrading validator classes

Posts   
 
    
hplloyd
User
Posts: 191
Joined: 29-Oct-2004
# Posted on: 06-Sep-2006 23:50:58   

Hi,

I am trying to upgrade my v1.2005 code to the new 2.0 code, but am having a few problems mainly to do with validation.

In v1.2005 I generate a validator class for each entity as follows (based on the original manager classes that were made available). The following code is for my Company Entity


public sealed class CompanyValidator : IEntityValidator
    {
    
        private int connectionID=0; // used to pass in the connectionID {default=0}
        
        /// <summary>
        /// Constructor
        /// </summary>
        public CompanyValidator(int connID)
        {
            connectionID=connID;
        }
        
        /// <summary>
        /// Entity validation rules for 'Company'
        /// </summary>
        public bool Validate(object containingEntity)
        {
            CompanyEntity myEntity = (CompanyEntity)containingEntity;
            
            #region Validation Code
            // __LLBLGENPRO_USER_CODE_REGION_START Validation
            CompanyEntity myUniqueTest = CompanyManager.FetchUsingUniqueConstraintDisplay( myEntity.Display,connectionID );
            if (myUniqueTest!=null && myUniqueTest.CompanyID!=myEntity.CompanyID)
            {
                throw new ORMEntityValidationException( "The 'Show As' column must be unique. If you are adding a new company, try adding contact details as well as a company name. If you are modifying a company, you can edit the 'Show As' value directly - Save cancelled.",containingEntity);
            }
            // __LLBLGENPRO_USER_CODE_REGION_END
            #endregion

            
        // valid
        return true;
        }

Then one I have fetched a company from the database into a CompanyEntity, I use the code


myEntity.EntityValidatorToUse = new CompanyValidator(0);

to ensure that the entity uses the validator I want.

Finally, I set all the properties of myEntity and save the entity using

if (myEntity.Validate())
                    {
                        CompanyManager.MGRUpdate(myEntity,0);
                    }

Which validates the code, and throws an exception is there is an error, or saves the record if the validation succeeds.

It all seems very simple to me, however I can see that the validation has changed in v2.0. How do I perform the same functionality using the 2.0 templates (N.B. I am still only generating .NET 1.1 code at thi stage)?

Many thanks in advance

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 07-Sep-2006 04:04:48   

A section of the manual titled Migrating your code explains the processes in depth. It is good to go through this so that you can understand what the new implementation does. It may help with your specific implementation. For starters IEntityValidator interface has been removed, its functionality is merged with IValidator and implemented in ValidatorBase. All entities now have a single validator for both field and entity validation. Your existing IEntityValidator implementing classes should be converted to classes which implement the new IValidator interface

hplloyd
User
Posts: 191
Joined: 29-Oct-2004
# Posted on: 08-Sep-2006 16:33:04   

Thanks, I have worked through the documentation and I think it will all work out OK..