Question on overriding OnSave

Posts   
 
    
dem3tre
User
Posts: 7
Joined: 25-Dec-2006
# Posted on: 07-Apr-2007 03:52:12   

I am working on my first "real" project with LLBLGen 2.0 and am wondering how others maintain basic row creation/modified DateTime fields. For this project I am using SelfServicing.

I have a method in a helper class:

        public static IEntity OnSaveUpdateDateTimeFields(IEntity toSave)
        {
            if (toSave != null)
            {
                if (toSave.IsNew)
                {
                    // only update the CreatedDate column once
                    if (toSave.Fields["CreatedDate"] != null)
                    {
                        toSave.SetNewFieldValue("CreatedDate", DateTime.UtcNow);
                    }

                    if (toSave.Fields["ModifiedDate"] != null)
                    {
                        toSave.SetNewFieldValue("ModifiedDate", DateTime.UtcNow);
                    }
                }

                if (toSave.IsDirty)
                {
                    if (toSave.Fields["ModifiedDate"] != null)
                    {
                        toSave.SetNewFieldValue("ModifiedDate", DateTime.UtcNow);
                    }
                }
            }

            return toSave;
        }

If I were using Adapter I would override OnBeforeEntitySave to call this, but in SS? I see the OnSave method but I don't see how I can call out to OnSaveUpdateDateTimeFields from there -- and I don't want to have this code duplicated in all my entity objects.

I've read through the reference guide, been through the documentation, and consulted mister Google to no avail. I'm missing how to make this work. I could hoist this call in a validator class but it feels wrong to be changing values there.

Could someone shed some light on what I'm missing? Thanks!

P.S. The above is not used for concurrency, for that I've got a timestamp column and a concurrency predicate factory.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39922
Joined: 17-Aug-2003
# Posted on: 08-Apr-2007 19:30:39   

Override UpdateEntity or InsertEntity in the entity itself, then modify your entity there.

Frans Bouma | Lead developer LLBLGen Pro
dem3tre
User
Posts: 7
Joined: 25-Dec-2006
# Posted on: 16-Apr-2007 04:37:15   

Thanks Otis. I ended up using the CommonEntity templates and put my overrides there. Works like a charm.