StrataFrame Forum

BO's BeforeSave Event

http://forum.strataframe.net/Topic464.aspx

By Scott - 1/23/2006

In the BeforeSave event on my BO I am adding records to a secondary table using another BO.  This secondary BO is adding the records correctly,  but it appears that the BeforeSave event on the primary BO is fireing twice.  Any suggestion on what I should be checking.

Scott

By StrataFrame Team - 1/24/2006

Are you saving through the form, or directly on the BO? Maybe you could post a little code snippet of where you're actually calling the save and how your business objects are configured...
By Scott - 1/24/2006

I am using a Maintenance form and the only thing that is set on the BO is the required fields collection.  Here are some of the methods on the BO itself.

        private void SFCategoryBO_CheckRulesOnCurrentRow(CheckRulesEventArgs e)

        {

            if (!this.CategoryDescriptionIsUnique())

                this.AddBrokenRule(SFCategoryBOFieldNames.CatDesc,

                    Localization.RetrieveTextValue("DuplicateCategoryDescription"));

        }

        private void SFCategoryBO_SetDefaultValues()

        {

            this.Created = DateTime.Now;

            this.CreatedUsrID = oApp.oUser.UserID;

        }

        private void SFCategoryBO_BeforeSave(MicroFour.StrataFrame.Data.BeforeSaveUndoEventArgs e)

        {

            System.Console.WriteLine("Before Save Event - " + DateTime.Now.ToString());

            this.LastUpd = DateTime.Now;

            this.LastUpdUsrID = oApp.oUser.UserID;

        }

I end up with two "Before Save Event" messages in the console.

By StrataFrame Team - 1/24/2006

Within your InitializeComponent() method, of the business object, you should find a line similar to:



this.BeforeSave += new BeforeSaveEventHandler(SFCategoryBO_BeforeSave);



Make sure you don't have two of those lines.



Also, are you doing anything special like using the business object on a child form that's shown through a ChildFormDialog, or within a UserControl?



I've checked, and there's no place that the event is being raised twice, but it's possible that the event handler is being added twice...
By Scott - 1/24/2006

Did not find any duplicate handlers.  Try this,  I went to the Strataframe sample windows application and added a beforesave event by double clicking on the event in the property sheet.  I then put the following code in the event:

System.Console.WriteLine("Before Save Event")

run the form, locate a record, click the edit button, (do nothing) click the save button, and look in the console, there is two "Before Save Event" messages. Is this the expected result?  I hope I just am not miss-understanding how the event is supposed to work.

By StrataFrame Team - 1/25/2006

OK, found the problem... there was an event that was being raised from within the DataLayer, not the BusinessLayer, and it was getting bubbled up and re-raised from within the BusinessLayer. I'll get the update posted on the Developers blog shortly (running all of my unit tests right now) and then we'll get the install rebuilt.



So, no, your concept of the BeforeSave event is the same as our concept of the BeforeSave event... it should only be firing once.
By Scott - 1/25/2006

Great.  Nice to know I am not going crazy and starting to understand how the framework works.  How would you debug something like an event anyway?  I wouldn't even know the first place to start.

Scott

By StrataFrame Team - 1/26/2006

Hehe, events are special... it's quite difficult to debug them. Within the framework, where we call RaiseEvent, you can step into the line and you will step into all of the methods attached to that event.



BTW, the update is posted on the Developers Blog.
By Scott - 1/26/2006

Thanks for the update.  Works great.
By StrataFrame Team - 1/26/2006

No problem, we'll get the install rebuilt and posted as soon as we can Smile