Custom Validation

Posts   
 
    
Tolyan1
User
Posts: 2
Joined: 03-Nov-2008
# Posted on: 03-Nov-2008 15:56:25   

Hi,

I have an entity class some properties of which have to be encrypted before they saved to the database and decrypted after fetched from the database.

I also need to perform custom validation before I can save this entity to the database.

To accomplish this, I created custom validator class and overriden ValidateEntityBeforeSave() method, i.e.


public override void ValidateEntityBeforeSave(IEntityCore involvedEntity)
        {
            BankDetailEntity toValidate = (BankDetailEntity)involvedEntity;

            ValidationErrors.Clear();

            switch ((BankDetailTypes)toValidate.BankDetailType.Value)
            {
                case BankDetailTypes.UK:
                    {
                        if (string.IsNullOrEmpty(toValidate.AccountNumber) || toValidate.AccountNumber.Trim().Length < 6)
                            ValidationErrors.Add("The account number you have provided is invalid");

                        if (string.IsNullOrEmpty(toValidate.SortCode) || toValidate.SortCode.Trim().Length < 6)
                            ValidationErrors.Add("The sort code you have provided is invalid");
                        
                         break;
                    }
                case BankDetailTypes.International:
                    {
                        if (string.IsNullOrEmpty(toValidate.BankName) || toValidate.BankName.Trim().Length == 0)
                            ValidationErrors.Add("Please provide the name of your bank");
                        break;
                    }
            }

            if (ValidationErrors.Count > 0)
            {
                throw new ORMEntityValidationException(ValidationErrors.ToString(), toValidate);
            }

            base.ValidateEntityBeforeSave(involvedEntity);
        }
    }

Also, in entity class I overriden OnSave() and OnFetchComplete() methods to encrypt/decrypt data before saving and after loading:


protected override void OnSave()
{   
    EncryptData();
    base.OnSave();
}

protected override void OnFetchComplete()
{   
    DecryptData();
    base.OnFetchComplete();
}

The problem I have is that OnSave() is called first so data is encrypted first and then Validation occurs and fails as it checks against encrypted data.

Is there any other way to make changes to entity fields after validation occurs but before saving to the database?

Kind regards, Anatoliy

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 03-Nov-2008 16:03:23   

Call the Encrypt and Decrypt methods from the validator's ValidateEntityBeforeSave and ValidateEntityAfterLoad.

So for example ValidateEntityBeforeSave should validate the entity before saving and if valid it should decryp it.

Tolyan1
User
Posts: 2
Joined: 03-Nov-2008
# Posted on: 03-Nov-2008 16:12:37   

Hi Walaa,

Thanks for a quick reply.

Your solution will work but I guess it's a bit weird to put any business logic into validator rather than just checking if the data is correct.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 04-Nov-2008 06:41:10   

I have an entity class some properties of which have to be encrypted before they saved to the database and decrypted after fetched from the database.

What about encryption in a TypeConverter? You can write a typeconverter which converts string to string but encrypts / decrypts them. As the type converter is compiled with the entity's persistence info, this is transparent: setting the field(s) which have the converter set (you can set them per entity/typed view field) to a string will encrypt them when the field is updated, and decrypt them when the field is read from the db. You can use your custom encrypt/decrypt assembly here, but your assembly must do just that (encrypt/decrypt) not save. This way is more transparent IMHO.

David Elizondo | LLBLGen Support Team