Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bubbling up events to be subscribed to

Tags:

c#

events

wcf

I have a class ChatManager, which has a ChatServer and a ChatClient(WCF) class inside of them.

I want my controller which instantiates the ChatManager to be able to subscribe to the UserConnected, UserDisconnected and MessageReceived events that are on the ChatClient.

What is the most elegant and logical way to do this? Is it silly for me to define the events in the ChatClient like I have, and then redefine the events in the ChatManager solely to pass the events up to the Controller without it having to deal with or know about the ChatClient? The ChatManager would subscribe to the events of the ChatClient, and then fire off its own events which the ChatController would be listening to.

I know WPF has the concept of bubbling up of events, but I don't know if that is for this type of scenario, since nothing is part of a user interface.

like image 567
Cowman Avatar asked Oct 22 '22 21:10

Cowman


1 Answers

I'd start by questioning whether both ChatManager and ChatController can both justify their own existence. Generally whenever you find yourself creating a "Manager" class, it really isn't necessary, especially if part of what it is doing consists of merely relaying messages.

Controller classes can struggle against SRP since their "responsibility" is quite broad. In cases where you want to delegate responsibility for certain behaviour then leave the responsibility for the ChatClient with the controller, and initialize a subordinate controller with the ChatClient (through a contract interface) so that it can interact with the client as needed. Just make sure that when you start registering for events that you de-register those events before discarding subordinates or the client, otherwise you'll be looking at managed memory leaks.

like image 190
Steve Py Avatar answered Oct 24 '22 10:10

Steve Py