Custom code when saving a collection

Posts   
 
    
Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 25-Mar-2010 18:01:11   

Hi,

I currently codegen my bl using an llbl template. I have a Save method for each entity that saves an entity. This method will automatically update a column with the time and date prior to the Save.

        Public Shared Function SaveEntity(ByVal Entity As JrHighHistoryEntity, Optional ByVal refetchAfterSave As Boolean = True, Optional ByVal updateTimeStamp As Boolean = True) As Boolean
            If (updateTimeStamp AndAlso Entity.IsDirty) OrElse Entity.IsNew Then
                Entity.TimeStampDate = CInt(Date.Now.ToString("yyyyMMdd"))
                Entity.TimeStampTime = CInt(Date.Now.ToString("hhmmss"))
            End If

            Using adapter As New CustomDataAccessAdapter
                Return adapter.SaveEntity(Entity, refetchAfterSave)
            End Using

        End Function

I'm not sure how I would do that when i want to update a collection.

For various reasons I can't use a trigger to do this.

Any help would be appreciated.

Using adapter with latest production build.

Thanks,

Fishy.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 25-Mar-2010 18:49:15   

The best place to do this is to use the hooks provided by LLBLGen itself, rather than trying to create your own.

I ususally use something like


// __LLBLGENPRO_USER_CODE_REGION_START CustomDataAccessAdapterCode

        
        protected override void OnBeforeEntitySave(IEntity2 entitySaved, bool insertAction)
        {
            string userName = WindowsIdentity.GetCurrent().Name.ToString();
            if (entitySaved.Fields["CreatedDate"] != null)
            {
                if (entitySaved.Fields["CreatedDate"].CurrentValue == null)
                {
                    entitySaved.Fields["CreatedDate"].CurrentValue = DateTime.Now;
                }   
            }

            if (entitySaved.Fields["CreatedBy"] != null)
            {
                if (entitySaved.Fields["CreatedBy"].CurrentValue == null)
                {
                    entitySaved.Fields["CreatedBy"].CurrentValue = userName;
                }
            }
            if (entitySaved.Fields["ModifiedDate"] != null)
            {
                entitySaved.Fields["ModifiedDate"].CurrentValue = DateTime.Now;
            }
            if (entitySaved.Fields["ModifiedBy"] != null)
            {
                entitySaved.Fields["ModifiedBy"].CurrentValue = userName;
            }
            

        }

in the custom user code region of the generated DataAccessAdapter to do exactly what you are after - it works perfectly.

See the documentation at http://www.llblgen.com/documentation/2.6/Using%20the%20generated%20code/gencode_tapinroutinesevents.htm

Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 25-Mar-2010 19:02:17   

Thank you for your quick response. simple_smile

Is there a way to pass parameters to it. In my example I pass 'updateTimeStamp' and I only update thoses if that is set to True.

Thanks,

Fishy

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 25-Mar-2010 21:01:28   

Not directly into that function, but you could add a property to the entity itself to control that flag, that you would need to set before saving the entity...


   entity1.UpdateTimeStamp = true

and in the function



if (entity.UpdateTimeStamp)
{...
}

Just out of interest, why would you not want to update a field like this on an entity - doesn't that kind of defeat the point of having it there? simple_smile

Matt

Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 25-Mar-2010 21:17:13   

MTrinder wrote:

Just out of interest, why would you not want to update a field like this on an entity - doesn't that kind of defeat the point of having it there? simple_smile

The only time we would not want this updated is when we run maintenance programs that fix the data or a mass change of data. We really want to capture the last time a user updated the data.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-Mar-2010 03:27:41   

The flag Matt mentioned is the way to go then. You can place it in CommonEntityBase so you have to for all entities.

David Elizondo | LLBLGen Support Team
Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 26-Mar-2010 15:47:01   

Thank you both for providing a solution to my problem.