Validation within a transaction

Posts   
 
    
hplloyd
User
Posts: 191
Joined: 29-Oct-2004
# Posted on: 21-Apr-2005 12:19:17   

I am using a validator class for each of my entities that implements the IValidator interface.

Sometimes I want to use transactions to save an entity because on saving an entity other "post save" actions need to be performed (e.g. adding other records to other tables). These other actions naturally fire off their own validation routines etc.

My issue arrises when a validation routine needs to access the database to check (for example) if the entity name is unique. This causes the system to hang because the validation routine is unable to make use of the adapter that is being used for the transaction....

How can I pass the DataAdapter used for the transaction to the validation routine so that the validation becomes part of the transaction.

Many thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 21-Apr-2005 12:58:08   

you can't, as an entity doesn't know about any adapter. Either use a readuncommitted transaction or do your validation up front (preferred, as a transaction should be as short as possible, thus pack all write actions together).

Frans Bouma | Lead developer LLBLGen Pro
hplloyd
User
Posts: 191
Joined: 29-Oct-2004
# Posted on: 21-Apr-2005 13:22:00   

So,

Are you saying that to save an entity I should do something like this ( I am using manager classes for this)

EmployeeEntity myEntity = EmployeeManager.CreateNew();
myEntity.ValidatorToUse = new EmployeeValidator();

if (myEntity.Validate())
{
myEntity.EntityValidatorToUse=null;
EmployeeManager.SaveWithTransaction(myEntity)
}

SaveWithTransaction will save this entity and then process any other entity saves (e.g. log file writes) that are triggered by saving this entity.

These log file writes within the transaction will be made without setting their respective EntityValidatorToUse property..... So I must make sure manually that any saves I do within the transaction would pass the validation for the respective entity.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 21-Apr-2005 16:17:39   

It's a bit of a pain, but the issue is that you use the IEntityValidator interface to implement an entity which validates an entity using data from outside the entity. (multi-entity validation). This is not the purpose of the interface, as its purpose is to validate the entity data itself by using in-entity multi-field validation.

In fact, the entity validation like you want it to be, is better off outside the entity.

But that aside, you could add a property to your entity validator class which accepts an adapter. you then cast the EntityValidatorToUse property contents to the validator class and pass in the adapter in use. Not very nice, but doable.

Frans Bouma | Lead developer LLBLGen Pro