when to add event handler?

when to add event handler?

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


FreeAsInBeer posted on Monday, June 04, 2007

hi all, I was hoping someone could please give some advice about when to add an event handler?

I have a typical invoicing application, with a rough object model like this;

Invoice (BB)
   InvoiceLines (BLB)
      InvoiceLine (Child BB)


I want to my Invoice object to handle the ListChangedEvent so that it can refresh the totals as changes happen. like this;

on creation of a new Invoice;

        [RunLocal()]
        protected override void DataPortal_Create()
        {
             // initialise new values

            lines.ListChanged += new System.ComponentModel.ListChangedEventHandler(Lines_ListChanged);
        }

when loading an existing invoice from the db;

        private void DataPortal_Fetch(Criteria criteria)
        {
             // get from db

            lines.ListChanged += new System.ComponentModel.ListChangedEventHandler(Lines_ListChanged);
        }

My main question is "is it correct to add the handler in the DataPortal_Fetch"?

Also, in general, is this how you guys keep track of totals like this? It was either the handle events or use parent references, and the event way feels nicer.

thanks

ps: I picked up the tip in another thread to override OnDeserialized.

ajj3085 replied on Tuesday, June 05, 2007

Your approach is the one I take.  You do need to hook things up again in the OnDeserialized (if you're not using serialized event handlers)... but this would only likely show up if you start using remoting.  If the DP is in local, you probably wont' notice any problems..

Seems to work fine for me.

Copyright (c) Marimer LLC