Best practice for "update by" columns

Posts   
 
    
wojo
User
Posts: 69
Joined: 10-Mar-2004
# Posted on: 11-Mar-2004 17:25:46   

Let me explain my situation a little. Every table in my database has an UpdateBy and CreateBy column, FKs to the User table. This includes the User table with FKs to itself.

Whenever an new entity is persisted, I should set CreateBy to the currently logged in user. On all saves, UpdateBy should be set to the logged in user.

I'm curious what the best way to automate this is. Right now I have derived my own DataAccessAdapter and overriden the SaveEntity() method:


        public override bool SaveEntity(IEntity2 entityToSave, bool refetchAfterSave, 
            IPredicateExpression updateRestriction, bool recurse)
        {
            PrepEntity(entityToSave);
            return base.SaveEntity(entityToSave, refetchAfterSave, updateRestriction, recurse);
        }

Which calls PrepEntity (_loggedInUser is a UserEntity object):


        private void PrepEntity(IEntity2 entity)
        {
            if (!entity.IsDirty && !entity.IsNew)
                return;

            for (int i = 0; i < entity.Fields.Count; i++)
            {
                IEntityField2 f = (IEntityField2) entity.Fields[i];

                if (f.Name == "UpdateBy" || f.Name == "CreateBy" && entity.IsNew)
                    f.CurrentValue = _loggedInUser.Id;
            }
        }

The current problem I have is that the _loggedInUser must be a UserEntity that has an Id already. I can't add a UserEntity and also some other row in another table all in one shot. I have to save the user first, then set _loggedInUser to a UserEntity with an Id, then do the rest of my saves.

I could get around this if I set the related entity property instead of the UpdateBy/CreateBy int columns directly, then LLBLGen could handle the dependencies for me... but I don't know how to do this dynamically. I suppose if I named all of the relationships "UserUpdateBy" and "UserCreateBy" and used reflection on the resulting properties, I could set them... but that pretty dirty stuck_out_tongue_winking_eye

I could call IEntity2's SetRelatedEntity directly, but it looks like the properties have logic that I can't possibly know about (look at the generated code for the setter properties for related entities).

Other ways I can think of would require changes to the entities, which I don't want to do. This should all be automatic for me simple_smile

Any ideas?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39617
Joined: 17-Aug-2003
# Posted on: 12-Mar-2004 21:45:49   

I'll dive into this question tomorrow. simple_smile (this post is just to kick this thread upwards wink )

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39617
Joined: 17-Aug-2003
# Posted on: 13-Mar-2004 14:44:01   

If CreatedById is nullable, you can set CreatedBy to the user instance, save the user instance and it will in fact save itself twice with the correct values simple_smile

Frans Bouma | Lead developer LLBLGen Pro