Re: Consuming your own events.

Frans Bouma <[email protected]> Wed, 30 Jan 2008 15:42:25 +0100
Newsgroups gmane.comp.windows.devel.dotnet.clr
Message-ID <00b401c8634e$54812d90$fd8388b0$@nl>
I did remember I had followed the MS guidelines for this in our code and
checked, and indeed, in the few cases where we use this, we too have
implemented the same pattern: protected virtual On... methods for raising
events and for tap-in methods. I recall finding it a bit odd, but we'd like to
stick with the guidelines provided in the MSDN, so it's done this way, however
if I could recall that decision I would.

It's not bad in all cases, but a developer who's not careful can accidently
avoid the events being raised.

One ADVANTAGE of having it in 1 method is: order of when takes place. If you
have this:

private void BeginInitializing()
{
        RaiseInitializing();
        OnInitializing();
}

protected virtual void OnInitializing()
{
        // nop
}

private void RaiseInitializing()
{
        if(Initializing!=null)
        {
                Initializing(this, new EventArgs());
        }
}

it might be that OnInitializing is called too late, or should be called
earlier by the developer. With a virtual method which combines both, you can
do that, with this method, you can't.

                FB

>         I don't find OnXXX useful for event raising methods if they're
virtual
> btw. That's combining two things: extensibility when something happens AND
> raising an event that something happened. I think that's the issue here and
I
> agree with you that that's not right. HOW these virtual methods which are
> meant to be used for extensibility should be called... I don't think we all
> will agree on that, nor do we have to.
>
>                 FB

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com