Notify when object saved and if new, modified, or deleted

Notify when object saved and if new, modified, or deleted

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


chrisb posted on Tuesday, May 22, 2007

I need to perform an action when an object is saved.  I need to know also if the object was new, modified, or deleted.  Is there an event I can wire up or a method to override to perform this?

Something like this:

  public override MyObject Save()
 {

    MyObject newObject = base.Save();
    if (object was new)
   {
      do something
   }
   else if (object was deleted)
   {
       do something different
   }

   ...
}

Or subscribe to an event?

 

ajj3085 replied on Tuesday, May 22, 2007

What about the Saved event from ISavable?

hurcane replied on Tuesday, May 22, 2007

Try checking the state of the object before you call the base.Save method. If it inherited from CSLA, it has IsNew and IsDeleted properties. Since CSLA objects save themselves, you have access to these properties.

You should also consider the case that an object is both new and deleted.

chrisb replied on Wednesday, May 30, 2007

My class derives from Csla.BusinessBase<MyClass>.  I don't know how to override BusinessBase<MyClass>.Save().  That would be ideal.

In the ISaveable.Saved event, I cannot tell if the object was originally Deleted, New, etc.  Example: I delete, call Save(), and ISaveable.Saved fires with SavedEventArgs.NewObject.IsDeleted == false.

 

 

chrisb replied on Wednesday, May 30, 2007

I see why I can't override BusinessBase.Save().  It is already overridden in the partial class generated using the CodeSmith templates.

ajj3085 replied on Wednesday, May 30, 2007

You should be able to move the override of save out of the generated code file into your own, so that you can customize it.

ajj3085 replied on Wednesday, May 30, 2007

Ahh, I see what you're getting at.

I would add a new interface, MyBusiness.ISavable define as:

public interface ISavable : Csla.ISavable {
    event EventHandler<SavingEventArgs> Saving;
}


SavingEventArgs can be whatever you need it to be.  Then on your custom BusinessBase (you did subclass BB and others, and use those as all your bases, right?) simply have BB implement your new interface.

RockfordLhotka replied on Wednesday, May 30, 2007

ajj3085:
Ahh, I see what you're getting at.

I would add a new interface, MyBusiness.ISavable define as:

public interface ISavable : Csla.ISavable {
    event EventHandler<SavingEventArgs> Saving;
}


SavingEventArgs can be whatever you need it to be.  Then on your custom BusinessBase (you did subclass BB and others, and use those as all your bases, right?) simply have BB implement your new interface.

Just remember that if you declare an event, and you want to ever use serialization, you must use the custom event declaration syntax - you can NOT use the one-line event declaration syntax. Look at the existing ISavable implementations in BusinessListBase or BusinessBase for examples.

chrisb replied on Thursday, May 31, 2007

Rocky,

  I was also thinking in of using static events that fire whenever any MyClass object changes:

public class MyClass : BusinessBase<MyClass>
{

      public static event MyClassSavedEventHandler MyClassSaved;

      public override MyClass Save()
      {
         ... Populate Event Args ....
        
        MyClass newClass = base.Save();
        OnMyClassSaved(this, eventArgs);
        return newClass;
      }
}

Any problems with this approach?

Chris

 

chrisb replied on Wednesday, May 30, 2007

 We did not subclass BusinessBase etc (yet) because we took what the CodeSmith templates gave us by default. I like your solution since it solves the problem in general of performing tasks based on deletions, addition, etc. 

Thanks for all the help.

Chris

 

Copyright (c) Marimer LLC