Possible Save(forceUpdate) errata

Possible Save(forceUpdate) errata

Old forum URL: forums.lhotka.net/forums/t/6091.aspx


AndrewBurns posted on Tuesday, January 06, 2009

I was in the middle of doing some custom save logic in my business base class when I noticed this bit of code from CSLA's BusinessBase.cs(143-154):
    public T Save(bool forceUpdate)
    {
      if (forceUpdate && IsNew)
      {
        // mark the object as old - which makes it
        // not dirty
        MarkOld();
        // now mark the object as dirty so it can save
        MarkDirty(true);
      }
      return this.Save();
    }

Either I am just being stupid (which is possible as I haven't had my coffee yet today), or this is a bug:  If the object is new (not in the DB) then that implies it is also dirty (so call the data portal methods) UNLESS it is also deleted in which case a force delete would fail anyway.  Is this correct?

I think the line should read (not IsNew):
if (forceUpdate && !IsNew)

Do I need more coffee or do I get a star?

ajj3085 replied on Tuesday, January 06, 2009

That's the correct code.

The assumption when you call this method is that the BO actually ISN'T new at all, but for performance reasons you're going to pretend it is. Force update forces the DataPortal_Update to run, when normally the DataPortal_Insert would run.  This allows you to code your website so that you can do a less expensive Create instead of a Fetch + Save.

Does that make sense?

AndrewBurns replied on Tuesday, January 06, 2009

Ahhh.  Thank you Ajj.

I have been out of the CSLA scene for a while.  I misinterpreted the name to mean that it would force the data portal methods to run (Update) if IsDirty was false.

Thank you for the clarification.

Copyright (c) Marimer LLC