Pre-post save methods for adapter

Posts   
 
    
stef
User
Posts: 28
Joined: 14-Jun-2005
# Posted on: 07-Sep-2005 07:29:21   

Hi,

I'm hoping this is possible... is there a standard way to execute a code block in an entity as a pre or post save event in the adapter model?

Given the entity has no reference to the adapter, are there standard pre/post methods that the adapter calls on an entity during an attempt to persist an object that can be customised?

Thanks, Stef

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 07-Sep-2005 16:15:11   

Adapter has in the DataAccessAdapter class OnSaveEntity() and OnSaveEntityComplete methods, which you can override in a derived class of DataAccessAdapter.

For what particular situation do you want to use these methods?

Frans Bouma | Lead developer LLBLGen Pro
stef
User
Posts: 28
Joined: 14-Jun-2005
# Posted on: 07-Sep-2005 21:48:07   

Thanks Otis, The need for this arose because I'd like to use the role object pattern to capture the various roles that a person can belong to. I can't use inheritance as the person may belong to serveral roles e.g. they could be a client and a vendor at the same time. There are also rules defining the compatibilty of roles, for example it is not possible to be a client and a prospect at the same time.

The roles are subobjects of the root person object and implemented using LLBLGens relationships however I'd prefer to provide an interface that doesn't require the user to test for the presence of each object. As the roles are expanded the code gets rather messy. By placing the sub entities into a roles collection I can then perform actions on the collection rather than having to know the subobject structure.

All this leads to the need to update the subobjects from the collection, if necessary, when the root person object is persisted and so I'd like to execute some additional before the adapter saves the object graph.

Cheers, Stef

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 08-Sep-2005 11:01:36   

I think you can solve this by creating a IEntityValidator implementation and set the entity's EntityValidatorToUse property to an instance of that validator. Then simply call Validate() and your validator is called.

Frans Bouma | Lead developer LLBLGen Pro
stef
User
Posts: 28
Joined: 14-Jun-2005
# Posted on: 08-Sep-2005 12:28:04   

Otis wrote:

I think you can solve this by creating a IEntityValidator implementation and set the entity's EntityValidatorToUse property to an instance of that validator. Then simply call Validate() and your validator is called.

Thanks, I'll have a look at this since it is a little neater than my current approach... given the methods you mentioned previously in the DataAccessAdapter I created a simple interface that an entity could implement.

public interface ISaveHooks { void PreSaveHook(); void PostSaveHook(); }

An entity implements this interface is there is additional code to execute before or after the save. The DataAdapter methods mentioned are extended to check whether or not the entityToSave implements this interface and if so the relevant method is called.

Thanks, Stef

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 08-Sep-2005 22:11:37   

That could work as well indeed. You can alter the template for the entities to implement this interface or add the interface to the user code region for additional interfaces simple_smile

Frans Bouma | Lead developer LLBLGen Pro