From what I've observed, methods that start with the word "On" fall into two categories:
A base class that defines an event will expose a
protectedmethod whose name starts with "On" followed by the event name in order for derived classes to be able to raise the event.
A base class exposes a
protected internal virtualmethod whose name starts with "On" which can be overridden by derived classes that want to replace the default behavior of that method.
#1 is straightforward and reasonable to me. By allowing the derived class to override the method, it guarantees that the event will be raised if and when the derived class calls the method.
#2 leaves me wondering, though, because I'm not sure if the associated or implied event in the base class will ever be raised unless the overridden method calls
base.On.... In the
protected internal virtual case, the method can even be called from other classes.
One example of #2 is
Microsoft.EntityFrameworkCore.DbContext: when the code generation automatically produces your derived class, it has two methods:
protected override void OnConfiguring and
protected override void OnModelCreating. Neither of the methods call the base class in the generated bodies. In this case -- since they are
protected internal, the base class must call these methods at some point. I'm not sure if there is even an event involved.
Another example of #2 is
System.Windows.Forms.Form. There are many
protected virtual methods, for instance
protected virtual void OnActivated. The description of that method is "Raises the System.Windows.Forms.Form.Activated event.", but of course that only happens if the method calls
Is there a definitive convention for using the word "On" at the beginning of certain method names? Must there always be an event involved, and should that event name always be used after the "On" in the method name? If an event is always associated with a method
On..., what can clients of your API assume will happen if they don't call
base.On... when they override the method? Can they assume the event won't be fired? If the latter is true, then why does the auto-generated code for
Microsoft.EntityFrameworkCore.DbContext.OnModelCreating not call the base class?