Save, Refetch and Validations

Posts   
 
    
Fabrice
User
Posts: 180
Joined: 25-May-2004
# Posted on: 21-Oct-2004 11:36:35   

Hi

I've a problem with validations I'm doing cross object validations, for example I've an Employee and an EmployeeTl entity, the link is done by EmployeeTl.EId = Employee.EId.

Now, I've overrided the FetchEntity and FetchEntityCollection to be sure to always have an EmployeeTl fetched with an Employee. So that in my validations I can check data are in a consistent state between the 2 objects.

The problem is : In the EmployeeValidator or EmployeeTlValidator when I check some data from the other object I can get a OutOfSync exception because the other object is allready saved. For example from an EmployeeTlValidator I try to access to Employee.Anyfield but the Employee is allready validates and then saved, so I got an OutOfSync exception

Ok my first reaction, I put the refetch after save to true. But ... this is the way it should work : 1) Employee is validate 2) Employee is saved 3) Employee is refetched 4) EmployeeTl is validated 3) EmployeeTl is refetched 4) EmployeeTl is refetched

but in point 3 --> bing exception. My overrided fetch method don't find any EmployeeTl related to the Employee, because the EmployeeTl is not yet saved !!

So my questions : 1) Is there any way to make ALL validations (on ALL objects) before starting persistance actions ? The OutOfSync problem will be there for all validations between objects, it's annoying and refetch only for validations is not very optimized ... 2) Is it a way to know if we are in a "normal" fetch or in a refetch after save ?

Thanks you simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 21-Oct-2004 12:20:45   

Fabrice wrote:

So my questions : 1) Is there any way to make ALL validations (on ALL objects) before starting persistance actions ? The OutOfSync problem will be there for all validations between objects, it's annoying and refetch only for validations is not very optimized ...

Sure, call recursively (i.e.: traverse the graph) Validate() on all entities before a save. However I don't see why you need your overrided fetch routine in the refetch action. The validate is done at a time when it has to work, i.e.: when there should be (in your situation) 2 objects related to eachother. So the validation object should first check if the related object is there, if not, the validation should fail.

2) Is it a way to know if we are in a "normal" fetch or in a refetch after save ? Thanks you simple_smile

Well, as the save is recursive, you can override SaveEntity() and store the recurse flag internally, then check that flag in your FetchEntity() override. Clear the flag in an override of OnSaveEntityComplete(), it will be set again with the next call to SaveEntity(). simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Fabrice
User
Posts: 180
Joined: 25-May-2004
# Posted on: 21-Oct-2004 13:21:59   

The problem is that sometime the related object is allready saved, so I got an OutOfSyncException when I try to access its properties.

How can I "traverse the graph" ? There is a way to retrieve all objects related to the current object ? Thank you !

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 21-Oct-2004 13:28:29   

Fabrice wrote:

The problem is that sometime the related object is allready saved, so I got an OutOfSyncException when I try to access its properties.

Not if you refetch it, which you have to if you want to access its properties after the save.

How can I "traverse the graph" ? There is a way to retrieve all objects related to the current object ? Thank you !

Using a call to entity.GetDependentRelatedEntities(), entity.GetDependingRelatedEntities() and entity.GetMemberEntityCollections(). See the reference manual for details.

Frans Bouma | Lead developer LLBLGen Pro
Fabrice
User
Posts: 180
Joined: 25-May-2004
# Posted on: 21-Oct-2004 13:56:25   
 Not if you refetch it, which you have to if you want to access its properties after the save. 

Yes it was why I put my second question simple_smile But I prefer to make all validations at once so that I can return all validations error to the client. Now I can only return error thrown by the entity being saved because of the exception.