Client Side Concurrency updates and EntityCollectionSaves

Posts   
 
    
Daniel9 avatar
Daniel9
User
Posts: 19
Joined: 05-Apr-2005
# Posted on: 12-Jul-2005 09:13:41   

Looking through the forums i've found a couple of mentions of Client side updates for concurrency control rather than using triggers or the timestamp functionality. The following post is pretty much what i was after (except i'm planning to use a date field called 'LastEdit' for auditing purposes):

http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=977


public override bool SaveEntity(...)
{
   // Updating an existing entity
   if (!entityToSave.IsNew)
    {
        IEntityFieldCore leField = entityToSave.Fields["LastEdit"];
        if ((leField != null)&& (!leField .IsChanged))
        {
            leField.SetNewFieldValue("LastEdit", DateTime.Now);
        }
    }
    return base.SaveEntity (entityToSave, refetchAfterSave, updateRestriction, recurse);
}

How does the above code work when saving an entity collection? I've been able to override the SaveEntity and set LastEdit for individual entity saves but how do you set fields when using the SaveEntityCollection method?

Also does this have an advantage in that the concurrency field does not have to be re-fethed from the database so it would be (slightly) more efficient?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 12-Jul-2005 10:43:57   

SaveEntityCollection traverses its collection it gets and calls SaveEntity on each entity which has to be saved. So overriding SaveEntity will be sufficient as that's the only part which actually performs a save simple_smile

Also does this have an advantage in that the concurrency field does not have to be re-fethed from the database so it would be (slightly) more efficient?

You mean, you first fetch the last date, then update it ? I'm not sure if I understand what you mean exactly.

Frans Bouma | Lead developer LLBLGen Pro
Daniel9 avatar
Daniel9
User
Posts: 19
Joined: 05-Apr-2005
# Posted on: 13-Jul-2005 02:32:33   

Otis wrote:

SaveEntityCollection traverses its collection it gets and calls SaveEntity on each entity which has to be saved. So overriding SaveEntity will be sufficient as that's the only part which actually performs a save simple_smile

Thanks for that, i was looking the wrong place (as usual flushed )

Finally got it working, my final code for anyone who's trying to do a similar thing:

            public class ConcurrencyFilterFactory: IConcurrencyPredicateFactory
    {
        public IPredicateExpression CreatePredicate(ConcurrencyPredicateType predicateTypeToCreate, object containingEntity)
        {
            IPredicateExpression toReturn = new PredicateExpression();

            IEntity2 entity = (IEntity2)containingEntity;

                switch(predicateTypeToCreate)
                {
                    case ConcurrencyPredicateType.Delete:   
                    case ConcurrencyPredicateType.Save:     
                        toReturn.Add(new FieldCompareValuePredicate(entity.Fields["LastEdit"],null, ComparisonOperator.Equal)); 
                        break;
                }

            return toReturn;
        }
    }