Executing custom code during entity.save() method call

Posts   
 
    
Flynn
User
Posts: 17
Joined: 28-Jan-2009
# Posted on: 23-Mar-2009 16:58:17   

When a subset of my entities are created or deleted, I need to add or delete a record in an additional database table to which no foreign key relationship exists (this record is used to implement a permissioning framework used by my organization).

I've looked at the user code regions and partial classes as an option, but I'm unsure as how to best leverage them for what I want. The Save method that I'm calling on the entity is actually implemented by the EntityBase base class, so it's not part of the entity's generated code. And the parameterless version that I am calling isn't declared as virtual, so I can't override it

Is there a recommended best practice for extending the entities in question to execute, within the same transaction, some custom code database code after performing their normal CRUD activity?

Do I have to use templates for this?

x3mka
User
Posts: 6
Joined: 23-Mar-2009
# Posted on: 23-Mar-2009 19:25:50   

Depending on your requirements you can just use triggers on your db.

Flynn
User
Posts: 17
Joined: 28-Jan-2009
# Posted on: 23-Mar-2009 19:44:32   

Agreed. And I appreciate the suggestion. But I'm also trying to understand the best way to make use of LLBLGen.

So, assuming we confine the logic to the business layer and don't make use of database triggers or custom stored procedures, is there some way to accomplish what I asked?

x3mka
User
Posts: 6
Joined: 23-Mar-2009
# Posted on: 23-Mar-2009 20:07:56   

I understand.

I'd suggest the following:

  1. To dig in the entity base classes to see if they have some events and delegates. I'm not a LLBLGen guru so I cannot tell for sure. I you don't find them you can try to add such code in partial classes or modify a bit generation templates.

  2. The second idea is to look into auditing. I did not use this feature in real applications but it seems to work like an observer pattern which is similar to events.

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 24-Mar-2009 06:03:53   

Hi Flynn,

I've done this successfully. Here's my code, which can either be in a.) a partial class for your CommonEntityBase, or b.) partial class per entity.

Hope this helps!

Ryan D. Hatch

Protected Overrides Sub OnSave()
    MyBase.OnSave()
    'Some Databases (ie, Access, SQLServer Compact Edition) don't support a true Timestamp field
    'For those Databases, Set Timestamp before Saving
    'Me.Timestamp.CurrentValue = System.DateTime.Now
End Sub
Flynn
User
Posts: 17
Joined: 28-Jan-2009
# Posted on: 24-Mar-2009 14:02:07   

Thank you. That's exactly what I was looking for.

I assume OnSave() is called prior to the database save occurring and SaveComplete() is called after the save is finished.

But is there any documentation of this that I am unaware of? I can't find either method mentioned in the normal or SDK help files.

x3mka
User
Posts: 6
Joined: 23-Mar-2009
# Posted on: 24-Mar-2009 14:51:28   

I found this in documentation:

Extending an entity by intercepting activity calls During the entity's lifecycle and the actions in which the entity participates, various methods of the entity are called, and which might be a good candidate for your own logic to be called as well, for example when the entity is initialized you might want to do your own initialization as well. The entity classes offer a variety of methods for you to override so you can make your code to be called in various situations. These methods start all with On and can be found in the LLBLGen Pro reference manual in the class EntityBase. The entity classes also offer events for some situations, like the Initializing and Initialized events.

If you want to perform a given action when one of these methods are called, you can override them in the generated entity classes, preferably using the methods discussed in Adding your own code to the generated classes.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 24-Mar-2009 20:45:19   

The section in the documentation is here

Matt

Flynn
User
Posts: 17
Joined: 28-Jan-2009
# Posted on: 24-Mar-2009 20:53:52   

Matt,

I appreciate the link. But there isn't any mentioned on the page of the EntityBase.OnSave() that I can find. That was the information I was specifically looking for.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 24-Mar-2009 21:03:37   

There is more information here

Matt