Do private event handlers remove encapsulation?

Everyone knows that a private event handler can listen to an event from another class. (In the examples in the documentation, only private handlers are always used.)

An event handler is nothing more than a private method in another class than the calling event. Therefore, calling the handler from outside its class interrupts the encapsulation. Or am I missing something?

Sample code for completeness:

class Caller { public event EventHandler MyEvent; public void RaiseMyEvent() { MyEvent(this, EventArgs.Empty); } } class Receiver { private void MyPrivateHandler(Object sender, EventArgs e) { Console.WriteLine("I'm a private method!"); } public void Subscribe(Caller caller) { caller.MyEvent += this.MyPrivateHandler; } } 

and after subscribing receiver.Subscribe(caller); we can comfortably call a private method in the receiver class from the outside: caller.RaiseMyEvent(); .

This is a purely academic question, even a scholastic one. Moreover, I personally find this feature very convenient, practical and actually love it. This is really great: we can directly give other classes the right to call our private methods. (We can also not sign it and do many exciting things with delegates and events.) In any case, it still violates the encapsulation cleanliness ... or not?

PS: Thanks to Matthew Watson for pointing out the following nuance: when subscribing to an event, a private handler can be called exclusively by this event. Although if we make it public (or invoked through the public wrapper method), it can be called by anyone. This is a pretty big difference in availability.

PPS: And yes - I have never seen this problem mentioned in a textbook. If you know one, please leave a link.

+4
source share
3 answers

Obviously, when you call caller.RaiseMyEvent () from outside any class, you don't have to have a key that will handle the event.

Actually, I donโ€™t think that the Subscribe method in the receiver should be publicly available, this view defeats the target. If the recipient is interested in handling the event, he must subscribe, he must not allow others to subscribe. Thus, you save the status of the subscription and the method that is hidden with it.

There is no direct call to this private method in the Receiver class, and you should not assume anything. The receiving class may unsubscribe at any time.

It comes down to encapsulation, but I don't think it completely violates it. This is basically a broadcast system.

If some kind of television station advises people to go outside and steal an apple, and they actually do it, who is to blame? TV channel or people who steal? I would have thought the latter. The same applies to events, those who raise this event, do not worry about the consequences.

0
source

the private event handler is private, because you will only be asked to provide the user with the opportunity to use it as an event.

 private void MyPrivateHandler(Object sender, EventArgs e) { Console.WriteLine("I'm a private method!"); } public void Subscribe(Caller caller) { caller.MyEvent += this.MyPrivateHandler; } 

coincides with

 public void Subscribe(Caller caller) { caller.MyEvent += (sender,e)=>{Console.WriteLine("I'm a anonymous method!"); } } 
0
source

Despite the fact that this is a little off topic, this is related to your question, and you might find it interesting: a link to a blog post about setting up add and remove C # event handlers . Comments deserve attention. One of the relevant comments refers to this answer ("Event Acessors" by Stephen Tub) , from MSDN magazine, to a blog question.

0
source

All Articles