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