OnSaved event in CSLA.NET 2.1 RC

OnSaved event in CSLA.NET 2.1 RC

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


Curelom posted on Friday, September 29, 2006

I'm having a difficult time getting the Saved event from the BusinessBase to fire (or capture it anyway)

I have a Customer object with the Save method overridden

public override Customer Save()

{

if (IsDeleted && !CanDeleteObject())

throw new System.Security.SecurityException("User not authorized to remove a Customer");

else if (IsNew && !CanAddObject())

throw new System.Security.SecurityException("User not authorized to add a Customer");

else if (!CanEditObject())

throw new System.Security.SecurityException("User not authorized to update a Customer");

return base.Save();

}

In my form I wire up the event like this

_customer.Saved += new EventHandler<Csla.Core.SavedEventArgs>(_customer_Saved);

void _customer_Saved(object sender, Csla.Core.SavedEventArgs e) {

throw new Exception("The method or operation is not implemented.");

}

When I make changes to the customer and save, it saves fine, but does not call the _customer_Saved method.

Am I doing anything wrong here?

Curelom replied on Friday, September 29, 2006

Ok, I found my problem.  I was following the recommended save routing in the book.

Customer temp = _customer.Clone();

temp.ApplyEdit();

try {

_customer = temp.Save();

_customer.BeginEdit();

So I'm actually calling the save on the clone which hasn't been wired up.  So now I just need to figure the best way to wire the save event up in some usercontrols on the form that also have the customer object passed to them.

 

RockfordLhotka replied on Friday, September 29, 2006

Curelom:

Ok, I found my problem.  I was following the recommended save routing in the book.



Ahh, you should know, then, that there's errata about that. There are some issues with data binding when following that approach, and so you probably want to look at PTWin in version 2.1 and follow the new model in ProjectEdit.

Curelom replied on Friday, September 29, 2006

Thanks for the heads up.Smile [:)]  I've created an EndEdit method for each of my controls on the form that I call before I save, to EndEdit any bindingsources in them.  I then listen for the DataSourceChanged event that fires when I rebind.  It's working good so far.

RockfordLhotka replied on Friday, September 29, 2006

I just tested both the VB and C# frameworks, and the event does work.

My guess is that you aren't hooking the event properly - or from the correct object. You can't hook the event until after you've created the instance of the object with which you are going to work, and of course you need to rehook the event after the Save() call, because you then have a new object with a new event hookup.

Copyright (c) Marimer LLC