StrataFrame Forum

Question: Original BO and subsequent copies

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

By Ben Hayat - 3/15/2007

a) I create a BO in a project and then I build the project. The BO appears on the Toolbox.

b) I then place a copy of the BO from Toolbox and put on my form.

c) I add some events to the original BO.

d) I add some events to the copy of my BO on the form.



Is it the correct understanding that SF will always execute the event's codes on the original BO first and then on any subsequent BO that were placed on the forms?



Thanks!
By Trent L. Taylor - 3/15/2007

The order in which a BO is dropped on a form has nothing to do with it actually...unless you ignore the INitializationPriority property.  You can get more specs on this in the help, but you can set the InitializationPriority property on a BO and actually control which order in which they initialize.
By Ben Hayat - 3/15/2007

Ok, I went ahead and put MessageBox on the main BO in the "BeforeSave" event and also a MessageBox the form's BO "BeforeSave" and I left the InitializationPriority=50.



I run the app and I see the MessageBox in the original BO shows first and then the one the form.



I then changed those value to 1 and 100 and vice versa, and still see the original BO gets executed first.



My question was mainly related to the order that which BO events get fired first (The main BO or subsequent ones) and not how it gets initialized on the form.

Thanks!
By Ben Hayat - 3/15/2007

I got the following from the help and makes sense for setting and over writting properties:

A business object is a component, and therefore, utilizes .NET's Component Designer. Because of this, there are two differet places for you to configure a property on a business object: the Component Designer and an a business object instance's property sheet in a form designer. A change to a business object in the Component Designer affects all instances of that business object and is known as a "class-level" change, while a change to the property sheet of an instance of a business object only affects that instance of the business object and is known as an "instance-level" change.



Many changes to business objects should be made within the Component Designer for that business object as you do not want to have to make the change every time you drop a new instance of the business object on the form. You can, however, override any property on a business object either through the business object instance's property sheet or programmatically at runtime.





Can the events be over written, or the BO Components gets fired first and then the Instance?
By Trent L. Taylor - 3/15/2007

I guess I did not ask which events you were referring to.  The events are fired in the order of the BusinessObjects collection on the form.  If you refer to the BusinessObjects collection on the form, this is the order in which the BOs are going to fire the events.

The InitializationPriority has to do with instantiation order basically.  It controls the order that the ParentFormLoading event is fired.

By Ben Hayat - 3/15/2007

Trent, thanks for the reply; However, I think I have misled you with my question. Let me try it differently.

The two BOs that I'm referring to, are NOT on the form.



First I created my CustomerBO in the project which is basically considered as the CustomerBO Class.

Then I placed a copy of that CustomerBO from the toolbox on my form CustomerBO1.



My question is, when event on CustomerBO1 is fired (i.e. BeforeSave), does the same event on the CustomerBO Class is fired FIRST?
By StrataFrame Team - 3/16/2007

Any events that are shared by the form and by the business object (like BeforeSave) are called on the business object first.  However, when you have a BO class defined in your application, the event is only raised on the instance, not within the class.

However, I think you might be talking about your event handlers for that event... if you handle the event in both your CustomersBO class and on the form where you dropped CustomersBO1, then most likely, the handler within the class will be invoked first.  However, with event handlers, you can never be assured what order they will be invoked in (because a multicast delegate does not keep track of the order of the handlers), so, when you want to handle an event within the class that declares it, you will almost always want to override the On"EventName" method instead of actually "handling" the event.  This way, you can control whether your code within the class will be executed before or after all of the handlers on the event.  The actual call to the base method will raise the event, so you can place it at either the top, the bottom, or somewhere in the middle of your overriding method, and the event will be raised then.  Like this:

Protected Overrides Sub OnBeforeSave(ByVal e As BeforeSaveUndoEventArgs)
    '-- Call the base method to raise the event before the code in this method is executed
    MyBase.OnBeforeSave(e) '-- The event will be raised here

    '-- Do any extra code

End Sub

The OnEvent() definition is standard .NET coding practices, so just about any time you want to handle an event in a derived class you'll want to override that instead.

By StrataFrame Team - 3/16/2007

By the same token, since the OnEvent() methods are protected, you can call them (if you need to) to tell the base class to raise that event.
By Ben Hayat - 3/16/2007

Ben Chase (03/16/2007)
Any events that are shared by the form and by the business object (like BeforeSave) are called on the business object first. However, when you have a BO class defined in your application, the event is only raised on the instance, not within the class.



However, I think you might be talking about your event handlers for that event... if you handle the event in both your CustomersBO class and on the form where you dropped CustomersBO1, then most likely, the handler within the class will be invoked first. However, with event handlers, you can never be assured what order they will be invoked in (because a multicast delegate does not keep track of the order of the handlers), so, when you want to handle an event within the class that declares it, you will almost always want to override the On"EventName" method instead of actually "handling" the event. This way, you can control whether your code within the class will be executed before or after all of the handlers on the event. The actual call to the base method will raise the event, so you can place it at either the top, the bottom, or somewhere in the middle of your overriding method, and the event will be raised then. Like this:



Protected Overrides Sub OnBeforeSave(ByVal e As BeforeSaveUndoEventArgs)

'-- Call the base method to raise the event before the code in this method is executed

MyBase.OnBeforeSave(e) '-- The event will be raised here



'-- Do any extra code



End Sub



The OnEvent() definition is standard .NET coding practices, so just about any time you want to handle an event in a derived class you'll want to override that instead.




Ben, thank you for getting my point. What you described is what I was actually trying to discover/uncover for myself.

Great explanation. Thank you!



This product is deep... Got a lot to learn. I hope I can make it to training class!
By Trent L. Taylor - 3/16/2007

I hope I can make it to training class!

I hope you can make it....it is definintely worth the time and money!!!!

By Ben Hayat - 3/17/2007

Off Topic:



Trent, why is it, when I use the quote "{}" feature of this Forum, my quote does not appear like your above post. The first quote I did, I clicked on {} button, and you can see how it showed. Then when I was quoting Ben's, I clicked on "Quote" button, and you can see again how it appears.



I only see this problem with this software and nobody else.



Note: I tried it with IE, FF and Netscape!
By StrataFrame Team - 3/19/2007

I don't think it's a problem with the way you are quoting things... I think it's a problem with the way we might have permissions configured within the forum software.  I'll look into it and see.  You'll notice that the [codesnippet] also only works when Trent or I use it, but not anyone else...
By Ben Hayat - 3/19/2007

Ben, while you're looking at the forum configuration, can you also check for "Edit" permission? There has been times, after posting, I wanted to go back to edit or add, but it won't let me, dueto permission.



Thanks!
By Trent L. Taylor - 3/19/2007

We fixed the RegEx string that InstantASP was using so the quote and CodeSnippet will now work.  As for the edit, we have intentionally not allowed this.  We have discussed allowing you to edit your own posts...but there are some concerns with this as well.  We would have to constantly monitor the edits as well as the new entries.  New entries are easy to monitor, but edits would require that we continually go back and make sure that previous posts are not being changed illegitamtely (going back and making changes that change the downstream posts and/or to post incorrect or invalid information)...which with 99% of the forum users this would not be an issue.  So this is not a new request...and we may turn it on, but for now it is off Ermm
By Ben Hayat - 3/19/2007

Thanks for the fix and I do see your point about the "Edit" feature which you loose trace of what has been said before. It's no big deal, worst come to worst, we can add another post to follow it up.



Just got off the phone with an old friend who is also switching from Delphi to C# and I told him how much he will need SF for darabase development. He or his company might be joining us soon!
By Trent L. Taylor - 3/19/2007

Just got off the phone with an old friend who is also switching from Delphi to C# and I told him how much he will need SF for darabase development. He or his company might be joining us soon!

This is great news!

By Greg McGuffey - 3/19/2007

We fixed the RegEx string that InstantASP was using so the quote and CodeSnippet will now work.




MessageBox.Show("Cool! This didn't used to work!")