If it doesnβt feel right, because the events are more complex and more appealing than what is necessary for internal messages (which, in my opinion, is at least partially true, given that events can cause several clients, then you know that you only need to notify about it, right?), then I propose the following alternative. Instead of using events to communicate the completion of Foo with FooHandler, since Foo is internal, you can add a callback parameter to the constructor or the Load Foo method that Foo can call when this is done at boot time. This parameter can be just a function if you have only one callback, or it can be an interface if you have a lot of them. Here, as I think, your code will look with a simplified backend:
public delegate void FooLoaded(FooHandler sender, EventArgs e); class Foo { Action<Foo> callback; public void Load(Action<Foo> callback) { this.callback = callback; load_asynchronously(); } public void callMeWhenLoadingIsDone() { callback(this); } } class FooHandler { public event FooLoaded OneFooLoaded; public void LoadAllFoos() { foreach (Foo f in FooList) { f.Load(foo_loaded); } } void foo_loaded(Foo foo) {
Please note that this also allows you to more strictly print with the FooLoaded delegate.
If, on the other hand, he does not feel right, because the event does not have to go through FooHandler to get to the client, then 1) I would dispute this because if the client does not want to deal with the Foo person, he also should fire events at them at this level, and 2) If you really wanted to do this, you could implement some open callback interface on Foo, although Foo is private or use a mechanism similar to Pavel, I think, however, that customers like ease of implementation with less processing events and distinguishing the source within one handler, rather than the need to bind (and possibly disconnect) events from dozens of smaller objects.
Bluemonkmn
source share