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?
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.
I see why I can't override BusinessBase.Save(). It is already overridden in the partial class generated using the CodeSmith templates.
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.
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
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